public Microcontroller(SerialPort port, int speed = 1000, int count = 30) { serialPort = port; serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); state = MicrocontrollerState.Idle; dataSpeed = speed; sensorCount = count; }
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs args) { Packet packet = null; try { byte startOfFrame = (byte)serialPort.ReadByte(); if (startOfFrame != 0xFE) { return; } byte command = (byte)serialPort.ReadByte(); byte payloadLength = (byte)serialPort.ReadByte(); byte[] payload = new byte[payloadLength]; if (serialPort.Read(payload, 0, payloadLength) < payloadLength) { return; } byte Xor = (byte)serialPort.ReadByte(); packet = new Packet(command, payloadLength, payload); if (packet.Xor != Xor) { return; } } catch (TimeoutException) { return; } if (packet == null) { return; } switch (state) { case MicrocontrollerState.Idle: if (packet.command == ((byte)PacketType.Start | (byte)PacketSender.GUI)) { // If we're idle and waiting to be told to do something and we receive a start packet from the GUI Packet startReplyPacket = new Packet(((byte)PacketSender.Microcontroller | (byte)PacketType.Start)); float[] data = new float[5]; Buffer.BlockCopy(packet.payload, 0, data, 0, packet.payloadLength); writePacket(startReplyPacket); Thread.Sleep(dataSpeed); writePacket(createDataPacket()); state = MicrocontrollerState.SendingData; } break; case MicrocontrollerState.SendingData: if ((packet.command == ((byte)PacketType.Report | (byte)PacketSender.GUI)) && (packet.payload[0] == sensor)) { // If we're sending data packets and we receive an acknowledge from the GUI Thread.Sleep(dataSpeed); writePacket(createDataPacket()); } if (packet.command == ((byte)PacketType.Stop | (byte)PacketSender.GUI)) { // If we're sending data packets and we receive a stop packet from the GUI Packet stopReplyPacket = new Packet(((byte)PacketSender.Microcontroller | (byte)PacketType.Stop)); writePacket(stopReplyPacket); state = MicrocontrollerState.Idle; } break; case MicrocontrollerState.SendingError: if (packet.command == ((byte)PacketType.Error | (byte)PacketSender.GUI)) { // If we're waiting for an error acknowledge packet and we receive one from the GUI if ((packet.payloadLength == 1) && (packet.payload[0] == errorSent)) { state = MicrocontrollerState.Idle; } } break; default: break; } }
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs args) { Packet packet = null; try { byte startOfFrame = (byte)serialPort.ReadByte(); if (startOfFrame != 0xFE) { return; } byte command = (byte)serialPort.ReadByte(); byte payloadLength = (byte)serialPort.ReadByte(); byte[] payload = new byte[payloadLength]; if (serialPort.Read(payload, 0, payloadLength) < payloadLength) { return; } byte Xor = (byte)serialPort.ReadByte(); packet = new Packet(command, payloadLength, payload); if (packet.Xor != Xor) { return; } } catch (TimeoutException) { return; } if (packet == null) return; switch (state) { case MicrocontrollerState.Idle: if (packet.command == ((byte)PacketType.Start | (byte)PacketSender.GUI)) {// If we're idle and waiting to be told to do something and we receive a start packet from the GUI Packet startReplyPacket = new Packet(((byte)PacketSender.Microcontroller | (byte)PacketType.Start)); float[] data = new float[5]; Buffer.BlockCopy(packet.payload, 0, data, 0, packet.payloadLength); writePacket(startReplyPacket); Thread.Sleep(dataSpeed); writePacket(createDataPacket()); state = MicrocontrollerState.SendingData; } break; case MicrocontrollerState.SendingData: if ((packet.command == ((byte)PacketType.Report | (byte)PacketSender.GUI)) && (packet.payload[0] == sensor)) {// If we're sending data packets and we receive an acknowledge from the GUI Thread.Sleep(dataSpeed); writePacket(createDataPacket()); } if (packet.command == ((byte)PacketType.Stop | (byte)PacketSender.GUI)) {// If we're sending data packets and we receive a stop packet from the GUI Packet stopReplyPacket = new Packet(((byte)PacketSender.Microcontroller | (byte)PacketType.Stop)); writePacket(stopReplyPacket); state = MicrocontrollerState.Idle; } break; case MicrocontrollerState.SendingError: if (packet.command == ((byte)PacketType.Error | (byte)PacketSender.GUI)) {// If we're waiting for an error acknowledge packet and we receive one from the GUI if ((packet.payloadLength == 1) && (packet.payload[0] == errorSent)) state = MicrocontrollerState.Idle; } break; default: break; } }
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs args) { Packet packet = null; bool dontStart = false; try { System.Threading.Thread.Sleep(100); byte startOfFrame = (byte)serialPort.ReadByte(); if (startOfFrame != 0xFE) { return; } byte command = (byte)serialPort.ReadByte(); byte payloadLength = (byte)serialPort.ReadByte(); byte[] payload = new byte[payloadLength]; if (serialPort.Read(payload, 0, payloadLength) < payloadLength) { return; } byte Xor = (byte)serialPort.ReadByte(); packet = new Packet(command, payload); if (packet.Xor != Xor) { return; } } catch (TimeoutException) { return; } if (packet == null) { return; } switch (state) { case MicrocontrollerState.Idle: if (packet.Command == (byte)PacketType.Config) { this.sensorMultiplexerAddresses = packet.Payload; this.sensorCount = packet.PayloadLength; ConfigReplyPacket configReplyPacket = new ConfigReplyPacket(); writePacket <ConfigReplyPacket>(configReplyPacket); } if (packet.Command == (byte)PacketType.Start) { // If we're idle and waiting to be told to do something and we receive a start packet from the GUI #if INCLUDE_ERRORS if (this.starts % 5 == 0) { writePacket(createStartErrorPacket()); dontStart = true; } #endif StartReplyPacket startReplyPacket = new StartReplyPacket(packet.Payload); writePacket <StartReplyPacket>(startReplyPacket); if (!dontStart) { timer = new Timer(new TimerCallback(timerEvent)); timer.Change(this.dataSpeed, 0); state = MicrocontrollerState.SendingData; } } break; case MicrocontrollerState.SendingData: if (packet.Command == (byte)PacketType.Stop) { // If we're sending data packets and we receive a stop packet from the GUI StopReplyPacket stopReplyPacket = new StopReplyPacket(); writePacket <StopReplyPacket>(stopReplyPacket); state = MicrocontrollerState.Idle; sensor = 0x00; } break; /*case MicrocontrollerState.SendingError: * if (packet.command == ((byte)PacketType.Error | (byte)PacketSender.GUI)) * {// If we're waiting for an error acknowledge packet and we receive one from the GUI * if ((packet.payloadLength == 1) && (packet.payload[0] == errorSent)) * state = MicrocontrollerState.Idle; * } * break;*/ default: break; } }