Ejemplo n.º 1
0
        public void SendDevicePropertyMessage(string type, string name, string argument)
        {
            DeviceProperty_10 deviceProperty = null;

            //if (Enum.TryParse(type, out DeviceProperty_10.Types.DevicePropertyType device_property_type))
            if (Enum.TryParse(type, true, out DeviceProperty_10.Types.DevicePropertyType device_property_type))
            {
                if (Enum.TryParse(name, true, out DeviceProperty_10.Types.DevicePropertyName device_property_name))
                {
                    deviceProperty          = new DeviceProperty_10();
                    deviceProperty.Type     = device_property_type;
                    deviceProperty.Name     = device_property_name;
                    deviceProperty.Argument = argument;
                }
            }

            if (deviceProperty != null)
            {
                LynxaMessageInfo lynxaMessageInfo = new LynxaMessageInfo();
                lynxaMessageInfo.deviceUid     = 0xFFFFFFFF;
                lynxaMessageInfo.messageId     = 10;
                lynxaMessageInfo.payloadBuffer = deviceProperty.ToByteArray();
                lynxaMessageInfo.payloadSize   = Convert.ToUInt16(lynxaMessageInfo.payloadBuffer.Length);

                byte[] output = MessageHandler.ConstructPacket(lynxaMessageInfo);
                _serialPort.Write(output, 0, output.Length);
            }
        }
Ejemplo n.º 2
0
        public void Read()
        {
            while (_serialPort.IsOpen)
            {
                try
                {
                    int data = _serialPort.ReadByte();
                    if (data != -1)
                    {
                        LynxaMessageInfo lynxa_message_info = _myMessageHandler.ParsePacket((byte)data);
                        if (lynxa_message_info != null)
                        {
                            LynxaMessageId lynxa_message_id = (LynxaMessageId)lynxa_message_info.messageId;
                            switch (lynxa_message_id)
                            {
                            case LynxaMessageId.DeviceProperty10Id:
                                DeviceProperty_10 deviceProperty_10 = DeviceProperty_10.Parser.ParseFrom(lynxa_message_info.payloadBuffer);
                                LynxaPacketReceivedEvent?.Invoke(this, deviceProperty_10);
                                break;

                            case LynxaMessageId.GnggaMessage100Id:
                                GnggaMessage_100 nmeaRecord_100 = GnggaMessage_100.Parser.ParseFrom(lynxa_message_info.payloadBuffer);
                                LynxaPacketReceivedEvent?.Invoke(this, nmeaRecord_100);
                                break;

                            case LynxaMessageId.WifiStationList102Id:
                                WifiStationList_102 wifiStationList_102 = WifiStationList_102.Parser.ParseFrom(lynxa_message_info.payloadBuffer);
                                LynxaPacketReceivedEvent?.Invoke(this, wifiStationList_102);
                                break;

                            case LynxaMessageId.ModemParameters103Id:
                                ModemParameters_103 modemParameters_103 = ModemParameters_103.Parser.ParseFrom(lynxa_message_info.payloadBuffer);
                                LynxaPacketReceivedEvent?.Invoke(this, modemParameters_103);
                                break;
                            }
                        }
                    }
                }
                catch (TimeoutException te)
                {
                    //comes here if there was a timeout
                }
            }
        }
Ejemplo n.º 3
0
        public static byte[] ConstructPacket(LynxaMessageInfo messageInfo)
        {
            UInt16 message_write_offset = 0;
            UInt16 checksum             = 0;

            byte[] buffer = new byte[1024];

            //write magic bytes
            buffer[message_write_offset++] = Convert.ToByte('#');
            buffer[message_write_offset++] = Convert.ToByte('#');
            //write modifier
            buffer[message_write_offset++] = 0xFF;
            //write device UID
            buffer[message_write_offset++] = (byte)(messageInfo.deviceUid >> 24);
            buffer[message_write_offset++] = (byte)(messageInfo.deviceUid >> 16);
            buffer[message_write_offset++] = (byte)(messageInfo.deviceUid >> 8);
            buffer[message_write_offset++] = (byte)(messageInfo.deviceUid >> 0);
            //write message ID
            buffer[message_write_offset++] = (byte)(messageInfo.messageId >> 8);
            buffer[message_write_offset++] = (byte)(messageInfo.messageId >> 0);
            //write payload size
            buffer[message_write_offset++] = (byte)(messageInfo.payloadSize >> 8);
            buffer[message_write_offset++] = (byte)(messageInfo.payloadSize >> 0);
            //write payload
            Array.Copy(messageInfo.payloadBuffer, 0, buffer, message_write_offset, messageInfo.payloadSize);
            message_write_offset += messageInfo.payloadSize;

            //calculate context->checksum
            for (UInt16 i = 0; i < message_write_offset; i++)
            {
                checksum += buffer[i];
            }

            //write context->checksum
            buffer[message_write_offset++] = (byte)(checksum >> 8);
            buffer[message_write_offset++] = (byte)(checksum >> 0);

            byte[] output_array = new byte[message_write_offset];
            Array.Copy(buffer, output_array, output_array.Length);
            return(output_array);
        }
