Beispiel #1
0
        public CANError DecodePacket(byte[] buff, ref CANPacket packet)
        {
            CANError errorcode  = CANError.CAN_ERROR_NONE;
            UInt32   extendedID = ((UInt32)buff[0] << 24) + ((UInt32)buff[1] << 16) + ((UInt32)buff[2] << 8) + (UInt32)buff[3];

            packet.Packet_type         = (CANPacket.Pkt_Type_t)((extendedID >> 24) & 0x03);
            packet.Packet_id           = ((extendedID >> 16) & 0x0F);
            packet.Source_address      = (extendedID >> 8) & 0xFF;
            packet.Destination_address = extendedID & 0xFF;

            int idx = 4;

            packet.Length = buff[idx++];

            if (packet.Length > CANPacket.BufferSize)
            {
                return(CANError.CAN_ERROR_LENGTH);
            }

            for (int i = 0; i < packet.Length; i++)
            {
                packet.Buffer[i] = buff[idx++];
            }

            return(errorcode);
        }
Beispiel #2
0
        public static void getDeviceInfor(CANPacket e, GlobalData globalData)
        {
            string UUID = null;
            int    short_address;

            int packet_idx = 0;

            CANPacket.Device_Types_t deviceType = (CANPacket.Device_Types_t)e.Buffer[packet_idx++];
            int dev_catogory = e.Buffer[packet_idx++];

            for (int i = 0; i < 16; i++)
            {
                UUID += e.Buffer[packet_idx++].ToString("X2");
            }
            short_address = e.Buffer[packet_idx++];


            if (deviceType == CANPacket.Device_Types_t.Device_Type_Keypad)
            {
                KeyPad keypad = new KeyPad();
                keypad.uuid         = UUID;
                keypad.shortAddress = short_address;
                keypad.numOfKeys    = e.Buffer[packet_idx++];
                keypad.type         = (KEYPAD_TYPE)dev_catogory;
                int index = globalData.deviceInfo.keyPads.FindIndex(x => x.uuid == UUID);
                if (index == -1)
                {
                    globalData.deviceInfo.keyPads.Add(keypad);
                }
                else
                {
                    globalData.deviceInfo.keyPads[index] = keypad;
                }
                Console.WriteLine("key pad found");
            }
            else if (deviceType == CANPacket.Device_Types_t.Device_Type_Mainboard)
            {
                ECU ecu = new ECU();
                ecu.uuid                    = UUID;
                ecu.shortAddress            = short_address;
                ecu.numberOFPOsitiveOutputs = e.Buffer[packet_idx++];
                ecu.numberOFNEgativeOutputs = e.Buffer[packet_idx++];
                ecu.numberOFAnalogInput     = e.Buffer[packet_idx++];
                ecu.numberOFExternalOutputs = e.Buffer[packet_idx++];
                ecu.type                    = (ECU_TYPE)dev_catogory;
                int index = globalData.deviceInfo.ecus.FindIndex(x => x.uuid == UUID);
                if (index == -1)
                {
                    globalData.deviceInfo.ecus.Add(ecu);
                }
                else
                {
                    globalData.deviceInfo.ecus[index] = ecu;
                }
                Console.WriteLine("mainboard found");
            }

            Console.Write("done");
        }
Beispiel #3
0
        public void cmdReadPosOutputConfigs(uint seleectedShortAddess, int negativeOutIdx)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id   = (UInt32)CANPacket.Pkt_ID_t.Cmd_Read_Pos_Output_Configs;
            //kasun complete the rest
        }
Beispiel #4
0
        public void cmdReadPosOutCurrent(uint seleectedShortAddess, int analogInputIndx)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id   = (UInt32)CANPacket.Pkt_ID_t.Cmd_Read_Pos_Out_Current;
            //kasun complete the rest
        }
Beispiel #5
0
        public void cmdReadBoardSensors(uint shortAddress)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Read_Board_Sensors;
            packet.Source_address      = 0;
            packet.Destination_address = shortAddress;
            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            transportLayer.SendPacket(ref tl_packet);
            logger.Info("Read board sensors", tl_packet);
        }
