简体   繁体   English

Linux串行端口:输入中缺少数据

[英]Linux Serial Port: missing data on input

    #include <fcntl.h>   
    #include <termios.h> 
    #include <unistd.h>  
    #include <errno.h>   
    #include <cerrno>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <cstdlib>
    #include <ctime>

    using namespace std;
    typedef struct termios ComSet;

    int main()
    {
    int acm=-1;                                   
    acm=open("/dev/ttyACM0",O_RDWR | O_NOCTTY);
    if(acm == -1)
    {
    cout<<"Error Opening ttyACM0"<<endl;
    exit(1);
    }
    else
    {
    cout<<"Preparing ttyACM0..."<<endl;
    ComSet SerialSettings;
    tcgetattr(acm, &SerialSettings);
    cfsetispeed(&SerialSettings,B9600);
    cfsetospeed(&SerialSettings,B9600);

 /* 8N1 Mode */
    SerialSettings.c_cflag &= ~PARENB;   
    SerialSettings.c_cflag &= ~CSTOPB;   
    SerialSettings.c_cflag &= ~CSIZE;   
    SerialSettings.c_cflag |=  CS8;     

    SerialSettings.c_cflag &= ~CRTSCTS;       
    SerialSettings.c_cflag |= CREAD | CLOCAL; 


    SerialSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          
    SerialSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);  
    SerialSettings.c_oflag &= ~OPOST;                           

 /* Setting Time outs */
    SerialSettings.c_cc[VMIN]  = 10;     /* Read at least 10 characters */
    SerialSettings.c_cc[VTIME] = 0;     /* Wait indefinetly   */

    if((tcsetattr(acm,TCSANOW,&SerialSettings)) != 0)   
    {
        cout<< " ERROR ! in Setting attributes"<<endl;

    }
    else
    {
        cout<< "=======ttyACM0 Setting====="<<endl;
        cout<<"BaudRate = 9600 StopBits = 1 Parity = none"<<endl;
        cout<<"Reading ttyACM0... "<<endl;
        char read_buffer[1024];                                
        bzero(read_buffer,1024);
        int read_bytes=0;                                      
        while(1)
         {
                tcflush(acm, TCIFLUSH);                        
                read_bytes=read(acm,&read_buffer,1024);        
                if(read_bytes>0)
                {
                    cout<<read_buffer;
                    bzero(read_buffer,1024);
                }

         }
    }



  close(acm); /* Close the serial port */

}

return 0;
}

for some reason code its not working properly im trying to read from an arduino as it was working properly earlier its giving me grabled output such as 由于某种原因,它无法正常工作,因为我试图从arduino读取,因为它可以正常工作,这给了我诸如此类的输出

lo World ! 罗世界! Hello World ! 你好,世界 ! ello World ! 你好世界! llo World ! llo世界! llo World ! llo世界!

one thing that changed was 改变的一件事是

there is one extra new line in terminal output this is using cat /dev/ttyAMC0 终端输出中有一条额外的新行,这是使用cat / dev / ttyAMC0

Hello World ! 你好,世界 !

Hello World ! 你好,世界 !

Hello World ! 你好,世界 !

Hello World ! 你好,世界 !

Hello World ! 你好,世界 !

i want to read line by line and as it was sent by the arduino to terminal 我想逐行阅读,因为它是由arduino发送到终端的

When you get garbled output like lo World ! Hello World ! ello World ! 当您得到lo World ! Hello World ! ello World !类的乱码输出时lo World ! Hello World ! ello World ! lo World ! Hello World ! ello World ! then that usually indicates that your clock speed (baud rate) isn't matched. 那么通常表明您的时钟速度(波特率)不匹配。 Make sure your Arduino is writing at a speed of 9600 baud. 确保您的Arduino的写入速度为9600波特。

Additionally, when the Arduino outputs characters, it puts them in a hardware buffer. 此外,当Arduino输出字符时,会将它们放在硬件缓冲区中。 The read() function takes the number of bytes you specify out of that buffer- in this case 1024. But you probably don't want to read 1024 bytes every time. read()函数从缓冲区中取出您指定的字节数-在这种情况下为1024。但是您可能不想每次都读取1024字节。 Instead, I think you should read until you reach a newline character. 相反,我认为您应该阅读直到找到换行符为止。 See if there is a readline() function in the library you are using. 查看您所使用的库中是否有readline()函数。

The Linux cat command is reading correctly, but the command itself adds an extra newline at the end. Linux cat命令可以正确读取,但是该命令本身在末尾添加了额外的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM