新入手的开发板:AIO-3568J 测试RS232串口通信: 1:开发板默认系统为Android,在官方网站Wiki.t 下载Ubuntu 固件刷机 版本18.04 AIO-3568J-UBUNTU18.04-GPT-20220222-1626.img 2: 开发板自带两路RS232 对应串口号 ttyS3 ttyS4 3: 上位机使用串口助手发送字符串,开发板使用 echo 指令发送和接收字符 测试发送: echo "firefly RS232 test..." > /dev/ttyS3 上位机收到的是乱码 测试接收:cat /dev/ttyS3 然后上位机发送“firefly ” 开发板能正常收到4:上位机发送接收十六进制数,开发板使用自行编译的串口测试代码以下为测试程序代码=======================================================#include <stdio.h> #include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h> #include <fcntl.h> #include <termios.h>#include <errno.h> #include <pthread.h> #include <sys/ioctl.h> #define FALSE 1#define TRUE 0//#include <stdio.h>//#include <stdlib.h>//malloc//#include <sys/types.h>//open//#include <sys/stat.h>//#include <fcntl.h>//#include <unistd.h>//close/read#include <linux/input.h>//struct input_event//EV_ABS...#include <strings.h>//bzero//#include <sys/ioctl.h>//#include <errno.h>int speed_arr[] = {B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200};int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200};void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr); cfsetospeed(&Opt, speed_arr); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); }}int set_Parity(int fd,int databits,int stopbits,int parity){ struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); } options.c_cflag &= ~CSIZE; switch (databits) { 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_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); options.c_oflag &= ~OPOST; options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; options.c_cc[VTIME] = 150; // 15 seconds options.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return (FALSE); } return (TRUE);}void receivethread_1(void){ int nread,i=0; char buff_tx_1[20]; //TX:55 AA 01 02 buff_tx_1[0] = 0x55; buff_tx_1[1] = 0xAA; buff_tx_1[2] = 0x01; buff_tx_1[3] = 0x02; char buff_1[512]; while(1) { write(fd_1, buff_tx_1, 4); usleep(2000); if((nread = read(fd_1,buff_1,100))>0) //接收数据 { printf("[RECEIVE] Len is %d,content is :\n[Hex]",nread); //buff[nread]='\0'; //printf("%s\n",buff); for (i = 0;i < nread; i++) { printf("%02x ", buff_1); } printf("\n"); } usleep(1000); } return;}int main(int argc, char *argv[]){ pthread_t receive_1; char port_1[20] = "/dev/ttyS3"; fd_1 = open(port_1, O_RDWR); if (fd_1 < 0) { printf("open device %s faild\n",port_1); exit(0); } set_speed(fd_1,115200); set_Parity(fd_1,8,1,'N'); pthread_create(&receive_1,NULL,(void*)receivethread_1,NULL); while(1) { printf("Please Input string to send to %s\n:",port_1); scanf("%s", str); if(strlen(str) > 0) { write(fd_1, str, strlen(str)); write(fd_1, "\n", strlen("\n")); usleep(200*1000); } } close(fd_1); exit(0);}===========================================================上位机到开发板正常,开发板到上位机乱数当设置为开发板到上位机发一个字节,55 上位机接收显示正确 55当设置为开发板到上位机发两个字节,55 AA 上位机接收显示错误为: 55 15使用示波器查看波形,发现位数不全,发送一个字节,为1个起始位,8个数据位,1个停止位,共10 位,而发送两个字节时,数波形,不够20 |