使用C / C ++和LibSerial在Ubuntu中读取和写入串行端口 [英] Read and Write on serial port in Ubuntu with C/C++ and LibSerial

查看:1748
本文介绍了使用C / C ++和LibSerial在Ubuntu中读取和写入串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上使用 LibSerial 在串行端口上读取和写入数据。



此时,我可以通过串口写入和接收字符串,但是我的代码不能很好地工作:我想控制读取功能,以便在没有信息读取以便发送另一个命令时,只读取有读取和退出的信息。



我想要:




  • 写一个命令

  • 等待答案

  • 然后写另一个命令

  • 等待回答



<现在,我能够发送第一个命令,并通过使用read函数在while循环中读取答案,但我不能做任何其他事情。
我无法发送第二个命令,因为while循环从不退出,因此程序会继续读取





这是我使用的代码:
(读写功能在代码的末尾)

  #include< SerialStream.h> 
#include< iostream>
#include< unistd.h>
#include< cstdlib>
#include< string>

int
main(int argc,
char ** argv)
{
//
//打开串口。
//
using namespace std;
使用命名空间LibSerial;
SerialStream serial_port;
char c;
serial_port.Open(/ dev / ttyACM0);
if(!serial_port.good())
{
std :: cerr<< [< __FILE__<< :<< __LINE__<< ]
<< 错误:无法打开串行端口。
<< std :: endl;
exit(1);
}
//
//设置串口的波特率。
//
serial_port.SetBaudRate(SerialStreamBuf :: BAUD_9600);
if(!serial_port.good())
{
std :: cerr<< 错误:无法设置波特率。 <<
std :: endl;
exit(1);
}
//
//设置数据位数。
//
serial_port.SetCharSize(SerialStreamBuf :: CHAR_SIZE_8);
if(!serial_port.good())
{
std :: cerr<< 错误:无法设置字符大小。 <<
std :: endl;
exit(1);
}
//
//禁用奇偶校验。
//
serial_port.SetParity(SerialStreamBuf :: PARITY_NONE);
if(!serial_port.good())
{
std :: cerr<< 错误:无法禁用奇偶校验。 <<
std :: endl;
exit(1);
}
//
//设置停止位数。
//
serial_port.SetNumOfStopBits(1);
if(!serial_port.good())
{
std :: cerr<< 错误:无法设置停止位数。
<< std :: endl;
exit(1);
}
//
//关闭硬件流控制。
//
serial_port.SetFlowControl(SerialStreamBuf :: FLOW_CONTROL_NONE);
if(!serial_port.good())
{
std :: cerr<< 错误:无法使用硬件流控制。
<< std :: endl;
exit(1);
}
//
//从
//串口读取时不要跳过空格字符。
//
// serial_port.unsetf(std :: ios_base :: skipws);
//
//等待一些数据在串行端口可用。
//
//
//从串口读取数据并将其打印到屏幕上。
//
//等待一些数据在串行端口可用。
//
while(serial_port.rdbuf() - > in_avail()== 0)
{
usleep(100);
}


char out_buf [] =check;
serial_port.write(out_buf,5); < - FIRST COMMAND
while(1)
{
char next_byte;
serial_port.get(next_byte);这里我接收第一个答案
std :: cerr<<下一页

}
std :: cerr<< std :: endl;
return EXIT_SUCCESS;
}


解决方案

我想你只需要使用 while(serial_port.rdbuf() - > in_avail()> 0)作为最后一个while循环的条件。然后它会读出所有可用的数据(answer),然后您可以发送第二个命令。


I'm using LibSerial on Ubuntu to read and write data on serial port.

At the moment, I'm able to write and receive strings over the serial port, but my code does not work very well: in particular, I'd like to control the reading function in order to read only if there is something to read and exit when there is no information to read in order to send another command without bloicking the flow program.

I want to do:

  • Write a command
  • Wait for the answer
  • then Write another command
  • Wait for answer

Now, i'm able to send the first command and read the answer by using read function in a while loop but i'm not able to do nothing else. I'm not able to send the second command because the while loop never exits so the program continues to read.

Can you help me, please?

This is the code i'm using: (Read and write function are at the end of the code)

#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string>

int
main( int    argc,
       char** argv  )
{
     //
     // Open the serial port.
     //
     using namespace std;
     using namespace LibSerial ;
     SerialStream serial_port ;
     char c;
     serial_port.Open( "/dev/ttyACM0" ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                   << "Error: Could not open serial port."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Set the baud rate of the serial port.
     //
     serial_port.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the baud rate." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of data bits.
     //
     serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the character size." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Disable parity.
     //
     serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not disable the parity." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of stop bits.
     //
     serial_port.SetNumOfStopBits( 1 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the number of stop bits."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Turn off hardware flow control.
     //
     serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not use hardware flow control."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Do not skip whitespace characters while reading from the
     // serial port.
     //
     // serial_port.unsetf( std::ios_base::skipws ) ;
     //
     // Wait for some data to be available at the serial port.
     //
     //
     // Keep reading data from serial port and print it to the screen.
     //
  // Wait for some data to be available at the serial port.
     //
     while( serial_port.rdbuf()->in_avail() == 0 )
     {
         usleep(100) ;
     }


     char out_buf[] = "check";
     serial_port.write(out_buf, 5);  <-- FIRST COMMAND
     while( 1  )
     {
         char next_byte;
         serial_port.get(next_byte);  HERE I RECEIVE THE FIRST ANSWER
         std::cerr << next_byte;

     }
     std::cerr << std::endl ;
     return EXIT_SUCCESS ;
}

解决方案

I think you just need to use while( serial_port.rdbuf()->in_avail() > 0 ) as a condition for your last while loop. Then it'll read out all the available data ("answer") and you can send the second command after that.

这篇关于使用C / C ++和LibSerial在Ubuntu中读取和写入串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