public class DataReceiveHandle { public static int MAX_PACKET_LENGTH = ChannelDataCount.count; public bool newData = false; public int rxOffset = 0; public byte[] rxBuffer = new byte[MAX_PACKET_LENGTH]; public byte[] rxPackage = new byte[MAX_PACKET_LENGTH]; public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { // Console.WriteLine("In Data receive "); if (e.EventType != SerialData.Chars) return; SerialPort port = (SerialPort)sender; int bytesCount = port.BytesToRead; port.Read(rxBuffer, rxOffset, bytesCount); rxOffset = rxOffset + bytesCount; if (HasCompletePackage()) { Console.WriteLine("Full packet has been received"); rxBuffer.CopyTo(rxPackage, 0); rxOffset = 0; newData = true; } if (newData == true) { if (rxPackage[0] == 224 && rxPackage[1] == 242 && rxPackage[2] == 1 && rxPackage[3] == 0 && rxPackage[4] == 60 && rxPackage[5] == 37 && rxPackage[6] == 132 && rxPackage[7] == 0 && rxPackage[8] == 0) { Console.WriteLine("Header Content is:"); for (int i = 0; i <= 8; i++) { Console.WriteLine(rxPackage[i]); } } Console.WriteLine("Message Content is: "); for (int i = 9; i < MAX_PACKET_LENGTH; i++) { Console.WriteLine(rxPackage[i]); } } } private bool HasCompletePackage() { if (rxOffset == 60) return true; else return false; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)