test.c
来自「一个Linux串口通信小程序.使用时请注意修改MAKEFILE.」· C语言 代码 · 共 53 行
C
53 行
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <pthread.h>int main(void){ int fd; char rxBuffer[1024]; int len = 1024; int rxByte; struct termios opt; fd = open("/dev/ttyS1",O_RDWR); if(fd==-1) { perror("error"); } //set bps tcgetattr(fd,&opt); cfsetispeed(&opt,B9600); cfsetospeed(&opt,B9600); //set raw input and output opt.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG); opt.c_oflag &= ~OPOST; tcsetattr(fd,TCSANOW,&opt); //print tips printf("Please enter any data to the COM1, Enter key to confirm\n"); write(fd,"READY!\n",7); //receive bytes and send back while(1) { rxByte = 0; rxByte = read(fd,rxBuffer,len); if(rxByte>0) { rxBuffer[rxByte]='\0'; write(fd,"RE:",3); write(fd,rxBuffer,rxByte); write(fd,"\n",1); printf("%s\n",rxBuffer); } } close(fd); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?