PLC to send data to x06 x0a (0 0 0 x00 0 x01 0 x00 x18 0 x01 0 0 xb1),
In the test, the following problems:
1. Use a serial port assistant can normally receive data from the PLC, but written in c # serial port function, always not receive data (registration method DataReceived never trigger events),
2. Try not to register method DataReceived events, convert loop active reading (corresponding to the following code Scan method), to be able to get to _serialPort. BytesToRead=8, but the data is read invalid data (0 x3f x3f x3f x3f x3f x3f 0 0 0 0 0 0 x3f 0 x3f),
In the BBS after checked some similar problems, try to, respectively, at the time of initialization will DTREnable and RTSEnable is set to True, but testing down doesn't work,
If a friend know or encounter this problem, please feel free to comment, your suggestion may help me to solve this problem,
The following for the relevant code:
//serial port initialization
Public void the Initialize ()
{
_serialPort. PortName="COM1";
_serialPort. BaudRate=19200;
_serialPort. DataBits=8;
_serialPort. StopBits=StopBits. Two;
_serialPort. Parity=Parity. Odd;
_serialPort. ReadTimeout=3000;
_serialPort. WriteTimeout=3000;
//try to respectively RtsEnable and DtrEnable set to True, but there is no effect
//_serialPort RtsEnable=true;
//_serialPort DtrEnable=true;
//_serialPort. Handshake=Handshake. None;
_serialPort. ReadBufferSize=8;
_serialPort. ReceivedBytesThreshold=1;
_serialPort. Method DataReceived -=CommDataReceived;
_serialPort. Method DataReceived +=CommDataReceived;
_serialPort. The Open ();
}
//data to monitor
Private void CommDataReceived (Object sender, SerialDataReceivedEventArgs e)
{
Int len=_serialPort. BytesToRead;
Byte [] readBuffer=new Byte (len);
_serialPort. Read (readBuffer, 0, len);
}
//active reading
Private void Scan ()
{
While (true)
{
Int len=_serialPort. BytesToRead;
//when active reading, can get to len of 8, but read the data as invalid data (0 x3f x3f x3f x3f x3f x3f 0 0 0 0 0 0 x3f 0 x3f)
If (len & gt;=8)
{
Byte [] readBuffer=new Byte (len);
_serialPort. Read (readBuffer, 0, len);
}
Thread.sleep (30);
}
}
CodePudding user response:
Add in the test, in the machine (PC) configure the virtual serial port (COM3 - COM4), through the virtual serial port, can be normal to receive data,CodePudding user response:
In this PC virtual serial port debugging can transport, can receive,With formal equipment for not, can look at the configuration of the baud rate, etc. These are right,
Because serial code is some configuration, and then open the serial port, there's no other,
CodePudding user response:
See, baud rate, stop bit is set correctlyWith 8 as a condition of decision for you, and then said that the data is wrong, the logic has a problem, the stream is continuous, and read is removed, so if it is 8 + 8, read 3 be thrown away, read after 9: what do you think is wrong, actually he is right
CodePudding user response:
_serialPort. BaudRate=19200;_serialPort. DataBits=8;
_serialPort. StopBits=StopBits. Two;
_serialPort. Parity=Parity. Odd
Confirm these parameter Settings and serial assistant?
Serial assistants of the default setting is not like that, is set up after the check digit, the received data validation failure not to receive handler?
CodePudding user response:
Capture your serial assistant tool set parameters to seeCodePudding user response:
The met, too long time in the past, seems to be associated with hardware, some do not support USB port,CodePudding user response: