1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
| [root@localhost serial]# cat serial.c #include<stdio.h> /*标准输入输出定义*/ #include<stdlib.h> /*标准函数库定义*/ #include<unistd.h> /*Unix 标准函数定义*/ #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> /*文件控制定义*/ #include<termios.h> /*PPSIX 终端控制定义*/ #include<errno.h> /*错误号定义*/ #include<string.h>
#define FALSE -1 #define TRUE 0
int UART_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity) {
int i; int status; int speed_arr[] = { B115200, B19200, B9600, B4800, B2400, B1200, B300}; int name_arr[] = {115200, 19200, 9600, 4800, 2400, 1200, 300};
struct termios options;
if( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); }
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { cfsetispeed(&options, speed_arr[i]); cfsetospeed(&options, speed_arr[i]); } }
options.c_cflag |= CLOCAL; options.c_cflag |= CREAD;
switch(flow_ctrl) {
case 0 : options.c_cflag &= ~CRTSCTS; break;
case 1 : options.c_cflag |= CRTSCTS; break; case 2 : options.c_cflag |= IXON | IXOFF | IXANY; break; } options.c_cflag &= ~CSIZE; switch (databits) { case 5 : options.c_cflag |= CS5; break; case 6 : options.c_cflag |= CS6; break; case 7 : options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return (FALSE); } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; options.c_iflag &= ~INPCK; break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); options.c_iflag |= INPCK; break; case 'e': case 'E': options.c_cflag |= PARENB; options.c_cflag &= ~PARODD; options.c_iflag |= INPCK; break; case 's': case 'S': options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; break; default: fprintf(stderr,"Unsupported parity\n"); return (FALSE); } switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return (FALSE); }
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cc[VTIME] = 1; options.c_cc[VMIN] = 1;
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("com set error!\n"); return (FALSE); } return (TRUE); }
int UART_Open(char* port) {
int err; int fd ; fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY); if (FALSE == fd) { perror("Can't Open Serial Port"); return(FALSE); } if(fcntl(fd, F_SETFL, 0) < 0) { return(FALSE); }
do { err = UART_Set(fd,9600,0,8,1,'N'); } while(FALSE == err || FALSE == fd);
return fd; }
void UART_Close(int fd) { close(fd); }
int UART_Recv(int fd, char *rcv_buf,int data_len) { int len,fs_sel; fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read); FD_SET(fd,&fs_read);
time.tv_sec = 10; time.tv_usec = 0;
fs_sel = select(fd+1,&fs_read,NULL,NULL,&time); if(fs_sel) { return read(fd,rcv_buf,data_len); } else { return FALSE; } }
int UART_Send(int fd, char *send_buf,int data_len) { int len = 0;
len = write(fd,send_buf,data_len); if (len == data_len ) { return len; } else {
tcflush(fd,TCOFLUSH); return FALSE; }
}
int main(int argc, char **argv) { int fd; int len; int i; char rcv_buf[100]; if(argc <= 3) { printf("Usage: %s /dev/ttySn 0(send data)/1 (receive data) \n",argv[0]); return FALSE; }
fd = UART_Open(argv[1]);
len = UART_Send(fd, argv[3], strlen(argv[3])); if(len <= 0) { return FALSE; }
usleep(1000 * 50);
len = UART_Recv(fd, rcv_buf,99); if(len > 0) { printf("%s", rcv_buf); }
UART_Close(fd); }
[root@localhost serial]#
|