diff --git a/si_autosend_serial.py b/si_autosend_serial.py new file mode 100644 index 0000000..3d940ba --- /dev/null +++ b/si_autosend_serial.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2019 Per Magnusson +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +This example reads the station programmed to autosend the data and redirects to UART1 port to be transfered over Meshtastic network +Sample of read data: +53|233177|12:14:09 +53|233385|12:14:32 +53|233173|12:14:37 +53|233174|12:14:46 +53|233176|12:14:55 +""" + +from sireader2 import SIReader, SIReaderException, SIReaderControl +import sys +import serial + + +try: + si = SIReader() + reader=SIReaderControl(si) + print('Connected to station on port ' + si.port) +except: + print('Failed to connect to an SI station on any of the available serial ports.') + exit() + + +ser = serial.Serial( + port='/dev/ttyS1', + baudrate=57600, + bytesize=8, + parity='N', + stopbits=1, + timeout=1 +) + +while True: + punches=reader.autosend_punch() + if len(punches) > 0: + message = f"{punches[0]}|{punches[1]}|{punches[2].time().isoformat(timespec='seconds')}\r\n" + bmsg=message.encode('utf-8') + ser.write(bmsg) + print(message) + + + + diff --git a/sireader2.py b/sireader2.py index e3e9b34..2591465 100644 --- a/sireader2.py +++ b/sireader2.py @@ -1823,13 +1823,39 @@ def poll_punch(self, timeout=0): self._next_offset += SIReader.REC_LEN self._next_offset = cur_offset + SIReader.REC_LEN - punches.append( (self._decode_cardnr(c[1][SIReader.T_CN:SIReader.T_CN+4]), - self._decode_time(c[1][SIReader.T_TIME:SIReader.T_TIME+2])) ) - else: - raise SIReaderException('Unexpected command %s received' % hex(byte2int(c[0]))) - + punches.append( (self._decode_cardnr(c[1][SIReader.T_CN:SIReader.T_CN+4]), + self._decode_time(c[1][SIReader.T_TIME:SIReader.T_TIME+2])) ) + else: + raise SIReaderException('Unexpected command %s received' % hex(byte2int(c[0]))) return punches - + + def autosend_punch(self, timeout=0): + """Reads autosend punches. + @return: (stationnr, cardnr, punchtime) + """ + + if not self.proto_config['ext_proto']: + raise SIReaderException('This command only supports stations in "Extended Protocol" ' + 'mode. Switch mode first') + + if not self.proto_config['auto_send']: + raise SIReaderException('This command only supports stations in "Autosend" ' + 'mode. Switch mode first') + + punches = [] + while True: + try: + c = self._read_command(timeout = None) + except SIReaderTimeout: + break + + if c[0] == SIReader.C_TRANS_REC: + punches= (self.get_station_code(),self._decode_cardnr(c[1][SIReader.T_CN:SIReader.T_CN+4]), self._decode_time(c[1][SIReader.T_TIME:SIReader.T_TIME+2])) + else: + raise SIReaderException('Unexpected command %s received' % hex(byte2int(c[0]))) + + return punches + def _read_punch(self, offset): """Reads a punch from the SI Stations backup memory. @param offset: Position in the backup memory to read