Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add 485 deadtime enforcement #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 71 additions & 38 deletions sunspec/core/modbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import struct
import serial
import sys
import time

try:
import xml.etree.ElementTree as ET
Expand All @@ -34,6 +35,10 @@

import sunspec.core.modbus.mbmap as mbmap


deadtime_clock = getattr(time, 'monotonic', time.clock)


PARITY_NONE = 'N'
PARITY_EVEN = 'E'

Expand All @@ -56,6 +61,16 @@ class ModbusClientTimeout(ModbusClientError):
class ModbusClientException(ModbusClientError):
pass


def wait_until(target, clock):
while True:
remaining = target - clock()
if remaining > 0:
time.sleep(remaining)
else:
break


def modbus_rtu_client(name=None, baudrate=None, parity=None):

global modbus_rtu_clients
Expand Down Expand Up @@ -148,6 +163,8 @@ def __init__(self, name='/dev/ttyUSB0', baudrate=9600, parity=None):
self.timeout = .5
self.write_timeout = .5
self.devices = {}
self.deadtime = 44 / int(self.baudrate)
self.last_receive_completion = -self.deadtime

self.open()

Expand Down Expand Up @@ -235,32 +252,40 @@ def _read(self, slave_id, addr, count, op=FUNC_READ_HOLDING, trace_func=None):
s += '%02X' % (ord(c))
trace_func(s)

wait_until(
target=self.last_receive_completion + self.deadtime,
clock=deadtime_clock,
)

self.serial.flushInput()
try:
self.serial.write(req)
except Exception as e:
raise ModbusClientError('Serial write error: %s' % str(e))

while len_remaining > 0:
c = self.serial.read(len_remaining)
if type(c) == bytes and sys.version_info > (3,):
temp = ""
for i in c:
temp += chr(i)
c = temp
try:
while len_remaining > 0:
c = self.serial.read(len_remaining)
if type(c) == bytes and sys.version_info > (3,):
temp = ""
for i in c:
temp += chr(i)
c = temp

len_read = len(c);
if len_read > 0:
resp += c
len_remaining -= len_read
if len_found is False and len(resp) >= 5:
if not (ord(resp[1]) & 0x80):
len_remaining = (ord(resp[2]) + 5) - len(resp)
len_found = True
else:
except_code = ord(resp[2])
else:
raise ModbusClientTimeout('Response timeout')
len_read = len(c);
if len_read > 0:
resp += c
len_remaining -= len_read
if len_found is False and len(resp) >= 5:
if not (ord(resp[1]) & 0x80):
len_remaining = (ord(resp[2]) + 5) - len(resp)
len_found = True
else:
except_code = ord(resp[2])
else:
raise ModbusClientTimeout('Response timeout')
finally:
self.last_receive_completion = deadtime_clock()

if trace_func:
s = '%s:%s[addr=%s] <--' % (self.name, str(slave_id), addr)
Expand Down Expand Up @@ -354,6 +379,11 @@ def _write(self, slave_id, addr, data, trace_func=None):
s += '%02X' % (ord(c))
trace_func(s)

wait_until(
target=self.last_receive_completion + self.deadtime,
clock=deadtime_clock,
)

self.serial.flushInput()
try:
if sys.version_info > (3,):
Expand All @@ -362,25 +392,28 @@ def _write(self, slave_id, addr, data, trace_func=None):
except Exception as e:
raise ModbusClientError('Serial write error: %s' % str(e))

while len_remaining > 0:
c = self.serial.read(len_remaining)
if type(c) == bytes and sys.version_info > (3,):
temp = ""
for i in c:
temp += chr(i)
c = temp
len_read = len(c);
if len_read > 0:
resp += c
len_remaining -= len_read
if len_found is False and len(resp) >= 5:
if not (ord(resp[1]) & 0x80):
len_remaining = 8 - len(resp)
len_found = True
else:
except_code = ord(resp[2])
else:
raise ModbusClientTimeout('Response timeout')
try:
while len_remaining > 0:
c = self.serial.read(len_remaining)
if type(c) == bytes and sys.version_info > (3,):
temp = ""
for i in c:
temp += chr(i)
c = temp
len_read = len(c);
if len_read > 0:
resp += c
len_remaining -= len_read
if len_found is False and len(resp) >= 5:
if not (ord(resp[1]) & 0x80):
len_remaining = 8 - len(resp)
len_found = True
else:
except_code = ord(resp[2])
else:
raise ModbusClientTimeout('Response timeout')
finally:
self.last_receive_completion = deadtime_clock()

if trace_func:
s = '%s:%s[addr=%s] <--' % (self.name, str(slave_id), addr)
Expand Down