Ejemplo n.º 4
0
        public LynxaMessageInfo ParsePacket(byte value)
        {
            bool             full_packet_received = false;
            bool             reset_state_machine  = false;
            UInt16           calculated_checksum  = 0;
            LynxaMessageInfo lynxa_message_info   = null;

            //Console.Write($"{value:X} ");
            _packetBuffer[_packetCounter++] = value;

            switch (_parserState)
            {
            case 0:     //look for the first magic byte '#'
                if (value == '#')
                {
                    _parserState++;
                }
                else
                {
                    reset_state_machine = true;
                }
                break;

            case 1:     //look for the second magic byte '#'
                if (value == '#')
                {
                    _parserState++;
                }
                else
                {
                    reset_state_machine = true;
                }
                break;

            case 2:     //save the modifier
                _modifier = value;
                _parserState++;
                break;

            case 3:     //save the device UID (MSB)
                _deviceUid = (UInt32)(value << 24);
                _parserState++;
                break;

            case 4:     //save the device UID
                _deviceUid |= (UInt32)(value << 16);
                _parserState++;
                break;

            case 5:     //save the device UID
                _deviceUid |= (UInt32)(value << 8);
                _parserState++;
                break;

            case 6:     //save the device UID (LSB)
                _deviceUid |= (UInt32)(value << 0);
                _parserState++;
                //Console.WriteLine($"_deviceUid={_deviceUid}");
                break;

            case 7:     //save the message id (MSB)
                _messageId = (UInt16)(value << 8);
                _parserState++;
                break;

            case 8:     //save the message id (LSB)
                _messageId |= (UInt16)(value << 0);
                _parserState++;
                //Console.WriteLine($"_messageId={_messageId}");
                break;

            case 9:     //save the payload size (MSB)
                _payloadSize = (UInt16)(value << 8);
                _parserState++;
                break;

            case 10:     //save the payload size (LSB)
                _payloadSize |= (UInt16)(value << 0);
                _parserState++;
                _payloadIndex = _packetCounter;
                //Console.WriteLine($"_payloadSize={_payloadSize}");
                break;

            case 11:     //save each byte of the payload
                _payloadCounter++;
                if (_payloadCounter == _payloadSize)
                {
                    _checksumPayloadIndex = _packetCounter;
                    _parserState++;
                }
                break;

            case 12:     //save the checksum (MSB)
                _checksum = (UInt16)(value << 8);
                _parserState++;
                break;

            case 13:     //save the checksum (LSB)
                _checksum |= (UInt16)(value << 0);
                _parserState++;
                //Console.WriteLine($"_checksum={_checksum}");

                //verify checksum
                for (int i = 0; i < _checksumPayloadIndex; i++)
                {
                    calculated_checksum += _packetBuffer[i];
                }

                //Console.WriteLine($"calculated_checksum={calculated_checksum}");

                if (_checksum == calculated_checksum)
                {
                    //packet is valid
                    full_packet_received = true;
                }
                reset_state_machine = true;
                break;
            }

            if (full_packet_received)
            {
                lynxa_message_info                  = new LynxaMessageInfo();
                lynxa_message_info.modifier         = _modifier;
                lynxa_message_info.deviceUid        = _deviceUid;
                lynxa_message_info.messageId        = _messageId;
                lynxa_message_info.payloadSize      = _payloadSize;
                lynxa_message_info.totalMessageSize = _packetCounter;
                lynxa_message_info.messageBuffer    = new byte[_packetCounter];
                lynxa_message_info.payloadBuffer    = new byte[_payloadSize];

                Array.Copy(_packetBuffer, 0, lynxa_message_info.messageBuffer, 0, _packetCounter);
                Array.Copy(_packetBuffer, _payloadIndex, lynxa_message_info.payloadBuffer, 0, _payloadSize);
            }

            if (reset_state_machine)
            {
                ResetStateMachine();
            }

            return(lynxa_message_info);
        }