Beispiel #6
0
        public void cmdRequestDevInfo()
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Request_Dev_Info;
            packet.Source_address      = 0;
            packet.Destination_address = 0xff;
            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            serial.sendMessage(transportLayer.SendPacket(ref tl_packet));
            logger.Info("Request device Info: ", tl_packet);
        }
Beispiel #7
0
        public void cmdReadAnalogInputs(uint seleectedShortAddess, int analogInputIndx)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Read_Analog_In_Settings;
            packet.Source_address      = 0;
            packet.Destination_address = seleectedShortAddess;
            packet.Length    = 1;
            packet.Buffer[0] = (byte)analogInputIndx;
            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            transportLayer.SendPacket(ref tl_packet);
            logger.Info("Read analog inputs", tl_packet);
        }
Beispiel #8
0
        public void cmdReadNegativeOutputConfigs(uint seleectedShortAddess, int negativeOutIdx)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Read_Neg_Output_Configs;
            packet.Source_address      = 0;
            packet.Destination_address = seleectedShortAddess;
            packet.Length = 0;
            packet.Buffer[packet.Length++] = (byte)negativeOutIdx;

            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            transportLayer.SendPacket(ref tl_packet);
            logger.Info("read negative output configs", tl_packet);
        }
Beispiel #9
0
        private void ReadSerialPackets()
        {
            while (true) // handle this
            {
                // Read packets form Serial port and put them in to relavant queues
                TL_Packet packet = new TL_Packet();
                try
                {
                    if (transportLayer.GetPacket(ref packet))
                    {
                        // Serial packet is available. Pass it to CAN papcket processor for decording.
                        CANPacket can_packet = new CANPacket();
                        if (DecodePacket(packet.Buffer, ref can_packet) == CANError.CAN_ERROR_NONE)
                        {
                            switch (can_packet.Packet_type)
                            {
                            case CANPacket.Pkt_Type_t.Pkt_Type_Command:
                                Console.WriteLine("command");
                                //InsertCommand(ref can_packet);
                                break;

                            case CANPacket.Pkt_Type_t.Pkt_Type_Response:
                                Console.WriteLine("response");
                                serial.addResponse(ref can_packet);
                                break;

                            case CANPacket.Pkt_Type_t.Pkt_Type_Event:
                                Console.WriteLine("event");
                                serial.addEvent(ref can_packet);
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                //if (!threadSerialPacketReaderRun) break;
                Thread.Sleep(200);
            }
        }
Beispiel #10
0
        internal void EncodePacket(ref CANPacket can_packet, ref TL_Packet serialPacket)
        {
            UInt32 extId = (can_packet.Destination_address & 0xFF) |
                           ((can_packet.Source_address & 0xFF) << 8) |
                           (((UInt32)can_packet.Packet_id & 0xF) << 16) |
                           ((UInt32)can_packet.Packet_type & 0x03) << 24;

            UInt16 buffIdx = 0;

            serialPacket.Buffer[buffIdx++] = (byte)(extId >> 24);
            serialPacket.Buffer[buffIdx++] = (byte)(extId >> 16);
            serialPacket.Buffer[buffIdx++] = (byte)(extId >> 8);
            serialPacket.Buffer[buffIdx++] = (byte)extId;
            serialPacket.Buffer[buffIdx++] = (byte)can_packet.Length;
            for (int i = 0; i < can_packet.Length; i++)
            {
                serialPacket.Buffer[buffIdx++] = can_packet.Buffer[i];
            }
            serialPacket.Length = (byte)buffIdx;
        }
Beispiel #11
0
        public void cmdWriteAnalogInputs(uint seleectedShortAddess, int analogInputIndx, int minVoltage, int maxVoltage)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Write_Analog_In_Settings;
            packet.Source_address      = 0;
            packet.Destination_address = seleectedShortAddess;
            packet.Length = 0;
            packet.Buffer[packet.Length++] = (byte)analogInputIndx;
            packet.Buffer[packet.Length++] = (byte)((minVoltage >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(minVoltage & 0xFF);
            packet.Buffer[packet.Length++] = (byte)((maxVoltage >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(maxVoltage & 0xFF);

            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            transportLayer.SendPacket(ref tl_packet);
            logger.Info("write analog inputs", tl_packet);
        }
Beispiel #12
0
 public void addEvent(ref CANPacket obj)
 {
     eventQueue.Add(obj);
 }
Beispiel #13
0
 public void addResponse(ref CANPacket obj)
 {
     responseQueue.Add(obj);
 }
Beispiel #14
0
        public void cmdWritePosOutputConfigs(uint seleectedShortAddess, PositiveOutput positive)
        {
            CANPacket packet = new CANPacket();

            packet.Packet_type         = CANPacket.Pkt_Type_t.Pkt_Type_Command;
            packet.Packet_id           = (UInt32)CANPacket.Pkt_ID_t.Cmd_Write_Pos_Output_Configs;
            packet.Source_address      = 0;
            packet.Destination_address = seleectedShortAddess;
            packet.Length = 0;

            packet.Buffer[packet.Length++] = (byte)positive.Index;

            packet.Buffer[packet.Length++] = (byte)positive.Activator;
            packet.Buffer[packet.Length++] = (byte)((positive.ActiveTime >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.ActiveTime & 0xFF);
            packet.Buffer[packet.Length++] = (byte)((positive.StartupDelay >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.StartupDelay & 0xFF);


            // Activate conditions
            packet.Buffer[packet.Length++] = (byte)positive.ConditionSource;
            packet.Buffer[packet.Length++] = (byte)positive.SourceIndex;
            packet.Buffer[packet.Length++] = (byte)((positive.TurnOnWhen >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.TurnOnWhen & 0xFF);
            packet.Buffer[packet.Length++] = (byte)((positive.TurnOffWhen >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.TurnOffWhen & 0xFF);
            // TODO:Fix this
            packet.Buffer[packet.Length++] = (byte)positive.LEDOutputErrFreq;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOutputErrBlink;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOutputErrBright;


            // Voltage monitoring
            packet.Buffer[packet.Length++] = Convert.ToByte(positive.VoltageMonitor);
            packet.Buffer[packet.Length++] = (byte)positive.VoltageSource;
            UInt16 minVoltage = (UInt16)(positive.MinVoltage * 1000.0);

            packet.Buffer[packet.Length++] = (byte)((minVoltage >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(minVoltage & 0xFF);
            UInt16 maxVoltage = (UInt16)(positive.MaxVoltage * 1000.0);

            packet.Buffer[packet.Length++] = (byte)((maxVoltage >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(maxVoltage & 0xFF);
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderVoltageFreq;
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderVoltageBlink;
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderVoltageBright;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverVoltageFreq;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverVoltageBlink;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverVoltageBright;

            // Current monitoring
            packet.Buffer[packet.Length++] = Convert.ToByte(positive.CurrentMonitor);
            packet.Buffer[packet.Length++] = (byte)((positive.MinimumCurrent >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.MinimumCurrent & 0xFF);
            packet.Buffer[packet.Length++] = (byte)((positive.MaxCurrent >> 8) & 0xFF);
            packet.Buffer[packet.Length++] = (byte)(positive.MaxCurrent & 0xFF);
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderCurrFreq;
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderCurrBlink;
            packet.Buffer[packet.Length++] = (byte)positive.LEDUnderCurrBright;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverCurrFreq;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverCurrBlink;
            packet.Buffer[packet.Length++] = (byte)positive.LEDOverCurrBright;

            // Load shading
            packet.Buffer[packet.Length++] = (byte)positive.LoadShading;

            TL_Packet tl_packet = new TL_Packet();

            canInterface.EncodePacket(ref packet, ref tl_packet);
            //transportLayer.SendPacket(ref tl_packet);
            serial.sendMessage(transportLayer.SendPacket(ref tl_packet));
            logger.Info("Write positive output configs", tl_packet);
        }