Skip to content

Commit

Permalink
fixes MODBUS writeRegister and BCD encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
MAKOMO committed May 1, 2018
1 parent 42a4609 commit 275e56a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 108 deletions.
8 changes: 5 additions & 3 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

## Steps to Reproduce the Problem

1.
2.
3.
1.
2.
3.

## Specifications

Expand All @@ -19,3 +19,5 @@

Please attach your current Artisan settings file (as exported via menu Help >> Save Setings as *.aset) file.
Please attach any relevant Artisan *.alog profiles.

_Note that you need either add a `.txt` extension or zip the files before uploading. Otherwise you will receive a "Not a supported file type" error on uploading._
79 changes: 79 additions & 0 deletions src/artisanlib/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'''
Python 2.x/3.x Compatibility Layer
'''

import sys
import codecs
import binascii

if sys.version < '3':
def decs2string(x):
return "".join(chr(b) for b in x)
def arange(x):
return xrange(x) # @UndefinedVariable
def stringp(x):
return isinstance(x, basestring) # @UndefinedVariable
def uchr(x):
return unichr(x) # @UndefinedVariable
def o(x): # converts char to byte
return ord(x)
def u(x): # convert to unicode string
return unicode(x) # @UndefinedVariable
def d(x):
if x is not None:
try:
return codecs.unicode_escape_decode(x)[0]
except Exception:
return x
else:
return None
def encodeLocal(x):
if x is not None:
return codecs.unicode_escape_encode(unicode(x))[0] # @UndefinedVariable
else:
return None
def hex2int(h1,h2=""):
return int(binascii.hexlify(h1+h2),16)
def s2a(s):
return u(s).encode('ascii','ignore')
def cmd2str(c):
return c
def str2cmd(s):
return s2a(s)
else:
def decs2string(x):
if len(x) > 0:
return bytes(x)
else:
return b""
def arange(x):
return range(x)
def stringp(x):
return isinstance(x, str)
def uchr(x):
return chr(x)
def o(x): # converts char to byte
return x
def u(x): # convert to unicode string
return str(x)
def d(x):
if x is not None:
return codecs.unicode_escape_decode(x)[0]
else:
return None
def encodeLocal(x):
if x is not None:
return codecs.unicode_escape_encode(str(x))[0].decode("utf8")
else:
return None
def hex2int(h1,h2=None):
if h2 is not None:
return int(h1*256 + h2)
else:
return int(h1)
def str2cmd(s):
return bytes(s,"ascii")
def cmd2str(c):
return str(c,"latin1")
def s2a(s):
return s.encode('ascii','ignore').decode("ascii")
106 changes: 1 addition & 105 deletions src/artisanlib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import warnings
import string as libstring
import cgi
import codecs
import numpy
import subprocess
import shlex
Expand Down Expand Up @@ -160,6 +159,7 @@
from artisanlib.util import appFrozen
from artisanlib.suppress_errors import suppress_stdout_stderr
from artisanlib.s7port import s7port
from artisanlib.compat import *
from artisanlib.modbusport import modbusport


Expand Down Expand Up @@ -281,110 +281,6 @@ def PHIDGET_GAIN_VALUE(gv):
return BridgeGain.BRIDGE_GAIN_1 # no gain


if sys.version < '3':
def decs2string(x):
return "".join(chr(b) for b in x)
def arange(x):
return xrange(x) # @UndefinedVariable
def stringp(x):
return isinstance(x, basestring) # @UndefinedVariable
def uchr(x):
return unichr(x) # @UndefinedVariable
def o(x): # converts char to byte
return ord(x)
def u(x): # convert to unicode string
return unicode(x) # @UndefinedVariable
def d(x):
if x is not None:
try:
return codecs.unicode_escape_decode(x)[0]
except Exception:
return x
else:
return None
def encodeLocal(x):
if x is not None:
return codecs.unicode_escape_encode(unicode(x))[0] # @UndefinedVariable
else:
return None
def hex2int(h1,h2=""):
return int(binascii.hexlify(h1+h2),16)
def s2a(s):
return u(s).encode('ascii','ignore')
def cmd2str(c):
return c
def str2cmd(s):
return s2a(s)
else:
def decs2string(x):
if len(x) > 0:
return bytes(x)
else:
return b""
def arange(x):
return range(x)
def stringp(x):
return isinstance(x, str)
def uchr(x):
return chr(x)
def o(x): # converts char to byte
return x
def u(x): # convert to unicode string
return str(x)
def d(x):
if x is not None:
return codecs.unicode_escape_decode(x)[0]
else:
return None
def encodeLocal(x):
if x is not None:
return codecs.unicode_escape_encode(str(x))[0].decode("utf8")
else:
return None
def hex2int(h1,h2=None):
if h2 is not None:
return int(h1*256 + h2)
else:
return int(h1)
def str2cmd(s):
return bytes(s,"ascii")
def cmd2str(c):
return str(c,"latin1")
def s2a(s):
return s.encode('ascii','ignore').decode("ascii")

def convert_to_bcd(decimal):
''' Converts a decimal value to a bcd value

:param value: The decimal value to to pack into bcd
:returns: The number in bcd form
'''
place, bcd = 0, 0
while decimal > 0:
nibble = decimal % 10
bcd += nibble << place
if sys.version < '3':
decimal = decimal / 10
else:
decimal = decimal // 10
place += 4
return bcd


def convert_from_bcd(bcd):
''' Converts a bcd value to a decimal value

:param value: The value to unpack from bcd
:returns: The number in decimal form
'''
place, decimal = 1, 0
while bcd > 0:
nibble = bcd & 0xf
decimal += nibble * place
bcd >>= 4
place *= 10
return decimal

umlaute_dict = {
uchr(228): 'ae', # U+00E4 \xc3\xa4
uchr(246): 'oe', # U+00F6 \xc3\xb6
Expand Down
34 changes: 34 additions & 0 deletions src/artisanlib/modbusport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
from PyQt5.QtCore import QSemaphore
from PyQt5.QtWidgets import QApplication

from artisanlib.compat import *

def convert_to_bcd(decimal):
''' Converts a decimal value to a bcd value
:param value: The decimal value to to pack into bcd
:returns: The number in bcd form
'''
place, bcd = 0, 0
while decimal > 0:
nibble = decimal % 10
bcd += nibble << place
if sys.version < '3':
decimal = decimal / 10
else:
decimal = decimal // 10
place += 4
return bcd


def convert_from_bcd(bcd):
''' Converts a bcd value to a decimal value
:param value: The value to unpack from bcd
:returns: The number in decimal form
'''
place, decimal = 1, 0
while bcd > 0:
nibble = bcd & 0xf
decimal += nibble * place
bcd >>= 4
place *= 10
return decimal

def getBinaryPayloadBuilder(byteorderLittle=True,wordorderLittle=False):
import pymodbus.version as pymodbus_version
from pymodbus.constants import Endian
Expand Down

0 comments on commit 275e56a

Please sign in to comment.