Discussion:
Strange behaviuor for asio::write()
(too old to reply)
a.villa
2017-05-03 10:37:40 UTC
Permalink
Hello everyone,

I'm setting up a connection on a serial rs-232 on top of USB with some
embedded device.

For this purpose I wrote a small c++ program to connect and send a
simple command to the device and read back the response built with
visual studio 2015 and linking boost asio version 1_61:

#include <boost/asio.hpp>
#include <iostream>

using namespace::boost::asio;


// windows uses com ports, this depends on what com port your cable is
plugged in to.
const char *PORT = "COM4";
// Note: all the following except BAUD are the exact same as the default
values

// what baud rate do we communicate at
serial_port_base::baud_rate BAUD(9600);
// how big is each "packet" of data (default is 8 bits)
serial_port_base::character_size CSIZE(8);
// what flow control is used (default is none)
serial_port_base::flow_control FLOW(serial_port_base::flow_control::none);
// what parity is used (default is none)
serial_port_base::parity PARITY(serial_port_base::parity::none);
// how many stop bits are used (default is one)
serial_port_base::stop_bits STOP(serial_port_base::stop_bits::one);

int main()
{
// create the I/O service that talks to the serial device
io_service io;
// create the serial device, note it takes the io service and the
port name
serial_port port(io, PORT);

// go through and set all the options as we need them
// all of them are listed, but the default values work for most cases
port.set_option(BAUD);
port.set_option(CSIZE);
port.set_option(FLOW);
port.set_option(PARITY);
port.set_option(STOP);

// buffer to store commands
// this device reads 8 bits, meaning an unsigned char, as instructions
// varies with the device, check the manual first
unsigned char command[3] = {0x0, 0x97, 0x0};
unsigned char rec_buf[256];

// this is the command that sends the actual bits over the wire
// note it takes a stream and a asio::buffer
// the stream is our serial_port
// the buffer is constructed using our command buffer and
// the number of instructions to send

printf("a\n");
write(port, buffer(command, 3));
printf("b\n");
read(port, buffer(rec_buf, 8));
printf("c\n");
for (int i = 0; i < 8; i++) {
printf("0x%x - %d\n", rec_buf[i], rec_buf[i]);
}

getchar();

// all done sending commands
return 0;
}

The problem is the following:

If I call the exe from the command prompt it hangs on the write()
function. For what I can understand looking at the write() code, the
number of bytes transferred is always zero, so the function never
returns, which is perfectly fine according to write() docs.
The problem is that if I run the same program through visual studio
debugger (both debug and release), it never hangs and returns the
correct response from the device.
I also tried to run the program from explorer and from powershell, and
it always runs fine without hanging on the write() function.

It seems like an environment issue, does anybody know of some
environment setting I'm not aware that I'm missing?

thank you

A. Villa
--
Alberto Villa
Software Engineer

Via Lavoratori Autobianchi, 1
20832 Desio
Italy


---
Questa e-mail è stata controllata per individuare virus con Avast antivirus.
https://www.avast.com/antivirus


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...