modbus/tcp 方式,在调用 master.WriteMultipleRegisters(slave, (ushort)(startAddress + 40), writeIn);出现不允许对非连接的套接字执行此操作;
client = new TcpClient(ConfigurationManager.AppSettings["PLCIP"], 502);
master = ModbusIpMaster.CreateIp(client);
foreach(var item in DevcieInfoList)
{
flag = false;
ushort startAddress ;
ushort numInputs = 44;
Byte slave ;
slave = Convert.ToByte(item.DeviceId);
startAddress = Convert.ToUInt16(item.FirstAddr);
ushort[] inputs = master.ReadHoldingRegisters(slave, startAddress, numInputs);
if (item.Status == "新建立")
master.WriteMultipleRegisters(slave, (ushort)(startAddress + 40), 1);
}
当循环执行到第二次时,会出现问题的报错。那modbus/tcp在操作之前怎么判断是否连接没有断开?