I have a lot of problems trying to get the serial port to get the correct message. He continues to trim messages. Here is my code, and I will try to clarify it after the code.
public SerialComms(SerialPort sp) { this.sp = sp; this.sp.Open(); this.sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); do { Console.WriteLine("port open waiting message"); Console.ReadKey(); } while(!_terminate); this.sp.Close(); } void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { string dataReceived; StringComparer strComp = StringComparer.OrdinalIgnoreCase; SerialPort sp = (SerialPort)sender; int i = sp.BytesToRead; byte[] _byte = new byte[i]; char[] _char = new char[i]; sp.read(_byte, 0, i); dataReceived = Encoding.UTF8.GetString(_byte);
Now I have a test project that we use to test our serial computers in production with legacy software - I know that this is normal. I connected a serial monitor to the port, and the message coming through the transmission does not cause any problems. When I send a message, such as 012345678901234, the first time it usually passes through it is a fine, and on the receiving side, the monitor shows that it passes; however, when it prints to the console, it is clipped after the first message. I attach screenshots of the message passing through the port monitor and the output to the console (the emoticon and heart that appear are converted to prefix bytes - why is it a heart and emoticon that I don’t know about)
Okay, so I can’t post the image because I don’t have enough reputation to do this. I will write what the output to the console looks like below (in this case, for some reason, it also cut the first message :()
On the serial port monitor, the transmitted message is as follows (I sent it three times in a few seconds, the “delay time” between each sending of the message):
02 31 32 33 34 35 36 37 38 39 30 31 32 33 34 03.12345678901234.
02 31 32 33 34 35 36 37 38 39 30 31 32 33 34 03.12345678901234.
02 31 32 33 34 35 36 37 38 39 30 31 32 33 34 03.12345678901234.
On the console, I received the following (the characters ☺ and ♥ are 02 and 03, this is a STX and ETX message, standard for our programs):
☺123456
78901234 ♥
☺
123
456
7890
123
4 ♥
☺
123
456
7890
123
4 ♥
This problem is driving me crazy !!! Please help! The legacy uses the obsolete MSCommLib, and we move on to .Net 4
c # serial-port
alykins
source share