Skip to content

Commit 2129204

Browse files
committed
update modbus tcp
1 parent ce7de2b commit 2129204

File tree

6 files changed

+10
-33
lines changed

6 files changed

+10
-33
lines changed
89.5 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

SCADA/Program/ModbusDriver/ModbusRTUDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public byte[] ReadBytes(DeviceAddress address, ushort size)
490490
while (numBytesRead != frameBytes.Length)
491491
numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead);
492492
Array.Copy(frameBytes, 1, data, 0, data.Length);
493-
Thread.Sleep(10);
493+
Thread.Sleep(20);
494494
return data;
495495
}
496496
else

SCADA/Program/ModbusDriver/ModbusTCPDriver.cs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -358,43 +358,13 @@ public void Dispose()
358358
_grps.Clear();
359359
}
360360

361-
internal string GetErrorString(byte exception)
362-
{
363-
switch (exception)
364-
{
365-
case Modbus.excIllegalFunction:
366-
return "Constant for ModbusModbus.exception illegal function.";
367-
case Modbus.excIllegalDataAdr:
368-
return "Constant for ModbusModbus.exception illegal data address.";
369-
case Modbus.excIllegalDataVal:
370-
return "Constant for ModbusModbus.exception illegal data value.";
371-
case Modbus.excSlaveDeviceFailure:
372-
return "Constant for ModbusModbus.exception slave device failure.";
373-
case Modbus.excAck:
374-
return "Constant for ModbusModbus.exception acknowledge.";
375-
case Modbus.excSlaveIsBusy:
376-
return "Constant for ModbusModbus.exception slave is busy/booting up.";
377-
case Modbus.excGatePathUnavailable:
378-
return "Constant for ModbusModbus.exception gate path unavailable.";
379-
case Modbus.excExceptionNotConnected:
380-
return "Constant for ModbusModbus.exception not connected.";
381-
case Modbus.excExceptionConnectionLost:
382-
return "Constant for ModbusModbus.exception connection lost.";
383-
case Modbus.excExceptionTimeout:
384-
return "Constant for ModbusModbus.exception response timeout.";
385-
case Modbus.excExceptionOffset:
386-
return "Constant for ModbusModbus.exception wrong offset.";
387-
case Modbus.excSendFailt:
388-
return "Constant for ModbusModbus.exception send failt.";
389-
}
390-
return string.Empty;
391-
}
361+
392362

393363
internal void CallException(int id, byte function, byte exception)
394364
{
395365
if (tcpSynCl == null) return;
396366
if (OnError != null)
397-
OnError(this, new IOErrorEventArgs(GetErrorString(exception)));
367+
OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(exception)));
398368
}
399369

400370
public byte[] ReadBytes(DeviceAddress address, ushort size)
@@ -511,42 +481,49 @@ public int WriteBit(DeviceAddress address, bool bit)
511481

512482
public int WriteBits(DeviceAddress address, byte bits)
513483
{
484+
if (address.DBNumber != 3) return -1;
514485
var data = WriteSingleRegister(address.Area, address.Start, new byte[] { bits });
515486
return data == null ? -1 : 0;
516487
}
517488

518489
public int WriteInt16(DeviceAddress address, short value)
519490
{
491+
if (address.DBNumber != 3) return -1;
520492
var data = WriteSingleRegister(address.Area, address.Start, BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
521493
return data == null ? -1 : 0;
522494
}
523495

524496
public int WriteUInt16(DeviceAddress address, ushort value)
525497
{
498+
if (address.DBNumber != 3) return -1;
526499
var data = WriteSingleRegister(address.Area, address.Start, BitConverter.GetBytes((ushort)IPAddress.HostToNetworkOrder((short)value)));
527500
return data == null ? -1 : 0;
528501
}
529502

530503
public int WriteUInt32(DeviceAddress address, uint value)
531504
{
505+
if (address.DBNumber != 3) return -1;
532506
var data = WriteMultipleRegister(address.Area, address.Start, BitConverter.GetBytes((uint)IPAddress.HostToNetworkOrder((int)value)));
533507
return data == null ? -1 : 0;
534508
}
535509

536510
public int WriteInt32(DeviceAddress address, int value)
537511
{
512+
if (address.DBNumber != 3) return -1;
538513
var data = WriteMultipleRegister(address.Area, address.Start, BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)));
539514
return data == null ? -1 : 0;
540515
}
541516

542517
public int WriteFloat(DeviceAddress address, float value)
543518
{
519+
if (address.DBNumber != 3) return -1;
544520
var data = WriteMultipleRegister(address.Area, address.Start, BitConverter.GetBytes((int)value));
545521
return data == null ? -1 : 0;
546522
}
547523

548524
public int WriteString(DeviceAddress address, string str)
549525
{
526+
if (address.DBNumber != 3) return -1;
550527
var data = WriteMultipleRegister(address.Area, address.Start, Encoding.ASCII.GetBytes(str));
551528
return data == null ? -1 : 0;
552529
}

SCADA/dll/ModbusDriver.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)