예제 #1
0
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="address"></param>
        /// <param name="values"></param>
        /// <param name="stationNumber"></param>
        /// <param name="functionCode"></param>
        /// <returns></returns>
        public override Result Write(string address, byte[] values, byte stationNumber = 1, byte functionCode = 16)
        {
            if (isAutoOpen)
            {
                Connect();
            }

            var result = new Result();

            try
            {
                values = values.ByteFormatting(format);
                var command = GetWriteCommand(address, values, stationNumber, functionCode);

                var commandAscii = DataConvert.ByteArrayToAsciiArray(LRC.GetLRC(command));
                var finalCommand = new byte[commandAscii.Length + 3];
                Buffer.BlockCopy(commandAscii, 0, finalCommand, 1, commandAscii.Length);
                finalCommand[0] = 0x3A;
                finalCommand[finalCommand.Length - 2] = 0x0D;
                finalCommand[finalCommand.Length - 1] = 0x0A;

                result.Requst = string.Join(" ", finalCommand.Select(t => t.ToString("X2")));
                var responsePackage = SendPackage(finalCommand);
                if (!responsePackage.Any())
                {
                    result.IsSucceed = false;
                    result.Err       = "响应结果为空";
                    return(result.EndTime());
                }

                byte[] resultLRC = new byte[responsePackage.Length - 3];
                Array.Copy(responsePackage, 1, resultLRC, 0, resultLRC.Length);
                var resultByte = DataConvert.AsciiArrayToByteArray(resultLRC);
                if (!LRC.CheckLRC(resultByte))
                {
                    result.IsSucceed = false;
                    result.Err       = "响应结果LRC验证失败";
                    //return result.EndTime();
                }

                result.Response = string.Join(" ", responsePackage.Select(t => t.ToString("X2")));
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err       = ex.Message;
                result.ErrList.Add(ex.Message);
            }
            finally
            {
                if (isAutoOpen)
                {
                    Dispose();
                }
            }
            return(result.EndTime());
        }
예제 #2
0
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="address">寄存器起始地址</param>
        /// <param name="stationNumber">站号</param>
        /// <param name="functionCode">功能码</param>
        /// <param name="readLength">读取长度</param>
        /// <returns></returns>
        public Result <byte[]> Read(string address, byte stationNumber = 1, byte functionCode = 3, ushort readLength = 1)
        {
            if (isAutoOpen)
            {
                Connect();
            }

            var result = new Result <byte[]>();

            try
            {
                //获取命令(组装报文)
                byte[] command    = GetReadCommand(address, stationNumber, functionCode, readLength);
                var    commandLRC = DataConvert.ByteArrayToAsciiArray(LRC.GetLRC(command));

                var finalCommand = new byte[commandLRC.Length + 3];
                Buffer.BlockCopy(commandLRC, 0, finalCommand, 1, commandLRC.Length);
                finalCommand[0] = 0x3A;
                finalCommand[finalCommand.Length - 2] = 0x0D;
                finalCommand[finalCommand.Length - 1] = 0x0A;

                result.Requst = string.Join(" ", finalCommand.Select(t => t.ToString("X2")));

                //发送命令并获取响应报文
                var responsePackage = SendPackage(finalCommand);
                if (!responsePackage.Any())
                {
                    result.IsSucceed = false;
                    result.Err       = "响应结果为空,请检查是否连上了服务端";
                    return(result);
                }

                byte[] resultLRC = new byte[responsePackage.Length - 3];
                Array.Copy(responsePackage, 1, resultLRC, 0, resultLRC.Length);
                var resultByte = DataConvert.AsciiArrayToByteArray(resultLRC);
                if (!LRC.CheckLRC(resultByte))
                {
                    result.IsSucceed = false;
                    result.Err       = "响应结果LRC验证失败";
                    return(result);
                }
                var resultData = new byte[resultByte[2]];
                Buffer.BlockCopy(resultByte, 3, resultData, 0, resultData.Length);
                result.Response = string.Join(" ", responsePackage.Select(t => t.ToString("X2")));
                //4 获取响应报文数据(字节数组形式)
                result.Value = resultData.Reverse().ToArray();
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err       = ex.Message;
                result.ErrList.Add(ex.Message);
            }
            finally
            {
                if (isAutoOpen)
                {
                    Dispose();
                }
            }
            return(result);
        }