Beispiel #1
0
 private void RequestToSend()
 {
     m_State = CommuState.Send_Request;
     SendHandshakeCode(HandshakeCodes.ENQ);
     //1. NO EOT AFTER BID (ENQ)
     Start_T2_Timer();
 }
Beispiel #2
0
        public SecsIDriver(SecsIMessageParser parser)
            : base(parser)
        {
            m_QueueSendingBlock = new Queue <byte[]>();

            m_RecvBuff = new byte[1024]; //actually use only 258 bytes

            m_MsgParser = new SecsIMessageParser();

            m_Port = new SerialPort();
            m_Port.DataReceived += new SerialDataReceivedEventHandler(m_Port_DataReceived);

            m_ENQ = new byte[] { 5 };
            m_EOT = new byte[] { 4 };
            m_ACK = new byte[] { 6 };
            m_NAK = new byte[] { 7 };

            m_T1_Timer          = new Timer();
            m_T1_Timer.Elapsed += new ElapsedEventHandler(T1_Elapsed);
            m_T2_Timer          = new Timer();
            m_T2_Timer.Elapsed += new ElapsedEventHandler(T2_Elapsed);

            m_T1_Interval = 0.5d;
            m_T2_Interval = 10d;
            m_T3_Interval = 45;

            m_RTY = 3;

            m_State = CommuState.Idle;

            m_Contention = SecsIContentionType.Slave;

            m_MultiBlockList = new List <byte[]>();

            m_T3Hash = new Hashtable();
        }
Beispiel #3
0
        private void OnDataArrival(byte[] buffer, int length)
        {
            Array.Copy(buffer, 0, m_RecvBuff, m_RecvBuffIndex, length);
            m_RecvBuffIndex += length;

            System.Diagnostics.Debug.Print("RecvBuffIndex := " + m_RecvBuffIndex.ToString());

            if (m_RecvBuffIndex > 0)
            {
                //LTH[0] << length of data 1 + CHECKSUM 2
                byte lth = m_RecvBuff[0];

                Stop_T1_Timer();

                //wait until end of block
                if (lth + 3 == m_RecvBuffIndex)
                {
                    ushort checksumCal = 0;
                    for (int i = 1; i < m_RecvBuffIndex - 2; i++)
                    {
                        checksumCal += m_RecvBuff[i];
                    }

                    ushort checksumRecv = BitConverter.ToUInt16(
                        new byte[] {
                        m_RecvBuff[m_RecvBuffIndex - 1],
                        m_RecvBuff[m_RecvBuffIndex - 2]
                    }, 0);

                    if (checksumCal == checksumRecv)
                    {
                        //change State to idle
                        m_State = CommuState.Idle;
                        //if CHECK SUM is correct
                        SendHandshakeCode(HandshakeCodes.ACK);

                        byte[] header = new byte[10];

                        Array.Copy(m_RecvBuff, 1, header, 0, header.Length);

                        System.Diagnostics.Debug.Print("HEAD {0} bytes ", header.Length);

                        byte[] transactionIdBytes = new byte[4];
                        Array.Copy(header, 6, transactionIdBytes, 0, 4);
                        Array.Reverse(transactionIdBytes);

                        //transaction Id alway same in Multi-Block message
                        uint tid = BitConverter.ToUInt32(transactionIdBytes, 0);
                        System.Diagnostics.Debug.Print("Transaction ID {0} bytes ", tid);

                        int bodyLength = lth - 10;

                        if (bodyLength > 0)
                        {
                            byte[] body = new byte[bodyLength];
                            Array.Copy(m_RecvBuff, 11, body, 0, bodyLength);

                            System.Diagnostics.Debug.Print("BODY {0} bytes ", body.Length);

                            m_MultiBlockList.Add(body);
                        }

                        if ((header[4] & 0x80) == 0x80) //last block
                        {
                            //build up all message
                            int totalBytes = 10; //header

                            foreach (byte[] bytes in m_MultiBlockList)
                            {
                                totalBytes += bytes.Length;
                            }

                            byte[] data  = new byte[totalBytes];
                            int    index = 0;

                            Array.Copy(header, 0, data, 0, header.Length);
                            index += header.Length;

                            foreach (byte[] bytes in m_MultiBlockList)
                            {
                                Array.Copy(bytes, 0, data, index, bytes.Length);
                                index += bytes.Length;
                            }

                            m_MultiBlockList.Clear();

                            ProcessSecsMessageBytes(data);
                        }
                    }
                    else
                    {
                        //send NAK for check sum error
                        SendHandshakeCode(HandshakeCodes.NAK);
                    }

                    //clear buffer
                    Array.Clear(m_RecvBuff, 0, m_RecvBuffIndex);
                    m_RecvBuffIndex = 0;
                }
                else
                {
                    Start_T1_Timer();
                }
            } //if (m_RecvBuffIndex > 0)
        }
Beispiel #4
0
        private void m_Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            lock (m_Locker)
            {
                byte[] buff   = new byte[m_Port.BytesToRead];
                int    length = m_Port.Read(buff, 0, buff.Length);

                System.Diagnostics.Debug.Print("DataReceived {0} bytes , State {1}", length, m_State);
                System.Diagnostics.Debug.Print("Hex : " + GetHexString(buff));

                if (length == 0)
                {
                    return;
                }

                switch (m_State)
                {
                case CommuState.Idle:
                    if (length == 1)
                    {
                        System.Diagnostics.Debug.Print("RecvControlChar := " + (HandshakeCodes)buff[0]);
                        if ((HandshakeCodes)buff[0] == HandshakeCodes.ENQ)
                        {
                            m_State = CommuState.Receiving;
                            SendHandshakeCode(HandshakeCodes.EOT);
                            //3. NO MESSAGE AFTER EOT
                            Start_T1_Timer();
                        }
                    }
                    break;

                case CommuState.Receiving:
                    Stop_T2_Timer();
                    OnDataArrival(buff, length);

                    break;

                case CommuState.Send_Request:
                    System.Diagnostics.Debug.Print("RecvControlChar := " + (HandshakeCodes)buff[0]);
                    if ((HandshakeCodes)buff[0] == HandshakeCodes.EOT)
                    {
                        Stop_T2_Timer();
                        m_State = CommuState.Sending;
                        StartSend();
                    }
                    break;

                case CommuState.Sending:
                    System.Diagnostics.Debug.Print("RecvControlChar := " + (HandshakeCodes)buff[0]);
                    if ((HandshakeCodes)buff[0] == HandshakeCodes.ACK)
                    {
                        Stop_T2_Timer();
                        m_State = CommuState.Idle;
                    }
                    else if ((HandshakeCodes)buff[0] == HandshakeCodes.NAK)
                    {
                        Stop_T2_Timer();
                        m_State = CommuState.Idle;     //message failed
                    }
                    break;
                }
            }

            if (m_State == CommuState.Idle && m_QueueSendingBlock.Count > 0)
            {
                RequestToSend();
            }
        }
Beispiel #5
0
 private void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     m_T1_Timer.Stop();
     m_State = CommuState.Idle;
     SendHandshakeCode(HandshakeCodes.NAK);
 }