本文介绍了Linux串行端口:输入缺少数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #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;

/ *设置超时* /
SerialSettings.c_cc [VMIN] = 10; / *读取至少10个字符* /
SerialSettings.c_cc [VTIME] = 0; / *无限等待* /

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); / *关闭串口* /

}

return 0;
}

因为某些原因代码它不能正常工作从一个arduino读它工作正常早了它给我grabled输出



lo世界!
Hello World!
ello世界!
llo世界!
llo World!



更改的一件事是



输出此为使用cat / dev / ttyAMC0



Hello World!



Hello World! b
$ b

Hello World!



Hello World!



Hello World! / p>

我想逐行阅读,因为它是由arduino发送到终端

解决方案

当你得到乱码输出像 lo World!你好,世界 ! ello World!那么通常表示你的时钟速度(波特率)不匹配。确保您的Arduino以9600波特的速度写入。



此外,当Arduino输出字符时,它将它们放在硬件缓冲区中。 read()函数接收您从该缓冲区指定的字节数 - 在这种情况下为1024.但是您可能不想每次读取1024字节。相反,我认为你应该阅读,直到你达到换行字符。查看您使用的库中是否有 readline()函数。



Linux cat 命令正确读取,但命令本身在末尾添加了一个换行符。


    #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 i m trying to read from an arduino as it was working properly earlier its giving me grabled outputsuch as

lo World !Hello World !ello World !llo World !llo World !

one thing that changed was

there is one extra new line in terminal output this is using 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

解决方案

When you get garbled output like 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.

Additionally, when the Arduino outputs characters, it puts them in a hardware buffer. 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. 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.

The Linux cat command is reading correctly, but the command itself adds an extra newline at the end.

这篇关于Linux串行端口:输入缺少数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:33