public static bool LSWrite(CSerialPort serial_port, byte port, byte[] tx_data, byte rx_data_length) { byte[] message = new byte[tx_data.Length + 5]; message[0] = 0x00; message[1] = (byte)DirectCommand.LSWrite; message[2] = port; message[3] = (byte)tx_data.Length; message[4] = rx_data_length; tx_data.CopyTo(message, 5); return serial_port.Send(message); }
public static byte LSGetStatus(CSerialPort serial_port, byte port) { byte[] message = new byte[3]; message[0] = 0x00; message[1] = (byte)DirectCommand.LSGetStatus; message[2] = port; byte[] reply = new byte[4]; if (!serial_port.Send(message, ref reply)) return 0; if (reply.Length != 4) return 0; return reply[3]; }
public static bool LSRead(CSerialPort serial_port, byte port, ref byte[] rx_data) { byte[] message = new byte[3]; message[0] = 0x00; message[1] = (byte)DirectCommand.LSRead; message[2] = port; byte[] reply = new byte[20]; if (!serial_port.Send(message, ref reply)) return false; if (reply.Length != 20) return false; Array.Resize(ref rx_data, reply[3]); Array.Copy(reply, 4, rx_data, 0, reply[3]); return true; }
/// <summary> /// Will try and connect to a specific ComPort /// </summary> /// <param name="port_name"></param> /// <returns></returns> private bool Connect(string port_name) { serial_port = new CSerialPort(); DeviceInfo = new CDeviceInfo(serial_port); InPort1 = new InPort(serial_port, 0, SensorType.Switch); InPort2 = new InPort(serial_port, 1, SensorType.SoundDB); InPort3 = new InPort(serial_port, 2, SensorType.LightActive); InPort4 = new InPort(serial_port, 3, SensorType.Sonar); OutPortA = new OutPort(serial_port, MotorPort.MotorA); OutPortB = new OutPort(serial_port, MotorPort.MotorB); OutPortC = new OutPort(serial_port, MotorPort.MotorC); OutPortAll = new OutPort(serial_port, MotorPort.MotorAll); comPort = port_name; Info = new Details(serial_port); DirectCommands = new Direct_Commands(serial_port); if (!serial_port.Connect(port_name)) return false; if (!serial_port.Send(DirectCommand.KeepAlive)) { NXTDisconnect(); return false; } connected = true; return true; }