def send_single_command(self, cmd, read_response = True): packet = [self.p_sync, self.p_adr, 0x06, cmd] packet = packet + int2bytes( self.__calc_crc(packet) ) self.serial.write( serial.to_bytes( packet ) ) if read_response: return self._read_response() return True
def send_single_command(self, cmd, read_response=True): packet = [self.p_sync, self.p_adr, 0x06, cmd] packet = packet + int2bytes(self.__calc_crc(packet)) self.serial.write(serial.to_bytes(packet)) if read_response: return self._read_response() return True
def send_command(self, data, read_response = True): packet = [ self.p_sync, self.p_adr, 5 + len(data)] + data packet = packet+ int2bytes(self.__calc_crc(packet)) self.serial.write(serial.to_bytes(packet) ) if read_response: return self._read_response() return True
def send_command(self, data, read_response=True): packet = [self.p_sync, self.p_adr, 5 + len(data)] + data packet = packet + int2bytes(self.__calc_crc(packet)) self.serial.write(serial.to_bytes(packet)) if read_response: return self._read_response() return True
def __read(self, timeout = 60): time_start = time.time() frame = [] data = '' byte = self.serial.read(1) data += byte try: first_byte = int(byte2hex( byte ), 16) self.logger.debug('DispenserAdapter.__read: first byte is: 0x%02X' % first_byte) if first_byte == self.dispenser.p_nck: self.logger.debug('DispenserAdapter.__read: first byte is NCK') return [self.dispenser.p_nck] if first_byte == self.dispenser.p_ack: self.logger.debug('DispenserAdapter.__read: first byte is: ACK') return [self.dispenser.p_ack] byte = self.serial.read(1) data += byte self.logger.debug('DispenserAdapter.__read: Wrong first byte in response 0x%02X' % first_byte ) self.logger.debug('DispenserAdapter.__read: Next byte in response is 0x%02X' % int(byte2hex( byte )) ) except Exception as e: self.logger.debug('DispenserAdapter.__read: %s' % str(e.args) ) pass self.logger.debug('DispenserAdapter.__read: first byte is detected') while True: if time.time() - time_start > timeout: self.logger.debug('DispenserAdapter.__read: timeout to read. ') for _, i in enumerate(serial.to_bytes(data)): frame.append( int(byte2hex(i), 16) ) self.logger.debug('DispenserAdapter.__read: full dump of response: %s' % (["0x%02X" % x for x in frame])) raise DispenserError(message = 'Timeout to read', code = 500) try: if int(byte2hex(byte), 16) == self.dispenser.p_etx: break except Exception as e: self.logger.debug('Error in read cycle: %s' % str(e.args) ) time.sleep( .01 ) byte = self.serial.read(1) data += byte self.logger.debug('DispenserAdapter.__read: cycle is complete') data += self.serial.read(1) for _, i in enumerate(serial.to_bytes(data)): frame.append( int(byte2hex(i), 16) ) self.logger.debug('DispenserAdapter.__read: full dump of response: [%s]' % ([x for x in frame])) return frame
def __write(self, bytes): return self.serial.write(serial.to_bytes(bytes))
(BYTE*8)(0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73)) DIGCF_PRESENT = 2 DIGCF_DEVICEINTERFACE = 16 INVALID_HANDLE_VALUE = 0 ERROR_INSUFFICIENT_BUFFER = 122 SPDRP_HARDWAREID = 1 SPDRP_FRIENDLYNAME = 12 ERROR_NO_MORE_ITEMS = 259 DICS_FLAG_GLOBAL = 1 DIREG_DEV = 0x00000001 KEY_READ = 0x20019 REG_SZ = 1 # workaround for compatibility between Python 2.x and 3.x PortName = serial.to_bytes([80, 111, 114, 116, 78, 97, 109, 101]) # "PortName" def comports(): """This generator scans the device registry for com ports and yields port, desc, hwid""" g_hdi = SetupDiGetClassDevs(ctypes.byref(GUID_CLASS_COMPORT), None, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); #~ for i in range(256): for dwIndex in range(256): did = SP_DEVICE_INTERFACE_DATA() did.cbSize = ctypes.sizeof(did) if not SetupDiEnumDeviceInterfaces(g_hdi, None, ctypes.byref(GUID_CLASS_COMPORT), dwIndex, ctypes.byref(did)): if ctypes.GetLastError() != ERROR_NO_MORE_ITEMS: raise ctypes.WinError() break dwNeeded = DWORD()
def __read(self, timeout=60): time_start = time.time() frame = [] data = "" byte = self.serial.read(1) data += byte try: first_byte = int(byte2hex(byte), 16) self.logger.debug("DispenserAdapter.__read: first byte is: 0x%02X" % first_byte) if first_byte == self.dispenser.p_nck: self.logger.debug("DispenserAdapter.__read: first byte is NCK") return [self.dispenser.p_nck] if first_byte == self.dispenser.p_ack: self.logger.debug("DispenserAdapter.__read: first byte is: ACK") return [self.dispenser.p_ack] byte = self.serial.read(1) data += byte self.logger.debug("DispenserAdapter.__read: Wrong first byte in response 0x%02X" % first_byte) self.logger.debug("DispenserAdapter.__read: Next byte in response is 0x%02X" % int(byte2hex(byte))) except Exception as e: self.logger.debug("DispenserAdapter.__read: %s" % str(e.args)) pass self.logger.debug("DispenserAdapter.__read: first byte is detected") while True: if time.time() - time_start > timeout: self.logger.debug("DispenserAdapter.__read: timeout to read. ") for _, i in enumerate(serial.to_bytes(data)): frame.append(int(byte2hex(i), 16)) self.logger.debug( "DispenserAdapter.__read: full dump of response: %s" % (["0x%02X" % x for x in frame]) ) raise DispenserError(message="Timeout to read", code=500) try: if int(byte2hex(byte), 16) == self.dispenser.p_etx: break except Exception as e: self.logger.debug("Error in read cycle: %s" % str(e.args)) time.sleep(0.01) byte = self.serial.read(1) data += byte self.logger.debug("DispenserAdapter.__read: cycle is complete") data += self.serial.read(1) for _, i in enumerate(serial.to_bytes(data)): frame.append(int(byte2hex(i), 16)) self.logger.debug("DispenserAdapter.__read: full dump of response: [%s]" % ([x for x in frame])) return frame