Example #1
0
        public TransportLayer()
        {
            _rxQueue = new BlockingCollection <TL_Packet>();
            var obj = new TL_Packet();

            while (_rxQueue.Count > 0)
            {
                _rxQueue.TryTake(out obj);
            }
            _rxPacket         = new TL_Packet();
            rxQueueIsDisposed = false;
        }
Example #2
0
 public bool GetPacket(ref TL_Packet packet)
 {
     //Console.WriteLine(_rxQueue.Count);
     if (!rxQueueIsDisposed)
     {
         return(_rxQueue.TryTake(out packet));
     }
     else
     {
         return(false);
     }
 }
Example #3
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);
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
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);
        }
Example #7
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);
            }
        }
Example #8
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;
        }
Example #9
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);
        }
Example #10
0
        //public static bool Connect(string comport)
        //{
        //    bool ret = false;
        //    try
        //    {
        //        _serialPort = new SerialPort();
        //        _rxPacket = new TL_Packet();
        //        _rxQueue = new BlockingCollection<TL_Packet>();
        //        rxQueueIsDisposed = false;

        //        readThread = new Thread(PacketCollector);

        //        _serialPort.PortName = comport;
        //        _serialPort.BaudRate = 115200;
        //        _serialPort.Parity = Parity.None;
        //        _serialPort.DataBits = 8;
        //        _serialPort.StopBits = StopBits.One;
        //        _serialPort.Handshake = Handshake.None;

        //        // Set the read/write timeouts
        //        _serialPort.ReadTimeout = 500;
        //        _serialPort.WriteTimeout = 500;

        //        _serialPort.Open();
        //        _packetCollectorStates = PacketCollectorStates.STATE_STX_1;
        //        _Run = true;

        //        var obj = new TL_Packet();
        //        while (_rxQueue.Count > 0)
        //        {
        //            _rxQueue.TryTake(out obj);
        //        }

        //        readThread.Start();
        //        ret = true;
        //    }
        //    catch (System.UnauthorizedAccessException uex)
        //    {

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    return ret;
        //}

        //public static String[] GetAvailablePorts()
        //{
        //    return SerialPort.GetPortNames();
        //}

        public byte[] SendPacket(ref TL_Packet packet)
        {
            int  idx = 0;
            byte LRC = 0;

            byte[] Buffer = new byte[TL_Packet.BufferSize + 10];

            Buffer[idx++] = STX_1;         // Copy start byte 1
            Buffer[idx++] = STX_2;         // Copy start byte 2
            Buffer[idx++] = STX_3;         // Copy start byte 3
            Buffer[idx++] = packet.Length; // Copy lenght L
            LRC          ^= packet.Length;
            for (int i = 0; i < packet.Length; i++)
            {
                LRC          ^= packet.Buffer[i];
                Buffer[idx++] = packet.Buffer[i]; // Copy data
            }
            // Copy LRC
            Buffer[idx++] = LRC;

            byte[] sendBuffer = new byte[idx];
            Array.Copy(Buffer, sendBuffer, idx);
            return(sendBuffer);
        }
Example #11
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);
        }
Example #12
0
        //public static void Close()
        //{
        //    _Run = false;
        //    if (readThread != null)
        //    {
        //        if (readThread.IsAlive)
        //            readThread.Join();
        //    }
        //    if (_serialPort != null)
        //    {
        //        if (_serialPort.IsOpen)
        //        {
        //            _serialPort.Close();
        //            _serialPort.Dispose();
        //        }
        //    }
        //    if (_rxQueue != null)
        //    {
        //        _rxQueue.Dispose();
        //        rxQueueIsDisposed = true;
        //    }
        //}

        //---------------------- Private methods ------------------------------

        //public static void PacketCollector()
        //{
        //    while (_Run)
        //    {
        //        Thread.Sleep(1);
        //        if (_serialPort.IsOpen)
        //        {
        //            try
        //            {
        //                if (_serialPort.BytesToRead > 0)
        //                {
        //                    int received = _serialPort.ReadByte();
        //                    PacketDecoder((byte)received);
        //                }
        //            }
        //            catch (TimeoutException) { }
        //            catch (UnauthorizedAccessException) { }
        //        }
        //    }
        //}

        // Packet format is:
        // [STX1][STX2][STX3] [LEN] [DATA] [LRC]
        //  8bit  8bit  8bit  16bit N bit   8bit
        // LEN + DATA are used to calculate the LRC
        public void PacketDecoder(byte rxByte)
        {
            switch (_packetCollectorStates)
            {
            case PacketCollectorStates.STATE_STX_1:
                if (rxByte == STX_1)
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_2;
                }
                break;

            case PacketCollectorStates.STATE_STX_2:
                if (rxByte == STX_2)
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_3;
                }
                else
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_1;
                }
                break;

            case PacketCollectorStates.STATE_STX_3:
                if (rxByte == STX_3)
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_LEN;
                }
                else
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_1;
                }
                break;

            case PacketCollectorStates.STATE_STX_LEN:
                _calLRC           = 0;
                _rxBufferWriteIdx = 0;

                _calLRC               ^= rxByte;
                _rxPacket.Length       = rxByte;
                _packetCollectorStates = PacketCollectorStates.STATE_STX_DATA;

                break;

            case PacketCollectorStates.STATE_STX_DATA:
                _calLRC ^= rxByte;
                _rxPacket.Buffer[_rxBufferWriteIdx++] = rxByte;
                if (_rxBufferWriteIdx == _rxPacket.Length)
                {
                    _packetCollectorStates = PacketCollectorStates.STATE_STX_LRC;
                }

                break;

            case PacketCollectorStates.STATE_STX_LRC:
                if (_calLRC == rxByte)
                {
                    // Valid Packet received. Push it to queue
                    TL_Packet pct = new TL_Packet();
                    pct       = _rxPacket;
                    _rxPacket = new TL_Packet();
                    _rxQueue.Add(pct);
                }
                _packetCollectorStates = PacketCollectorStates.STATE_STX_1;
                break;

            default:
                _packetCollectorStates = PacketCollectorStates.STATE_STX_1;
                break;
            } // End of switch
        }     //End of PacketDecoder method