Boost asio串行端口最初发送错误数据 [英] Boost asio serial port initially sending bad data

查看:67
本文介绍了Boost asio串行端口最初发送错误数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用boost asio进行串行通信.我目前在Windows中工作,但最终会将代码移到Linux中.每当我重新启动计算机时,从程序发送的数据都不应该是应该的(例如,我发送一个空值,然后返回一个回车,并以二进制形式获取"00111111 10000011"),并且不一致(多个空值会产生不同的二进制值).

I am attempting to use boost asio for serial communication. I am currently working in Windows, but will eventually be moving the code into Linux. When ever I restart my computer data sent from the program is not what it should be (for example I send a null followed by a carriage return and get "00111111 10000011" in binary) and it is not consistent (multiple nulls yield different binary).

但是,一旦我使用任何其他程序将任何数据发送到串行端口并再次运行该程序,它就会完美运行.我想我在端口的初始化中肯定会丢失一些东西,但是我的研究没有发现任何东西.

However, as soon as I use any other program to send any data to the serial port and run the program again it works perfectly. I think I must be missing something in the initialization of the port, but my research has not turned anything up.

这是我打开端口的方式:

Here is how I am opening the port:

// An IOService to get the socket to work
boost::asio::io_service *io;
// An acceptor for getting connections
boost::shared_ptr<boost::asio::serial_port> port;

// Cnstructor Functions
void Defaults() {
    io = new boost::asio::io_service();

    // Set Default Commands
    command.prefix = 170;
    command.address = 3;
    command.xDot[0] = 128;
    command.xDot[1] = 128;
    command.xDot[2] = 128;
    command.throtle = 0;
    command.button8 = 0;
    command.button16 = 0;
    command.checkSum = 131;
}

void Defaults(char * port, int baud) {
    Defaults();

    // Setup the serial port
    port.reset(new boost::asio::serial_port(*io,port));
    port->set_option( boost::asio::serial_port_base::baud_rate( baud ));

    // This is for testing
    printf("portTest: %i\n",(int)port->is_open());
    port->write_some(boost::asio::buffer((void*)"\0", 1));
    boost::asio::write(*port, boost::asio::buffer((void*)"\0", 1));
    boost::asio::write(*port, boost::asio::buffer((void*)"\r", 1));
    boost::asio::write(*port, boost::asio::buffer((void*)"\r", 1));
    Sleep(2000);
}

:为了删除不相关的代码,我不小心删除了设置波特率的行,将其重新添加回去.另外,我正在使用null调制解调器和Docklight检查输出.除了波特率之外,我还使用了为升压串行端口指定的所有默认串行设置(我也尝试过显式设置它们,但不起作用).

In an attempt to remove unrelated code I accidentally deleted the the line where I set the baud rate, I added it back. Also, I am checking the output with a null-modem and Docklight. Aside from the baud rate I am using all of the default serial settings specified for a boost serial port (I have also tried explicitly setting them with no effect).

推荐答案

这是寻找此问题时搜索结果的顶部,所以我想我会给出我认为是正确的答案:

This is the top search result when looking for this problem, so I thought I'd give what I believe is the correct answer:

在撰写本文时,Boost Asio发生了错误,并且未在Windows平台上为端口设置默认值.取而代之的是,它将获取端口上的当前值,然后将其重新烘焙.重新启动后,该端口尚未使用,因此其Windows默认值可能对通信没有用(字节大小通常默认为9).将端口与正确设置了值的程序或库一起使用后,Asio之后获取的值是正确的.

Boost Asio is bugged as of this writing and does not set default values for ports on the Windows platform. Instead, it grabs the current values on the port and then bakes them right back in. After a fresh reboot, the port hasn't been used yet, so its Windows-default values are likely not useful for communication (byte size typically defaults to 9, for example). After you use the port with a program or library that sets the values correctly, the values Asio picks up afterward are correct.

要解决此问题,直到Asio合并了此修复程序,只需在端口上显式设置所有内容,即:

To solve this until Asio incorporates the fix, just set everything on the port explicitly, ie:

using boost::asio::serial_port;
using boost::asio::serial_port_base;

void setup_port(serial_port &serial, unsigned int baud)
{
    serial.set_option(serial_port_base::baud_rate(baud));
    serial.set_option(serial_port_base::character_size(8));
    serial.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
    serial.set_option(serial_port_base::parity(serial_port_base::parity::none));
    serial.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
}

这篇关于Boost asio串行端口最初发送错误数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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