
# a simple class to manage serial communication testing without to install the pySerial package

class Serial:

    def __init__(self, device, baudrate, timeout):
        self.device   = device
        self.baudrate = baudrate
        self.timeout  = timeout
        self.msg_ok  = [ 0x3c, 0x3e, 0x01, 0x0d, 0x0a ]
        self.msg_nok = [ 0x3c, 0x3e, 0x02, 0x0d, 0x0a ]
        self.msg_br  = [ 0x3c, 0x3e, 0x03, 0x00, 0x01, 0xc2, 0x00, 0x0d, 0x0a ]
        self.msg_ml  = [ 0x3c, 0x3e, 0x04, 0x00, 0x0d, 0x0a ]

    def read(self, num=0):
      if num == 0:
        return bytearray(['0'])
      else:
        ret = []
        tmp  = self.msg_ok
        tmp += self.msg_nok
        tmp += self.msg_br
        tmp += self.msg_ml
        for i in range(0, num):
          if i < len(tmp):
            ret.append(tmp[i])

        return bytearray(ret)

    def write(self, data):
      for d in data:
        print "0x%02x " % (d)
      pass

    def close(self):
      print "Serial connection close"
