@@ -124,6 +124,10 @@ def __exit__(self, klass, value, traceback):
124
124
self .close ()
125
125
126
126
def idle_time (self ):
127
+ """
128
+ Bus Idle Time to initiate next transaction
129
+ :return: time stamp
130
+ """
127
131
if self .last_frame_end is None or self .silent_interval is None :
128
132
return 0
129
133
return self .last_frame_end + self .silent_interval
@@ -286,11 +290,11 @@ def __repr__(self):
286
290
"port={self.port}, timeout={self.timeout}>"
287
291
).format (self .__class__ .__name__ , hex (id (self )), self = self )
288
292
289
-
290
-
291
293
# --------------------------------------------------------------------------- #
292
294
# Modbus UDP Client Transport Implementation
293
295
# --------------------------------------------------------------------------- #
296
+
297
+
294
298
class ModbusUdpClient (BaseModbusClient ):
295
299
""" Implementation of a modbus udp client
296
300
"""
@@ -386,10 +390,14 @@ def __repr__(self):
386
390
# --------------------------------------------------------------------------- #
387
391
# Modbus Serial Client Transport Implementation
388
392
# --------------------------------------------------------------------------- #
393
+
394
+
389
395
class ModbusSerialClient (BaseModbusClient ):
390
396
""" Implementation of a modbus serial client
391
397
"""
392
398
state = ModbusTransactionState .IDLE
399
+ inter_char_timeout = 0
400
+ silent_interval = 0
393
401
394
402
def __init__ (self , method = 'ascii' , ** kwargs ):
395
403
""" Initialize a serial client instance
@@ -413,18 +421,21 @@ def __init__(self, method='ascii', **kwargs):
413
421
BaseModbusClient .__init__ (self , self .__implementation (method , self ),
414
422
** kwargs )
415
423
416
- self .port = kwargs .get ('port' , 0 )
417
- self .stopbits = kwargs .get ('stopbits' , Defaults .Stopbits )
418
- self .bytesize = kwargs .get ('bytesize' , Defaults .Bytesize )
419
- self .parity = kwargs .get ('parity' , Defaults .Parity )
420
- self .baudrate = kwargs .get ('baudrate' , Defaults .Baudrate )
421
- self .timeout = kwargs .get ('timeout' , Defaults .Timeout )
422
- self .last_frame_end = None
424
+ self ._port = kwargs .get ('port' , 0 )
425
+ self ._stopbits = kwargs .get ('stopbits' , Defaults .Stopbits )
426
+ self ._bytesize = kwargs .get ('bytesize' , Defaults .Bytesize )
427
+ self ._parity = kwargs .get ('parity' , Defaults .Parity )
428
+ self ._baudrate = kwargs .get ('baudrate' , Defaults .Baudrate )
429
+ self ._timeout = kwargs .get ('timeout' , Defaults .Timeout )
430
+ self .last_frame_end = 0
423
431
if self .method == "rtu" :
424
- if self .baudrate > 19200 :
432
+ if self ._baudrate > 19200 :
433
+ self ._t0 = self .inter_char_timeout = 750.0 / 1000000 #Micro
425
434
self .silent_interval = 1.75 / 1000 # ms
426
435
else :
427
- self .silent_interval = 3.5 * (1 + 8 + 2 ) / self .baudrate
436
+ self ._t0 = float ((1 + 8 + 2 )) / self ._baudrate
437
+ self .inter_char_timeout = 1.5 * self ._t0
438
+ self .silent_interval = 3.5 * self ._t0
428
439
self .silent_interval = round (self .silent_interval , 6 )
429
440
430
441
@staticmethod
@@ -453,16 +464,17 @@ def connect(self):
453
464
if self .socket :
454
465
return True
455
466
try :
456
- self .socket = serial .Serial (port = self .port ,
457
- timeout = self .timeout ,
458
- bytesize = self .bytesize ,
459
- stopbits = self .stopbits ,
460
- baudrate = self .baudrate ,
461
- parity = self .parity )
467
+ self .socket = serial .Serial (port = self ._port ,
468
+ timeout = self ._timeout ,
469
+ bytesize = self ._bytesize ,
470
+ stopbits = self ._stopbits ,
471
+ baudrate = self ._baudrate ,
472
+ parity = self ._parity )
462
473
except serial .SerialException as msg :
463
474
_logger .error (msg )
464
475
self .close ()
465
476
if self .method == "rtu" :
477
+ self .socket .interCharTimeout = self .inter_char_timeout
466
478
self .last_frame_end = None
467
479
return self .socket is not None
468
480
@@ -512,8 +524,8 @@ def _send(self, request):
512
524
return 0
513
525
514
526
def _wait_for_data (self ):
515
- if self .timeout is not None and self .timeout != 0 :
516
- condition = partial (lambda start , timeout : (time .time () - start ) <= timeout , timeout = self .timeout )
527
+ if self ._timeout is not None and self ._timeout != 0 :
528
+ condition = partial (lambda start , timeout : (time .time () - start ) <= timeout , timeout = self ._timeout )
517
529
else :
518
530
condition = partial (lambda dummy1 , dummy2 : True , dummy2 = None )
519
531
start = time .time ()
@@ -550,7 +562,7 @@ def __str__(self):
550
562
551
563
:returns: The string representation
552
564
"""
553
- return "ModbusSerialClient(%s baud[%s])" % (self .method , self .baudrate )
565
+ return "ModbusSerialClient(%s baud[%s])" % (self .method , self ._baudrate )
554
566
555
567
def __repr__ (self ):
556
568
return (
0 commit comments