Beispiel #1
0
        /// <summary>
        ///  TCP下写入数据组帧
        /// </summary>
        private byte[] CreateWriteMsg_TCP(InputModule cmdMsg)
        {
            byte[] byMsg           = null;
            int    nWriteDataIndex = 0;

            if (cmdMsg.byFuntion >= byWRITE_MULTI_COILS)
            {
                byMsg = new byte[10 + cmdMsg.byWriteData.Length + 3];
            }
            else
            {
                byMsg = new byte[10 + cmdMsg.byWriteData.Length];
            }
            if (m_nTCPCount++ == 65535)
            {
                m_nTCPCount = 0;
            }
            //事务标识符
            byte[] byCount = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)m_nTCPCount));
            byMsg[0] = byCount[0];
            byMsg[1] = byCount[1];
            //协议标识符
            byMsg[2] = 0;
            byMsg[3] = 0;
            //长度
            byte[] byLength = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)(byMsg.Length - 6)));
            byMsg[4] = byLength[0];
            byMsg[5] = byLength[1];

            byMsg[6] = cmdMsg.bySlaveID;
            byMsg[7] = cmdMsg.byFuntion;
            byte[] byAddr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)cmdMsg.nStartAddr));
            byMsg[8]        = byAddr[0];
            byMsg[9]        = byAddr[1];
            nWriteDataIndex = 9 + 1;
            if (cmdMsg.byFuntion >= byWRITE_MULTI_COILS)
            {
                byte[] _cnt = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)cmdMsg.nDataLength));
                byMsg[10]       = _cnt[0];              // Number of bytes
                byMsg[11]       = _cnt[1];              // Number of bytes
                byMsg[12]       = Convert.ToByte(cmdMsg.byWriteData.Length);
                nWriteDataIndex = 12 + 1;
            }
            Array.Copy(cmdMsg.byWriteData, 0, byMsg, nWriteDataIndex, cmdMsg.byWriteData.Length);
            //定义接收buffer大小
            SetRecvBufSize(ref m_byTCPDataRecv, cmdMsg);
            return(byMsg);
        }
Beispiel #2
0
        private void SetRecvBufSize(ref byte[] byArr, InputModule input)
        {
            if (byArr != null)
            {
                Array.Clear(byArr, 0, byArr.Length);
                byArr = null;
            }
            int nHead = 20, nCRC = 0;

            switch (m_nRunMode)
            {
            case emCommMode.TCP:
                nHead = 8;
                break;

            case emCommMode.RTU:
                nHead = 2;
                nCRC  = 2;
                break;

            case emCommMode.ASCII:
                break;

            default:
                break;
            }
            if ((input.byFuntion == byREAD_COIL) || (input.byFuntion == byREAD_DISCRETE_INPUTS))
            {
                int nCount, nTemp = input.nDataLength;
                nCount = ((nTemp % 8) == 0) ? (nTemp / 8) : ((nTemp - (nTemp % 8)) / 8 + 1);
                byArr  = new byte[nHead + 1 + nCount + nCRC];
            }
            else if ((input.byFuntion == byREAD_HOLDING_REG) ||
                     (input.byFuntion == byREAD_INPUT_REG))
            {
                byArr = new byte[nHead + 1 + input.nDataLength * 2 + nCRC];
            }
            else if ((input.byFuntion == byWRITE_SINGLE_COIL) ||
                     (input.byFuntion == byWRITE_MULTI_HOLDING_REG) ||
                     (input.byFuntion == byWRITE_MULTI_COILS) ||
                     (input.byFuntion == byWRITE_SINGLE_HOLDING_REG))
            {
                byArr = new byte[nHead + 4 + nCRC];
            }
        }
Beispiel #3
0
 /// <summary>
 ///   RTU方式读取数据组帧
 /// </summary>
 private byte[] CreateReadHeader_RTU(InputModule cmdMsg)
 {
     byte[] byMsg = new byte[8];
     byMsg[0] = cmdMsg.bySlaveID;
     byMsg[1] = cmdMsg.byFuntion;
     byte[] byAddr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)cmdMsg.nStartAddr));
     byMsg[2] = byAddr[0];
     byMsg[3] = byAddr[1];
     byte[] byDataLength = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)cmdMsg.nDataLength));
     byMsg[4] = byDataLength[0];
     byMsg[5] = byDataLength[1];
     byte[] CRC = CRC16(byMsg);
     byMsg[byMsg.Length - 2] = CRC[0];
     byMsg[byMsg.Length - 1] = CRC[1];
     //定义接收buffer大小
     SetRecvBufSize(ref m_byRtuDataRecv, cmdMsg);
     return(byMsg);
 }
Beispiel #4
0
        private void Send(InputModule input)
        {
            Thread thd = new Thread(new ThreadStart(
                                        delegate
            {
                m_output.nStartAddr = input.nStartAddr;
                m_output.byFuntion  = input.byFuntion;

                bool bReadOrWriteReg;
                string strException;
                if ((input.byFuntion == byREAD_COIL) ||
                    (input.byFuntion == byREAD_DISCRETE_INPUTS) ||
                    (input.byFuntion == byREAD_HOLDING_REG) ||
                    (input.byFuntion == byREAD_INPUT_REG))
                {
                    bReadOrWriteReg = true;
                }
                else if ((input.byFuntion == byWRITE_SINGLE_COIL) ||
                         (input.byFuntion == byWRITE_MULTI_HOLDING_REG) ||
                         (input.byFuntion == byWRITE_MULTI_COILS) ||
                         (input.byFuntion == byWRITE_SINGLE_HOLDING_REG))
                {
                    bReadOrWriteReg = false;
                }
                else
                {
                    if (event_MessageText != null)
                    {
                        event_MessageText("检测到不支持的功能码!", emMsgType.Error, 0, 0);
                    }
                    return;
                }
                try
                {
                    switch (m_nRunMode)
                    {
                    case emCommMode.RTU:
                        if (bReadOrWriteReg)
                        {
                            if ((strException = SendMessage_SP(CreateReadHeader_RTU(input))) != null)
                            {
                                if (event_MessageText != null)
                                {
                                    event_MessageText(strException, emMsgType.Error, 0, 0);
                                }
                            }
                        }
                        else
                        {
                            if ((strException = SendMessage_SP(CreateWritrHeader_RTU(input))) != null)
                            {
                                if (event_MessageText != null)
                                {
                                    event_MessageText(strException, emMsgType.Error, 0, 0);
                                }
                            }
                        }
                        break;

                    case emCommMode.TCP:
                        if (bReadOrWriteReg)
                        {
                            if ((strException = SendMessage_TCP_Sync(CreateReadMsg_TCP(input), Convert.ToInt32(input.byFuntion), input.nStartAddr)) != null)
                            {
                                if (event_MessageText != null)
                                {
                                    event_MessageText(strException, emMsgType.Error, 0, 0);
                                }
                            }
                        }
                        else
                        {
                            if ((strException = SendMessage_TCP_Sync(CreateWriteMsg_TCP(input), Convert.ToInt32(input.byFuntion), input.nStartAddr)) != null)
                            {
                                if (event_MessageText != null)
                                {
                                    event_MessageText(strException, emMsgType.Error, 0, 0);
                                }
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch (System.Exception ex)
                {
                    if (event_MessageText != null)
                    {
                        event_MessageText(ex.Message, emMsgType.Error, 0, 0);
                    }
                }
            }
                                        ));

            thd.IsBackground = true;
            thd.Start();
        }