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 and 3F in binary representation differ by two zeros. For some reason, these two zeros are lost. Try reducing data transfer rates and other StopBits values. Perhaps the problem is not in C # code, but in your device. - Astronavigator
  • @Astronavigator agrees about the transfer options. For the diagnosis you need to use a working terminalka. - Vladimir Martyanov
  • @Astronavigator @Vladimir Martyanov It seems everything is as in the documentation. screen When manually replacing FC CRC, it is normal to consider. - Andrew Romanenko
  • Try to increase the pause, maybe the device is not coping with your speed. - VladD
  • @VladD Using 9600 baud - Andrew Romanenko

0