相关地址
该库可以通过包管理工具(apt, yum)安装
sudo apt intstall libserial-dev
但进行交叉编译必须git源代码,或者通过apt解包下载头文件与库文件
serialTest.cc
/*
read 串口读取函数用法
参数:
std::string data 接受数据,
size_t NumberOfByte 需要接受的字节数,
size_t Timeout 接收超时
当NumberOfByte为0,而Timeout不为0时时,接受数据取决于Timeout
这种情况下,需要用try&catch,并输出data内容,否则程序会因为read报Timeout而停止
当Timeout为0,而NumberOfByte不为0时,将一直等待数据接收字节数直到为NumberOfByte停止并返回
当都为0时,立即返回,啥都没有,啥都不是
readLine 唯一的不同之处就是 其第二个参数为停止字符(默认为换行符)
*/
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <future>
#include <condition_variable>
#include "include/libserial/include/SerialPort.h"
#include "include/libserial/include/SerialPortConstants.h"
namespace {
const ::std::string SERIAL_OUT_PORT = "/dev/ttyS8";
const ::std::string SERIAL_IN_PORT = "/dev/ttyS6";
bool ready2read = false;
bool readComplete = false;
std::mutex lock;
std::condition_variable serialRead;
}
namespace serial = LibSerial;
std::string receiveFromSerial(serial::SerialPort& s){
std::unique_lock m_lock(lock);
size_t timeout = 20;
std::string receiveMesg;
receiveMesg.reserve(100);
serialRead.wait(m_lock, []{ return ready2read;});
ready2read = false;
try
{
// s.Read(receiveMesg, receiveMesg.size(), timeout);
s.ReadLine(receiveMesg,'\n', timeout);
}
catch(const serial::ReadTimeout&)
{
if (receiveMesg.size()==0)
std::cerr << "The Read() call has timed out." << '\n';
}
std::cout<<receiveMesg<<std::endl;
readComplete = true;
serialRead.notify_one();
return receiveMesg;
}
int main(){
serial::SerialPort S8;
serial::SerialPort S6;
try
{
S8.Open(SERIAL_OUT_PORT);
S6.Open(SERIAL_IN_PORT);
}
catch(const serial::OpenFailed&)
{
std::cerr << "The serial ports did not open correctly." << std::endl ;
std::cerr << "---------- Try sudo mode --------------" << std::endl ;
return EXIT_FAILURE ;
}
S8.SetBaudRate(serial::BaudRate::BAUD_1000000);
S8.SetCharacterSize(serial::CharacterSize::CHAR_SIZE_8);
S8.SetFlowControl(serial::FlowControl::FLOW_CONTROL_NONE);
S8.SetParity(serial::Parity::PARITY_NONE);
S8.SetStopBits(serial::StopBits::STOP_BITS_1);
S6.SetBaudRate(serial::BaudRate::BAUD_1000000);
S6.SetCharacterSize(serial::CharacterSize::CHAR_SIZE_8);
S6.SetFlowControl(serial::FlowControl::FLOW_CONTROL_NONE);
S6.SetParity(serial::Parity::PARITY_NONE);
S6.SetStopBits(serial::StopBits::STOP_BITS_1);
std::string sendMesg = "If time can not help, let the ocean do\n";
// std::future<std::string> Fu_receiveMesg = std::async(std::launch::async, receiveFromSerial, std::ref(S6));
// char a;
// std::cin>> a;
// while(a!='q'){
// std::cin>> a;
// }
// S8.Write(sendMesg);
// S8.DrainWriteBuffer();
while (true)
{
std::unique_lock serial_lock(lock);
std::thread t1(receiveFromSerial, std::ref(S6));
S8.Write(sendMesg);
ready2read = true;
serialRead.notify_one();
S8.DrainWriteBuffer();
serialRead.wait(serial_lock, []{ return readComplete;});
readComplete = false;
char a;
std::cin >> sendMesg;
std::getline(std::cin, sendMesg);
if ((sendMesg[0] == 'q') && (sendMesg.size() == 1))
{
t1.join();
break;
}
t1.join();
}
// S6.ReadByte(receiveByte, 100);
//std::string receiveMesg = Fu_receiveMesg.get();
//Fu_receiveMesg.wait();
// std::cout<<receiveMesg<<std::endl;
// std::cout<<receiveMesg.size()<<std::endl;
S8.Close();
S6.Close();
std::vector<int> a;
a.size
continue;
return 0;
}