예제 #1
0
        private Packet createDataPacket()
        {
            sensor++;
            if (sensor > sensorCount)
                sensor = 1;
            if (sensor == 16)
                sensor++;

            float[] data = new float[10];
            Random random = new Random();
            for(int i = 0; i < 10; i++)
            {
                data[i] = (float)(random.NextDouble());
            }
            /*data[0] = 1.0F;
            data[1] = 1.0F;
            data[2] = 1.0F;
            data[3] = 1.0F;
            data[4] = 1.0F;
            data[5] = 1.0F;
            data[6] = 1.0F;
            data[7] = 1.0F;
            data[8] = 1.0F;
            data[9] = 1.0F;*/
            byte[] payload = new byte[41];
            payload[0] = sensor;
            Buffer.BlockCopy(data, 0, payload, 1, payload.Length - 1);
            Packet dataPacket = new Packet((byte)PacketSender.Microcontroller | (byte)PacketType.Report, (byte)payload.Length, payload);
            return dataPacket;
        }
예제 #2
0
        public int HandlePacket(Packet packetIn, SerialPort serialPort, Chart chart = null, bool forceSend = false)
        {
            if (forceSend)
            {
                writePacket(packetIn, serialPort);
                return (int)ProtocolDirective.DoNothing;
            }

            switch (state)
            {
                case ProtocolState.Idle:
                    if (packetIn.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
                        writePacket(packetIn, serialPort);
                        state = ProtocolState.StartSent;
                    }
                    break;

                case ProtocolState.StartSent:
                    if (packetIn.command == ((byte)PacketType.Start | (byte)PacketSender.Microcontroller))
                    {// If we're waiting for an acknowledge and we receive one from the microcontroller
                        state = ProtocolState.WaitForData;
                    }
                    else if (packetIn.command == ((byte)PacketType.Error | (byte)PacketSender.Microcontroller))
                    {// If we're waiting for an acknowledge and we receive an error packet from the microcontroller
                        state = ProtocolState.ErrorReceived;
                    }
                    break;

                case ProtocolState.StopSent:
                    if (packetIn.command == ((byte)PacketType.Stop | (byte)PacketSender.Microcontroller))
                    {// If we're waiting for an acknowledge and we receive one from the microcontroller
                        state = ProtocolState.Idle;
                    }
                    break;

                case ProtocolState.WaitForData:
                    if (packetIn.command == ((byte)PacketType.Report | (byte)PacketSender.Microcontroller))
                    {// If we're waiting for data packets and we receive one from the microcontroller
                        byte[] payload = new byte[1];
                        payload[0] = packetIn.payload[0];
                        Packet ackPacket = new Packet((byte)PacketType.Report | (byte)PacketSender.GUI, 1, payload);
                        writePacket(ackPacket, serialPort);
                        return (int)ProtocolDirective.AddData;
                    }
                    else if (packetIn.command == ((byte)PacketType.Stop | (byte)PacketSender.GUI))
                    {// If we're waiting for data packets and we receive a stop packet from the GUI
                        writePacket(packetIn, serialPort);
                        state = ProtocolState.StopSent;
                    }
                    break;

                case ProtocolState.ErrorReceived:
                    break;

                default:
                    break;
            }
            return (int)ProtocolDirective.DoNothing;
        }
예제 #3
0
        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;
            }
        }
예제 #4
0
        private void writePacket(Packet packetToWrite)
        {
            byte[] buf = new byte[4 + packetToWrite.payloadLength];
            buf[0] = packetToWrite.SOF;
            buf[1] = packetToWrite.command;
            buf[2] = packetToWrite.payloadLength;
            //if(packetToWrite.payloadLength != 0)
            //	Array.Copy(packetToWrite.payload, 0, buf, 3, packetToWrite.payloadLength);
            for (int i = 0; i < packetToWrite.payloadLength; i++)
            {
                buf[i + 3] = packetToWrite.payload[i];
            }
            try
            {
                buf[3 + packetToWrite.payloadLength] = packetToWrite.Xor;
                if (serialPort != null)
                    serialPort.Write(buf, 0, buf.Length);
            } catch (ArgumentNullException) {

            } catch (InvalidOperationException) {

            } catch (ArgumentOutOfRangeException) {

            } catch (ArgumentException) {

            } catch (TimeoutException) {

            } catch (Exception) {

            }
        }
예제 #5
0
 private void writePacket(Packet packetToWrite, SerialPort port)
 {
     byte[] buf = new byte[4 + packetToWrite.payloadLength];
     buf[0] = packetToWrite.SOF;
     buf[1] = packetToWrite.command;
     buf[2] = packetToWrite.payloadLength;
     if(packetToWrite.payloadLength != 0)
         Array.Copy(packetToWrite.payload, 0, buf, 3, packetToWrite.payloadLength);
     buf[3 + packetToWrite.payloadLength] = packetToWrite.Xor;
     try
     {
         port.Write(buf, 0, buf.Length);
     } catch (ArgumentNullException) {
         //throw new ArgumentNullException();
     } catch (InvalidOperationException) {
         //throw new InvalidOperationException();
     } catch (ArgumentOutOfRangeException) {
         //throw new ArgumentOutOfRangeException();
     } catch (ArgumentException) {
         //throw new ArgumentException();
     } catch (TimeoutException) {
         //throw new TimeoutException();
     } catch (Exception) {
         //throw new Exception();
     }
 }