I am writing a program to control the device on the serial port. Each team is an array of bits and must begin at FC
. Example commands:
Status Request ---> [FC 05 11 27 56] <--- Idling [FC 05 11 27 56] Status Request ---> [FC 05 11 27 56] <--- ACCEPTING [FC 05 12 BC 64] Status Request ---> [FC 05 11 27 56] <--- Reject 77 [FC 06 17 77 67 E9]
For some reason, in response, the FC
place comes to me 3F
because of what I can’t count the CRC normally. Of course, I could change 3F
to FC
manually, but I do not think that this is the right decision.
public void CommOpen(string port, int bauld) { try { _serialPort = new SerialPort(port, bauld, Parity.Even, 8, StopBits.One); _serialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); _serialPort.Open(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer Thread.Sleep(20); byte[] ba = Encoding.Default.GetBytes(_serialPort.ReadExisting()); if (ba.Length >= 5) { try { Console.WriteLine("<------------"); var hexString = BitConverter.ToString(ba); Console.WriteLine(hexString); if (CheckCRC(ba)) { switch (ba[2]) { case ENABLE: Console.WriteLine("Inhibit"); break; case INITIALIZE: Console.WriteLine("Initializing"); break; case ACCEPTING: Console.WriteLine("accepting"); break; case ESCROW: Console.WriteLine("ESCROW"); break; case STACKING: Console.WriteLine("STACKING"); break; case VENDVALID: Console.WriteLine("vendvalid"); break; case STACKED: Console.WriteLine("stacked"); break; case REJECTING: Console.WriteLine("rejecting"); break; case RETURNING: Console.WriteLine("returning"); break; case HOLDING: Console.WriteLine("holding"); break; case DISABLE: Console.WriteLine("disable"); break; case INVALID_COMMAND: Console.WriteLine("INVALID_COMMAND"); break; case POWERUP: Console.WriteLine("Powerup"); break; case COMUNICATION_ERROR: Console.WriteLine("COMUNICATION_ERROR"); break; default: break; } } else { Console.WriteLine("command error"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
FC
and3F
in binary representation differ by two zeros. For some reason, these two zeros are lost. Try reducing data transfer rates and otherStopBits
values. Perhaps the problem is not in C # code, but in your device. - AstronavigatorFC
CRC, it is normal to consider. - Andrew Romanenko