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

Enhancements #3

Open
wants to merge 5 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
25 changes: 10 additions & 15 deletions M701x.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
#
# Authors: [email protected], [email protected]


import re,sys,string,serial,time


class M701x:
""" Class for interfacing with Gossen Metrawatt devices over serial """
def __init__(self, serial_port):
Expand All @@ -23,6 +21,9 @@ def __init__(self, serial_port):
timeout=3,
xonxoff=True
)
# turn off local echo
self.__serial.write('\x06') # ACK

# r = self.request('IDN!0')
#

Expand All @@ -44,26 +45,23 @@ def _read(self):
i+=1
return returnstr


def _write(self,str):
""" adds $-delimiter, checksum and line ending to str and sends it to the serial line """
self.__serial.write(str + '$' + self._checksum(str) + '\r\n')


def _checksum(self,str):
@staticmethod
def _checksum(str):
""" calculates checksum of a request/answer """
qsum_dec = ord('$')
for i in str:
d = ord(i)
qsum_dec += d
return "%x" % (qsum_dec & 0xff)

return "%02x" % (qsum_dec & 0xff)

def _flush(self):
""" discards all waiting answers in the buffer """
self.__serial.flushInput()


def request(self,command,retries=3):
""" sends a command to device and parses reply """
i = 0
Expand Down Expand Up @@ -92,16 +90,13 @@ def request(self,command,retries=3):
else:
return False,'CHKSUM_ERROR'


def sync_clock(self,idn):
def sync_clock(self, idn=None):
# needs more testing and ability to sync all devices (e.g. PSI + S2N)
""" synchronizes device clock with PC """
if idn is None:
idn = ''
return self.request('DAT'+idn+'!'+time.strftime("%d.%m.%y;%H:%M:%S"))





if __name__ == "__main__":
m701 = M701x(sys.argv[1])
#m701._write("IDN?")
Expand All @@ -115,4 +110,4 @@ def sync_clock(self,idn):
print m701.request('WER?')

# str = "\x13DATIMx=20.09.14;18:33$18\r\n\x11"
# ^what ^param ^XOFF
# ^what ^param ^XOFF