使用C/C++和LibSerial在Ubuntu的串行端口上读写
Read and Write on serial port in Ubuntu with C/C++ and LibSerial
我在Ubuntu上使用LibSerial在串行端口上读写数据。
目前,我可以通过串行端口写入和接收字符串,但我的代码工作不太好:特别是,我想控制读取功能,以便只有在有要读取的内容时才读取,并在没有要读取的信息时退出,以便发送另一个命令而不会阻塞流程序。
我想做:
- 编写命令
- 等待答案
- 然后编写另一个命令
- 等待答案
现在,我可以通过在while循环中使用read函数发送第一个命令并读取答案,但我不能做其他任何事情。我无法发送第二个命令,因为while循环永远不会退出,所以程序将继续读取
你能帮我吗?
这是我正在使用的代码:(读取和写入功能位于代码末尾)
#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 ;
}
我认为您只需要使用while( serial_port.rdbuf()->in_avail() > 0 )
作为最后一个while循环的条件。然后它会读出所有可用的数据("答案"),然后你可以发送第二个命令。
使用try-and-catch会很有帮助。:)
试试这个:全局指针SerialPort*pu已经声明,并且该端口已打开。
int rxstring(char *cstr, unsigned int len, bool print_str)
{
char temp=0;
int i=0;
while(temp!='n')
{
try
{
temp=pu->ReadByte(100);
}
catch(SerialPort::ReadTimeout &e)
{
//cout<<"Read Timeout"<<endl;
return 1;
}
if((temp!='n')&&(temp!=0)&&(temp!=' '))
{
cstr[i]=temp;
++i;
//cout<<i++<<temp<<'x'<<endl;
}
}
cstr[i]=' ';
if(print_str)
puts(cstr);
return 0;
}
参考:http://libserial.sourceforge.net/doxygen/class_serial_port.html#a15
相关文章:
- Qt Q串行端口未编程设备未关闭
- 在 linux c++ 中没有通过串行端口发送的数据
- 检查串行端口Linux中是否有传入数据(cbInQue for linux)
- 在不传输的情况下更改 Win32 中的串行端口波特率
- 增强 ASIO 和串行端口异步读取
- 串行端口写入究竟如何从缓冲区实际写入数据?
- 如何将参数这些串行端口传递到函数中
- 从 C/C++ 访问 Android 中的串行端口
- Qt两个串行端口发送和接收
- 通过串行端口从GPS传感器读取
- 提升 asio 串行端口"end of file"
- 通过串行端口发送多个值
- 与QSerialPort配合使用的串行端口仿真
- 如何通过boost::asio和shared_ptr创建串行端口
- 串行端口编码
- 使用来自串行端口的字符串数据来操作振镜扫描仪在更高的速度下会出错
- 写入和读取十六进制 TRAM QT 串行端口
- Qt串行端口数据接收
- Qt modbus 串行端口流量控制处理
- 使用C/C++和LibSerial在Ubuntu的串行端口上读写