/// <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()); }
/// <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); }
/// <summary> /// 接收数据回调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataReceived(object sender, SerialDataReceivedEventArgs e) { try { byte[] requetData = new byte[serialPort.BytesToRead]; serialPort.Read(requetData, 0, requetData.Length); var address = $"{requetData[5]}-{requetData[6]}-{requetData[7]}-{requetData[8]}"; byte[] tempData = new byte[requetData.Length - 3]; Buffer.BlockCopy(requetData, 1, tempData, 0, tempData.Length); var requetAsciiData = DataConvert.AsciiArrayToByteArray(tempData); var type = $"{requetData[3].ToString("X2")}{requetData[4].ToString("X2")}"; switch (type) { //读取线圈 case "3031": { var value = dataPersist.Read(address); var bytes = JsonConvert.DeserializeObject <byte[]>(value); byte[] data = null; //没有存储过的数据的,默认响应false if (bytes == null) { data = DataConvert.StringToByteArray("01 01 01 00"); } else { if (bytes[0].ToString("X2") != "3A" || bytes[bytes.Length - 2].ToString("X2") != "0D" || bytes[bytes.Length - 1].ToString("X2") != "0A") { throw new Exception("标记验证失败"); } byte[] asciiData = new byte[bytes.Length - 3]; Buffer.BlockCopy(bytes, 1, asciiData, 0, asciiData.Length); var byteDataArray = DataConvert.AsciiArrayToByteArray(asciiData); //true if (byteDataArray[4].ToString("X2") == "FF" && byteDataArray[5].ToString("X2") == "00") { data = DataConvert.StringToByteArray("01 01 01 01"); } else //false { data = DataConvert.StringToByteArray("01 01 01 00"); } } var dataString = string.Join("", LRC.GetLRC(data).Select(t => t.ToString("X2"))); var databyte = Encoding.ASCII.GetBytes(dataString); var responseData = new byte[databyte.Length + 3]; Buffer.BlockCopy(databyte, 0, responseData, 1, databyte.Length); responseData[0] = 0x3A; responseData[responseData.Length - 2] = 0x0D; responseData[responseData.Length - 1] = 0x0A; serialPort.Write(responseData, 0, responseData.Length); } break; //写入线圈 case "3035": { dataPersist.Write(address, JsonConvert.SerializeObject(requetData)); serialPort.Write(requetData, 0, requetData.Length); } break; //读取 case "3033": { var value = dataPersist.Read(address); var bytes = JsonConvert.DeserializeObject <byte[]>(value); byte[] data = null; if (bytes == null) { data = new byte[2 + 3]; data[2] = 2; //数据长度 } else { if (bytes[0].ToString("X2") != "3A" || bytes[bytes.Length - 2].ToString("X2") != "0D" || bytes[bytes.Length - 1].ToString("X2") != "0A") { throw new Exception("标记验证失败"); } byte[] asciiData = new byte[bytes.Length - 3]; Buffer.BlockCopy(bytes, 1, asciiData, 0, asciiData.Length); var byteDataArray = DataConvert.AsciiArrayToByteArray(asciiData); data = new byte[byteDataArray[6] + 3]; data[2] = byteDataArray[6]; //数据长度 Buffer.BlockCopy(byteDataArray, 7, data, 3, data.Length - 3); } data[0] = requetAsciiData[0]; //站号 data[1] = 0x03; //功能码 var dataString = string.Join("", LRC.GetLRC(data).Select(t => t.ToString("X2"))); var databyte = Encoding.ASCII.GetBytes(dataString); var responseData = new byte[databyte.Length + 3]; Buffer.BlockCopy(databyte, 0, responseData, 1, databyte.Length); responseData[0] = 0x3A; responseData[responseData.Length - 2] = 0x0D; responseData[responseData.Length - 1] = 0x0A; serialPort.Write(responseData, 0, responseData.Length); } break; //写入3130 对应十进制16 十六进制 10 case "3130": { dataPersist.Write(address, JsonConvert.SerializeObject(requetData)); serialPort.Write(requetData, 0, requetData.Length); } break; } } catch (Exception ex) { } }