异步串行是指UART(Universal Asynchronous Receiver/Transmitter),通用异步接收/发送。
UART包含TTL电平的串口和RS232电平的串口 RS232电平 逻辑1为-3~-15V的电压, 逻辑0为3~15V的电压 笔记本通过RS232电平和单片机通信
cp ../wiringOP/examples/serialTest.c .
修改系统自带的串口文件,使用线程进行读和发
/** serialTest.c:*/#include
#include
#include
#include
#include
#include
#include int fd;void *Sendhandler()
{char *sendBuf;sendBuf = (char *)malloc(32 * sizeof(32));while(1){memset(sendBuf,'\0',32);scanf("%s",sendBuf);while(*sendBuf){serialPutchar (fd, *sendBuf++);}}
}void *Revhandler()
{while(1){while (serialDataAvail (fd)){printf ("%c", serialGetchar (fd)) ;fflush (stdout) ;}}}int main ()
{int count ;unsigned int nextTime ;pthread_t idSend;pthread_t idRev;if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0){fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;return 1 ;}pthread_create(&idSend,NULL,Sendhandler,NULL);pthread_create(&idRev,NULL,Revhandler,NULL);if (wiringPiSetup () == -1){fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;return 1 ;}while(1){sleep(10);}printf ("\n") ;return 0 ;
}
用source insight软件进行阅读系统自带的串口文件
uartTool.h
int myserialOpen (const char *device, const int baud);void serialSendstring (const int fd, const char *s);int serialGetstring (const int fd,char *buffer);
uartTool.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include #include "wiringSerial.h"int myserialOpen (const char *device, const int baud)
{struct termios options ;speed_t myBaud ;int status, fd ;switch (baud){case 9600: myBaud = B9600 ; break ;case 115200: myBaud = B115200 ; break ;default:return -2 ;}if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)return -1 ;fcntl (fd, F_SETFL, O_RDWR) ;// Get and modify current options:tcgetattr (fd, &options) ;cfmakeraw (&options) ;cfsetispeed (&options, myBaud) ;cfsetospeed (&options, myBaud) ;options.c_cflag |= (CLOCAL | CREAD) ;options.c_cflag &= ~PARENB ;options.c_cflag &= ~CSTOPB ;options.c_cflag &= ~CSIZE ;options.c_cflag |= CS8 ; /* 控制模式标志*/options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ; /* 本地模式 */options.c_oflag &= ~OPOST ; /* 输出模式 */options.c_cc [VMIN] = 0 ;options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)tcsetattr (fd, TCSANOW, &options) ;ioctl (fd, TIOCMGET, &status);status |= TIOCM_DTR ;status |= TIOCM_RTS ;ioctl (fd, TIOCMSET, &status);usleep (10000) ; // 10mSreturn fd ;
}void serialSendstring (const int fd, const char *s)
{int ret;ret = write (fd, s, strlen (s));if (ret < 0)printf("Serial Puts Error\n");
}int serialGetstring (const int fd,char *buffer)
{int n_read;n_read=read(fd,buffer,32);return n_read;
}
uartTest.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "uartTool.h"int fd;void *readSerial()
{char buffer[32];while(1){memset(buffer,'\0',sizeof(buffer));serialGetstring(fd,buffer);printf("GET->%s\n",buffer);}
}void *sendSerial()
{char buffer[32];while(1){memset(buffer,'\0',sizeof(buffer));scanf("%s",buffer);serialSendstring(fd,buffer);}
}int main(int agrc,char **argv)
{pthread_t readt;pthread_t sendt;char deviceName[32]={'\0'};if(agrc < 2){printf("uage:%s /dev/ttyS?\n",argv[0]);return -1;}strcpy(deviceName,argv[1]);if((fd=myserialOpen(deviceName,115200))== 1){printf("open %s error!",deviceName);return -1;}pthread_create(&readt,NULL,readSerial,NULL);pthread_create(&sendt,NULL,sendSerial,NULL);while(1){sleep(10);}}