Firefly开源社区

打印 上一主题 下一主题

[技术讨论] AIO-3568 测试RS232串口,上位机到开发板正常,开发板到上位机乱数

31

积分

0

威望

0

贡献

技术小白

积分
31

AIO-3568 测试RS232串口,上位机到开发板正常,开发板到上位机乱数

发表于 2022-8-22 11:25:16      浏览:2614 | 回复:3        打印      只看该作者   [复制链接] 楼主
  • 固件类型: 官方提供的固件
  • 固件文件名称: AIO-3568J-UBUNTU18.04-GPT-20220222-1626.img
  • 固件下载地址: 百度云盘
  • Log日志: rs232.txt
问题描述及复现步骤:
新入手的开发板: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

image.jpg (36.41 KB, 下载次数: 86)

image.jpg

rs232.txt

88 Bytes, 下载次数: 0, 下载积分: 灯泡 -1 , 经验 -1

回复

使用道具 举报

31

积分

0

威望

0

贡献

技术小白

积分
31
发表于 2022-8-22 11:42:51        只看该作者  沙发
自行编译的串口测试代码
以下为测试程序代码
=======================================================

#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[i])
                {
                        tcflush(fd, TCIOFLUSH);
                        cfsetispeed(&Opt, speed_arr[i]);
                        cfsetospeed(&Opt, speed_arr[i]);
                        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[i]);
                        }
                        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);
}
回复

使用道具 举报

2077

积分

10

威望

12

贡献

中级创客

Rank: 4

积分
2077
发表于 2022-8-22 16:56:03        只看该作者  板凳
我测试发送: echo "firefly RS232 test..." > /dev/ttyS3,上位机收到的是正常数据,没有问题
不过我的上位机是linux,没用串口助手,不过应该不影响
你这种情况可能是线路接触不良或者硬件故障
回复

使用道具 举报

31

积分

0

威望

0

贡献

技术小白

积分
31
发表于 2022-8-27 21:32:44        只看该作者  地板

串口测试,RS232接线是按照  GND、TX、RX ,如图下部红框,而实际上绿色端子的定义是上部红框内的,TX、GBD、RX

重新接线后,测试正常
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表