2

I need to change this code to cpp code

This is the c# code

    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();
}
aakpro
  • 1,538
  • 2
  • 19
  • 53
  • You forgot to ask a question. What difficulty are you having porting the code? (The documentation is [here](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx#Y0) by the way.) – David Schwartz Mar 28 '12 at 23:53
  • Will it be running on Windows afterward? – wallyk Mar 28 '12 at 23:54

1 Answers1

1

Using 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));
}
Sergei Nikulov
  • 5,029
  • 23
  • 36