I am having an issue with the following code:

SerialPort^ com = gcnew SerialPort("COM1", 9600);
array<unsigned char>^ messageBuf = gcnew array<unsigned char>(512);

com->Open();
com->Read(messageBuf, 0, 512);
com->Close();

messageBuf is not being filled with anything. I know the port is open cause writing works fine. Any ideas why this is?

Recommended Answers

All 5 Replies

hi,
maybe reading from serial port needs special configuration first, as for example:

port->DataBits = 8;
port->Parity = Parity::None;
port->StopBits = StopBits::One;

krs,
tesu

I've tried that and it didnt fix it, but thanks anyways. Im just so confused, ive tried ReadExisting(), I dont know how to get this to work.

messageBuf is not being filled with anything. I know the port is open cause writing works fine. Any ideas why this is?

What do you have attached to the COM1 port that will transmit data to your computer ?

I have a usb (with serial emulated through driver) to serial cable with a null rs232 cable attached. I just decided to read in one char at a time using an index (using com->ReadChar()) and it works. It still annoys me that this does not work, because I think it should, if anyone knows why. Thanks.

There is something to keep in mind when using null modem wiring, see http://www.lammertbies.nl/comm/info/RS-232_null_modem.html
Instead of checking the serial port with c++/cli program (maybe managed code cause problems) I would check it with serial port checker, for example using Portmon of Mark Russinovich or free serial port monitor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.