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

            if (cmdMsg.byFunction >= 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.byFunction;
            byte[] byAddr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)cmdMsg.nStartAddr));
            byMsg[8]        = byAddr[0];
            byMsg[9]        = byAddr[1];
            nWriteDataIndex = 9 + 1;
            if (cmdMsg.byFunction >= 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);
        }
Exemplo n.º 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.byFunction == byREAD_COIL) || (input.byFunction == 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.byFunction == byREAD_HOLDING_REG) ||
                     (input.byFunction == byREAD_INPUT_REG))
            {
                byArr = new byte[nHead + 1 + input.nDataLength * 2 + nCRC];
            }
            else if ((input.byFunction == byWRITE_SINGLE_COIL) ||
                     (input.byFunction == byWRITE_MULTI_HOLDING_REG) ||
                     (input.byFunction == byWRITE_MULTI_COILS) ||
                     (input.byFunction == byWRITE_SINGLE_HOLDING_REG))
            {
                byArr = new byte[nHead + 4 + nCRC];
            }
        }
Exemplo n.º 3
0
 /// <summary>
 ///   RTU方式读取数据组帧
 /// </summary>
 private byte[] CreateReadHeader_RTU(InputModule cmdMsg)
 {
     byte[] byMsg = new byte[8];
     byMsg[0] = cmdMsg.bySlaveID;
     byMsg[1] = cmdMsg.byFunction;
     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);
 }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        private OutputModule Send(InputModule input, bool bIsRet = false)
        {
            m_output.nStartAddr  = input.nStartAddr;
            m_output.bySlaveID   = input.bySlaveID;
            m_output.nDataLength = input.nDataLength;
            m_output.byRecvData  = null;
            OutputModule output = null;

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

                    #endregion
                    #region TCP
                case emCommMode.TCP:
                    if (bReadOrWriteReg)
                    {
                        output = SendMessage_TCP_Sync(CreateReadMsg_TCP(input), bIsRet);
                    }
                    else
                    {
                        output = SendMessage_TCP_Sync(CreateWriteMsg_TCP(input), bIsRet);
                    }
                    break;

                    #endregion
                default:
                    break;
                }
            }
            catch (System.Exception ex)
            {
                if (event_MessageText != null)
                {
                    event_MessageText(ex.Message, emMsgType.Error);
                }
            }
            if (!bIsRet)
            {
                m_Event_SendMsg.Set();
            }
            return(output);
        }