linux下串口测试程序 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <sys/time.h> #include <time.h> #include <string.h> #include <sys/ioctl.h> #include <termios.h> #include <stdint.h> #include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/spi/spidev.h> #include <string.h> #include <assert.h> #include <netinet/in.h> #include <sys/types.h> #include <signal.h> #include <stdlib.h> #include <sys/time.h> #define msleep(x) usleep(x*1000) void my_printf(const char* head_l, unsigned char*date_in, int length_l) { int k; printf(“%s “, head_l); for ( k = 0; k < length_l; k++) { printf(“%02x “, date_in[k]); } printf(” ”); } int select_serial_device(int dev_id, char *name) { switch (dev_id) { case 1: strcpy(name, “/dev/ttyS1”); break; case 2: strcpy(name, “/dev/ttyS2”); break; case 3: strcpy(name, “/dev/ttyS3”); break; case 4: strcpy(name, “/dev/ttyS4”); break; case 5: strcpy(name, “/dev/ttyS5”); break; case 6: strcpy(name, “/dev/ttyS6”); break; case 7: strcpy(name, “/dev/ttyS7”); break; case 8: strcpy(name, “/dev/ttyS8”); break; case 9: strcpy(name, “/dev/ttyS9”); break; case 10: strcpy(name, “/dev/ttyS10”); break; case 11: strcpy(name, “/dev/ttyS11”); break; default: return -1; } return 0; } unsigned int choose_baud_rate(int br_id) { unsigned int baud; switch (br_id) { case 0: baud = B0; break; case 50: baud = B50; break; case 75: baud = B75; break; case 110: baud = B110; break; case 134: baud = B134; break; case 150: baud = B150; break; case 200: baud = B200; break; case 300: baud = B300; break; case 600: baud = B600; break; case 1200: baud = B1200; break; case 1800: baud = B1800; break; case 2400: baud = B2400; break; case 9600: baud = B9600; break; case 19200: baud = B19200; break; case 38400: baud = B38400; break; case 57600: baud = B57600; break; case : baud = B; break; case : baud = B; break; case : baud = B; break; case : baud = B; break; default: printf(“input error: baud rate not correct”); return -1; } return baud; } int init_serial_device(char *name, int baud) { int fd; int ret; struct termios options; fd = open(name, O_RDWR | O_NDELAY | O_NOCTTY); if (fd == -1){ printf(“%s: open error ”, name); return -1; } ret = tcgetattr(fd, &options); if (-1 == ret) return -1; options.c_cflag &= ~CSIZE; //屏蔽其他标志 options.c_cflag |= CS8; //将数据位修改为8bit options.c_cflag &= ~PARENB; //无校验 options.c_cflag &= ~CSTOPB; // 设置一位停止位; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); cfsetispeed(&options, baud); cfsetospeed(&options, baud); options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); ret = tcsetattr(fd, TCSANOW, &options); if (-1 == ret) return -1; return fd; } void process_serial_data(int fd) { char sendbuf[64] = “789”; char recvbuf[64] = “”; int ret; while (1){ ret = write(fd, sendbuf,36); //发送数组 my_printf(“send:”,sendbuf,ret); msleep(100); while(1){ memset(recvbuf, 0, sizeof(recvbuf)); ret = read(fd, recvbuf, 255); if (ret == 0) continue; if(ret>0){ my_printf(“recv:”,recvbuf,ret); break; } } msleep(200); } } int main(int argc, char argv) { int fd; int bn; int ret; char name[12]; char sn; speed_t baud; if (argc < 2){ printf(“input error:*argv=1-11 ”); exit(-1); } if (argc > 3){ printf(“input error: less than 4 parameters ”); exit(-1); } ret = sscanf(argv[1], “%d”, &sn); ret = select_serial_device(sn,name); if (-1 == ret) return -1; ret = sscanf(argv[2], “%d”, &bn); baud = choose_baud_rate(bn); if (-1 == ret) return -1; fd = init_serial_device(name,baud); if (-1 == fd) return -1; process_serial_data(fd); return 0; }
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/61614.html