/// <summary> /// 初始化modbusrtu对象 /// </summary> private static void InitModbusRtu() { if (rtu != null) { if (rtu.IsOpen()) rtu.Close(); } else { rtu = new ModbusRtu(); } try { rtu.Station = _station; rtu.SerialPortInni(sp => { sp.PortName = _com; sp.BaudRate = _baudRate; sp.DataBits = _dataBit; sp.StopBits = _stopBits; sp.Parity = _parity; sp.ReadTimeout = 500; sp.WriteTimeout = 500; }); // rtu.Open(); // 打开 } catch (Exception ex) { MsgChannl.SendMsg(ex.Message); } }
public bool Write(string address, ushort value) { HslCommunication.OperateResult rst; rst = rtu.Write(address, value); if (rst.IsSuccess) { return true; } MsgChannl.SendMsg(rst.Message); return false; }
public string ReadCoil(string address) { HslCommunication.OperateResult<bool> rst; rst = rtu.ReadCoil(address); if (rst.IsSuccess) { return rst.Content ? "ON" : "OFF"; } MsgChannl.SendMsg(rst.Message); return "Exception"; }
public uint ReadUInt32(string address) { HslCommunication.OperateResult<uint> rst; rst = rtu.ReadUInt32(address); if (rst.IsSuccess) { return rst.Content; } MsgChannl.SendMsg(rst.Message); return 0; }
public short ReadInt16(string address) { HslCommunication.OperateResult<short> rst; rst =rtu.ReadInt16(address); if (rst.IsSuccess) //判断是否成功 { return rst.Content; } MsgChannl.SendMsg(rst.Message);//未成功就发送错误消息 return 0; }
public double ReadDouble(string address) { HslCommunication.OperateResult<double> rst; rst = rtu.ReadDouble(address); if (rst.IsSuccess) { return rst.Content; } MsgChannl.SendMsg(rst.Message); return 0; }
public bool Write(string address, uint value) { HslCommunication.OperateResult rst; rst = TCP.Write(address, value); if (rst.IsSuccess) { return(true); } MsgChannl.SendMsg(rst.Message); return(false); }
public ushort ReadUInt16(string address) { HslCommunication.OperateResult <ushort> rst; rst = TCP.ReadUInt16(address); if (rst.IsSuccess) { return(rst.Content); } MsgChannl.SendMsg(rst.Message); return(0); }
public float ReadFloat(string address) { HslCommunication.OperateResult <float> rst; rst = TCP.ReadFloat(address); if (rst.IsSuccess) { return(rst.Content); } MsgChannl.SendMsg(rst.Message); return(0); }
public string ContentTest(string sendContent) { byte[] data = HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(sendContent); string rst = ""; HslCommunication.OperateResult <byte[]> read = TCP.ReadFromCoreServer(data); if (read.IsSuccess) { // 获取结果,并转化为Hex字符串,方便显示 rst = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content, ' '); } else { MsgChannl.SendMsg(read.Message); } return(rst); }
public string ContentTest(string sendContent) { string rst = ""; try { rtu.ReceiveTimeout = 500; OperateResult<byte[]> read = rtu.ReadBase(HslCommunication.Serial.SoftCRC16.CRC16(HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(sendContent))); if (read.IsSuccess) { rst = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content, ' '); } else { MsgChannl.SendMsg("读取失败:" + read.Message); } } catch (Exception ex) { MsgChannl.SendMsg("读取失败:" + ex.Message); } return rst; }