I am trying to send commands and receive data from a hardware device via a serial port in Windows.
The documentation has the following information:
There is also an example command given in the documentation, which looks like this:
Client: $02 $30 $34 $30 $31 $30 $33 $32 $41 $03
which should result in the returned data:
$02 $30 $34 $30 $35 $30 $33 $30 $32 $33 $30 $32 $37 $41 $36 $44 $33 $03
When I send the command in full $02 $30 $34 $30 $31 $30 $33 $32 $41 $03 as Hex, in a serial port monitor application, I indeed get the data return. In my c++ application however, I get nothing.
I am using boost::asio in c++, and have the following code:
int main(int argc, char* argv[])
{
asio::io_service io;
asio::serial_port port(io);
port.open("COM4");
port.set_option(asio::serial_port_base::baud_rate(115200));
uint8_t obuf[512]; // arbitrary size, larger than largest expected buffer
for (size_t i = 0; i < 512; ++i) obuf[i] = 0;
while (true)
{
port.write_some(boost::asio::buffer(char(0x02) + "0401032A" + char(0x03), 10));
//$02 $30 $34 $30 $31 $30 $33 $32 $41 $03
std::cout << "reading" << std::endl;
asio::read(port, asio::buffer(obuf,10));
std::cout << obuf << std::endl;
}
std::cin.get();
}
Where am I going wrong in the formatting here? How can I send this command '$02 $30 $34 $30 $31 $30 $33 $32 $41 $03' as ASCI to a port in c++?
