public CommsLib(String param, processDataRx process_data) { process_decoded = process_data; phys_connection = new CommsCon(); if (phys_connection.connect(param, process_raw)) { connected = true; } else { phys_connection = null; connected = false; } }
public bool connect(String parameter, processDataRx process_rx_data) { process_data = process_rx_data; try { serial_port = new SerialPort(parameter); // // We know now that the baud rate must be 460800 because we will only be communicating with the // the unit via USB. The actual COM port will vary with the machine so we shouldn't // restrict ourselves. // // Setup the serial port serial_port.BaudRate = 460800; serial_port.Parity = Parity.None; serial_port.DataBits = 8; serial_port.StopBits = StopBits.One; serial_port.Handshake = Handshake.None; serial_port.ReadBufferSize = 0x10000; serial_port.RtsEnable = true; // Try to open it serial_port.Open(); // Clear out the buffers serial_port.DiscardInBuffer(); serial_port.DiscardOutBuffer(); serial_port.ReceivedBytesThreshold = 1; // Write 'x' to the unit serial_port.Write("x\r\n"); int loops = 10; while (loops > 0) { if (serial_port.BytesToRead > 12) { String sdata = serial_port.ReadLine(); break; } System.Threading.Thread.Sleep(10); loops--; } serial_port.DiscardInBuffer(); serial_port.DiscardOutBuffer(); if (loops == 0) { //02 00 20 E0 03 byte[] cmd = new byte[] { 0x02, 0x00, 0x20, 0xE0, 0x03 }; serial_port.Write(cmd, 0, cmd.Length); loops = 10; while (loops > 0) { if (serial_port.BytesToRead > 12) { String sdata = serial_port.ReadExisting(); break; } System.Threading.Thread.Sleep(10); loops--; } serial_port.DiscardInBuffer(); serial_port.DiscardOutBuffer(); if (loops == 0) { serial_port.Close(); serial_port = null; return(false); } } } catch (Exception) { serial_port.Close(); serial_port = null; return(false); } serial_port.DataReceived += new SerialDataReceivedEventHandler(receive_data); return(true); }