2

我需要将此代码更改为 cpp 代码

这是c#代码

    public void SendDataViaSerialPort(string data)
{
    var com = new System.IO.Ports.SerialPort(
                "COM29", 9600, System.IO.Ports.Parity.None, 8, 
                                 System.IO.Ports.StopBits.One);
            com.Open();
            com.Write(data);
            com.Close();
}
4

1 回答 1

1

使用 boost::asio

#include <string>
#include <boost/asio.hpp>
using namespace::boost::asio;

serial_port_base::baud_rate BAUD(9600);
serial_port_base::parity PARITY(serial_port_base::parity::none);
serial_port_base::stop_bits STOP(serial_port_base::stop_bits::one);

void SendDataViaSerialPort(const std::string& to_write)
{
        io_service io;
        serial_port port(io, "COM29");

        port.set_option(BAUD);
        port.set_option(PARITY);
        port.set_option(STOP);

        write(port, buffer(to_write,1));
}
于 2012-03-29T00:29:34.407 回答