开发者

SerialPort StopBits Windows7 C++/CLI

开发者 https://www.devze.com 2023-03-16 05:48 出处:网络
I\'ve been using the SerialPort class from C++/CLI (Windows::IO::Ports) to open a serial port and communicate with a periferic device, and everything seems to work. The port opens, and the data is sen

I've been using the SerialPort class from C++/CLI (Windows::IO::Ports) to open a serial port and communicate with a periferic device, and everything seems to work. The port opens, and the data is sent.. But i didn't get an answer from the periferic device.

I use a code like:

SerialPort^ _serialPort;
_serialPort = gcnew SerialPort();

//_serialPort = gcnew SerialPort("COM1",115200,Parity::None, 8, StopBits::One);

// Allow the user to set the appropriate proper开发者_Python百科ties.
_serialPort->PortName = "COM1";
_serialPort->BaudRate = 115200;

//Parity None by default
//_serialPort->Parity = Parity::None;

_serialPort->DataBits = 8;

_serialPort->StopBits = StopBits::One;

// Set the read/write timeouts
_serialPort->ReadTimeout = 1000;
_serialPort->WriteTimeout = 500;
_serialPort->Open();

Using a program to monitor the Serial Port communication, it seems that the number of Stop bits is 0. As you can see, i set the stop bits to 1, and even the strangest thing is that the default value is 1. I don't know why it keeps being 0. I have tried to change the baudrate and the databits, and it is changed, but there is no way with the StopBits...

It is really a pitty, because this seems really easier to use that other codes around the web.

Can anybody help me? Any ideas why it didn't work?


Can anybody tell me then, if this code is ok?

the array sent is: array<Byte>^ bytes = gcnew array<Byte>(32); (it is initialized with some values)

to send data i use: _serialPort->Write(bytes,0,32);

To read data i have tried:

_serialPort->Read(readarray,0,18);
_serialPort->ReadExisting();

And none of them get back any data... When i use the Serial Port Monitor to open the communication and send the same bytes, it answers...


The stop bits are configured by an enumerated value which is not equal to the number of stop bits.

Zero is the correct encoding for one stop bit. See http://msdn.microsoft.com/en-us/library/aa363214.aspx


Found it!! It seems that even setting the "Handshake" to "None", the DTR and RTS ports were set LOW after the data is sent. (i don't know why).

I set them manually to HIGH with:

_serialPort->RtsEnable = true;
_serialPort->DtrEnable =true;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号