Home > Net >  C # serial port method DataReceived doesn't always trigger, but the use of serial assistants ca
C # serial port method DataReceived doesn't always trigger, but the use of serial assistants ca

Time:12-09

Recently in with mitsubishi PLC (FX3U) communication USES RS485 communication (using the 485 turn 232 converter connected to the PC serial port),
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 correctly

With 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 see

CodePudding 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:

refer to the second floor Hobo, reply:
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 is no other.

Configuration parameters are confirmed, and equipment

CodePudding user response:

reference 4 floor glen30 response:
_serialPort. The 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?


Parameter configuration and serial assistants, equipment send to the same instruction, but serial assistants can receive, but his writing function cannot receive normally

CodePudding user response:

https://blog.csdn.net/chenhongwu666/article/details/40142513? Utm_medium=distribute. Pc_relevant_bbs_down. None - task - blog - baidujs - 1. Nonecase& Depth_1 - utm_source=distribute. Pc_relevant_bbs_down. None - task - blog - baidujs - 1. Nonecase

Try this one

CodePudding user response:

You under a machine is set up to receive a newline FuCai send upper machine data? If this is the case,
PC send instructions to be in the back and a newline '\ r',
Serial debugging assistant is the default on this option

CodePudding user response:

Met colleagues appear this problem before, do you see your two new serialPort object
  •  Tags:  
  • C#
  • Related