Beispiel #1
0
        /// <summary>
        /// Инициализировать ADU запроса и рассчитать длину ответа
        /// </summary>
        public virtual void InitReqADU(byte devAddr, TransModes transMode)
        {
            if (ReqPDU != null)
            {
                int pduLen = ReqPDU.Length;

                switch (transMode)
                {
                case TransModes.RTU:
                    ReqADU    = new byte[pduLen + 3];
                    ReqADU[0] = devAddr;
                    ReqPDU.CopyTo(ReqADU, 1);
                    ushort crc = ModbusUtils.CalcCRC16(ReqADU, 0, pduLen + 1);
                    ReqADU[pduLen + 1] = (byte)(crc % 256);
                    ReqADU[pduLen + 2] = (byte)(crc / 256);
                    RespAduLen         = RespPduLen + 3;
                    break;

                case TransModes.ASCII:
                    byte[] aduBuf = new byte[pduLen + 2];
                    aduBuf[0] = devAddr;
                    ReqPDU.CopyTo(aduBuf, 1);
                    aduBuf[pduLen + 1] = ModbusUtils.CalcLRC(aduBuf, 0, pduLen + 1);

                    StringBuilder sbADU = new StringBuilder();
                    foreach (byte b in aduBuf)
                    {
                        sbADU.Append(b.ToString("X2"));
                    }

                    ReqADU     = Encoding.Default.GetBytes(sbADU.ToString());
                    ReqStr     = ModbusUtils.Colon + sbADU;
                    RespAduLen = RespPduLen + 2;
                    break;

                default:     // TransModes.TCP
                    ReqADU    = new byte[pduLen + 7];
                    ReqADU[0] = 0;
                    ReqADU[1] = 0;
                    ReqADU[2] = 0;
                    ReqADU[3] = 0;
                    ReqADU[4] = (byte)((pduLen + 1) / 256);
                    ReqADU[5] = (byte)((pduLen + 1) % 256);
                    ReqADU[6] = devAddr;
                    ReqPDU.CopyTo(ReqADU, 7);
                    RespAduLen = RespPduLen + 7;
                    break;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Выполнить запрос в режиме ASCII
        /// </summary>
        public bool AsciiRequest(DataUnit dataUnit)
        {
            if (!CheckConnection())
            {
                return(false);
            }

            bool result = false;

            // отправка запроса
            ExecWriteToLog(dataUnit.ReqDescr);
            Connection.WriteLine(dataUnit.ReqStr, out string logText);
            ExecWriteToLog(logText);

            // приём ответа
            string line = Connection.ReadLine(Timeout, out logText);

            ExecWriteToLog(logText);
            int lineLen = line == null ? 0 : line.Length;

            if (lineLen >= 3)
            {
                int aduLen = (lineLen - 1) / 2;

                if (aduLen == dataUnit.RespAduLen && lineLen % 2 == 1)
                {
                    // получение ADU ответа
                    byte[] aduBuf  = new byte[aduLen];
                    bool   parseOK = true;

                    for (int i = 0, j = 1; i < aduLen && parseOK; i++, j += 2)
                    {
                        try
                        {
                            aduBuf[i] = byte.Parse(line.Substring(j, 2), NumberStyles.HexNumber);
                        }
                        catch
                        {
                            ExecWriteToLog(ModbusPhrases.IncorrectSymbol);
                            parseOK = false;
                        }
                    }

                    if (parseOK)
                    {
                        if (aduBuf[aduLen - 1] == ModbusUtils.CalcLRC(aduBuf, 0, aduLen - 1))
                        {
                            // расшифровка ответа
                            string errMsg;

                            if (dataUnit.DecodeRespPDU(aduBuf, 1, aduLen - 2, out errMsg))
                            {
                                ExecWriteToLog(ModbusPhrases.OK);
                                result = true;
                            }
                            else
                            {
                                ExecWriteToLog(errMsg + "!");
                            }
                        }
                        else
                        {
                            ExecWriteToLog(ModbusPhrases.LrcError);
                        }
                    }
                }
                else
                {
                    ExecWriteToLog(ModbusPhrases.IncorrectAduLength);
                }
            }
            else
            {
                ExecWriteToLog(ModbusPhrases.CommErrorWithExclamation);
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Run ASCII query
        /// </summary>
        public bool AsciiRequest(DataUnit dataUnit)
        {
            if (!CheckConnection())
            {
                return(false);
            }

            var result = false;

            // sending request
            ExecWriteToLog(dataUnit.ReqDescr);
            Connection.WriteLine(dataUnit.ReqStr, out string logText);
            ExecWriteToLog(logText);

            // reception of the answer
            string line = Connection.ReadLine(Timeout, out logText);

            ExecWriteToLog(logText);
            int lineLen = line == null ? 0 : line.Length;

            if (lineLen >= 3)
            {
                int aduLen = (lineLen - 1) / 2;

                if (aduLen == dataUnit.RespAduLen && lineLen % 2 == 1)
                {
                    // getting ADU response
                    var aduBuf  = new byte[aduLen];
                    var parseOK = true;

                    for (int i = 0,
                         j = 1;
                         i < aduLen && parseOK;
                         i++, j += 2)
                    {
                        try {
                            aduBuf[i] = byte.Parse(line.Substring(j, 2), NumberStyles.HexNumber);
                        } catch {
                            ExecWriteToLog(ModbusPhrases.IncorrectSymbol);
                            parseOK = false;
                        }
                    }

                    if (parseOK)
                    {
                        if (aduBuf[aduLen - 1] == ModbusUtils.CalcLRC(aduBuf, 0, aduLen - 1))
                        {
                            // answer decryption
                            string errMsg;

                            if (dataUnit.DecodeRespPDU(aduBuf, 1, aduLen - 2, out errMsg))
                            {
                                ExecWriteToLog(ModbusPhrases.OK);
                                result = true;
                            }
                            else
                            {
                                ExecWriteToLog(errMsg + "!");
                            }
                        }
                        else
                        {
                            ExecWriteToLog(ModbusPhrases.LrcError);
                        }
                    }
                }
                else
                {
                    ExecWriteToLog(ModbusPhrases.IncorrectAduLength);
                }
            }
            else
            {
                ExecWriteToLog(ModbusPhrases.CommErrorWithExclamation);
            }

            return(result);
        }