Linux下libserial库对接串口传感器的实例一

源码地址:
https://download.csdn.net/download/zlyadvocate/12562897
一、 定长数据传感器处理例子

1.接收定长包,检验CHECKSUM计算和数据解析。
数据包包头为0x55,定长为11,checksum为累加和(不包含包头0x55).
容错处理需要注意的地方:
 接受时不能确认接受到包头、完整数据的(校验和错误),一般都直接丢弃。
 接受预定非法字符串,如不是ASCII码等需要丢弃(ASCII码数据).
2.串口初始化
std::string p = “/dev/ttyUSB0”;
TYYS_INFO(“open OBD port”);
try
{
// Open the Serial Port at the desired hardware port.
serial_port.Open§ ;
}
catch (const OpenFailed&)
{

//    std::cerr << "The serial port did not open correctly." << std::endl ;
  //  return EXIT_FAILURE ;
}

// Set the baud rate of the serial port.设置波特率
serial_port.SetBaudRate(BaudRate::BAUD_19200) ;

// Set the number of data bits.
serial_port.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;

// Turn off hardware flow control.
serial_port.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;

// Disable parity.
serial_port.SetParity(Parity::PARITY_NONE) ;

// Set the number of stop bits.
serial_port.SetStopBits(StopBits::STOP_BITS_1) ;
serial_port.FlushIOBuffers();
serial_port.Write("obd\n");

// Wait for data to be available at the serial port.
//ile(!serial_port.IsDataAvailable())
{
    usleep(1000) ;
}

pthread_t nRet = pthread_create(&_nPid, 0, run2, this);
if(nRet != 0){
    perror("SerialPortManager pthread_create failed!\n");
}

nRet = pthread_create(&_nPid1, 0, run1, this);
if(nRet != 0){
    perror("SerialPortManager pthread_create failed!\n");
}

3.处理流程
void G_Sensor_manager::run()
{
std::string accrbuff;
int ret;
search_header();

/*优化方案:此处将串口每一句作为一个节点保存到链表中,另一个线程去以此取节点解析*/

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); //允许退出线程
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //设置立即取消
    while(1){
      serial_port.Read(read_buffer, 11) ;
       // std::cout <

}

a) 读取定长的数据(数据输出为定长)
b) 首先寻找包头。
实例:包头为0x55,定长为11
void G_Sensor_manager::search_header(){

for(int i=0;i<11;i++)
{
// Read a single byte of data from the serial port.
serial_port.ReadByte(data_byte, 0) ;

    // Show the user what is being read from the serial port.
    std::cout << data_byte << std::flush ;
    if(data_byte==0x55)
    break;
 }
 serial_port.Read(read_buffer, 10) ;

}

c) 计算checksum
bool G_Sensor_manager::accr_checksum()
{
unsigned char m_checksum=0;
for(int i=0;i<10;i++)
m_checksum=read_buffer.at(i)+m_checksum;

if(m_checksum==read_buffer.at(10))
    return true;
else
    return false;

}
d) 解析数据
void G_Sensor_manager::decodeaccr()
{
switch(read_buffer.at(1))
{

     case 0x51:	stc_Acc.a[0] = ((unsigned short)read_buffer.at(3)<<8)|read_buffer.at(2);
                stc_Acc.a[1] = ((unsigned short)read_buffer.at(5)<<8)|read_buffer.at(4);
                stc_Acc.a[2] = ((unsigned short)read_buffer.at(7)<<8)|read_buffer.at(6);
                accr_cx=(double)stc_Acc.a[0]/32768*16;//g
                accr_cy=(double)stc_Acc.a[1]/32768*16;//g
                accr_cz=(double)stc_Acc.a[2]/32768*16;//g
                std::cout <<"acc x y z "<

}
源码地址:
https://download.csdn.net/download/zlyadvocate/12562897

你可能感兴趣的:(Linux下libserial库对接串口传感器的实例一)