def motor_up_down(self, move='1'): assert isinstance(move, str) cmd = self.send_translater('UP_DOWN', 'xstart', move) logger.error('mode send cmd' + hex2str(cmd)) self.ser.write(cmd) self.timeout_times = 0 while self.RUNNING: sleep(0.5) self.ser.write(self.read_translater('up1')) sleep(0.1) readed = self.ser.read(7) self.timeout_times += 1 if self.timeout_times == 20: logger.debug("modbus recieved time out") self.timeout_times = 0 break if readed: if len(readed) == 7: result = self.return_parse('up_down_state', readed) if result == 1: logger.error("finished motor") self.timeout_times = 0 break else: self.ser.flushInput()
def run(self): while self.RUNNING: readed = self.ser.read(50) # print('readed'+readed) if readed: self.data_buffer = readed logger.info('slave get buffer ' + hex2str(readed))
def run(self): while self.RUNNING: try: readed = self.ser.read(21) except Exception as e: raise e if readed: self.data_buffer = readed info = 'slave get buffer ' + " ".join("{:02x}".format(ord(c)) for c in readed) logging.info(info) if readed[1:2] == '\x10': cmdline = readed[:8] crc = struct.pack('>H', self.crc16(cmdline)) cmdline = cmdline + crc[1:] + crc[:1] # print 'get write'," ".join("{:02x}".format(ord(c)) for c in result) self.ser.write(cmdline) value = readed[9:11] # print " ".join("{:02x}".format(ord(c)) for c in value) # value = value[1:] + value[:1] # print " ".join("{:02x}".format(ord(c)) for c in value) value = struct.unpack('>H', value)[0] self._goto_direction(value) elif readed[1:2] == '\x03': for_invert = struct.pack('>I', self.direction) value = for_invert[2:] + for_invert[:2] readed = '\x01\x03\x04' + value crc = struct.pack('>H', self.crc16(readed)) readed = readed + crc[1:] + crc[:1] logger.info('slave write' + hex2str(readed)) self.ser.write(readed)
def test_move_mode(self): slave = Slave() slave.start() mod = DyModeBusMode('x', port='com13') mod.start(True) time.sleep(0.1) assert slave.data_buffer[:8] == mod.data_buffer[:8] # cmdline = " ".join("{:02x}".format(ord(c)) for c in slave.data_buffer) cmdline = hex2str(slave.data_buffer) assert cmdline.upper( ) == '01 10 00 C8 00 06 0C 00 01 00 00 03 E8 00 00 00 C8 00 00 F0 58' assert slave.data_buffer[9:11] == '\x00\x00' mod.reversed() assert slave.data_buffer[9:11] == '\x00\x01' mod.start(False) time.sleep(0.1) assert slave.data_buffer[7:9] == '\x00\x00' for x in range(10): time.sleep(0.1) gets = mod.read_pulse() if gets: print 'read get no.', x, gets assert gets < 1000 assert gets > 10 slave.close() slave.ser.close() mod.ser.close()
def plat_motor_goto(self, station, head_dir, move): if self._platform_state: station = self._platform_state # state = self._platform_state # station = STATION_DIR[state] # assert station in MOTOR_STATE[state] print station, head_dir, move cmd = self.send_translater(station, head_dir, move) logger.info('mode send cmd' + hex2str(cmd)) self.ser.write(cmd)
def Ttest_modebusmerge_x1_runs_on_slave(): logging.basicConfig(level=logging.INFO) slave = Slave() slave.start() mode = AbsModeBusModeByAxis(port='com13') mode.plat_motor_goto("PLAT1", 'xstart', 50000) sleep(1) print('databuffer' + hex2str(slave.data_buffer)) mode.plat_motor_goto("PLAT1", 'xstart', 'stop') mode.plat_motor_goto("PLAT1", 'xstart', 0) sleep(1) mode.plat_motor_goto("PLAT1", 'xstart', 'stop') mode.close() slave.close()
def run(self): Thread(target=self._to_dist).start() Thread(target=self.emit_sharp).start() #清晰度线程 while self.RUNNING: try: readed = self.ser.read(21) except Exception as e: raise e if readed: self.data_buffer = readed info = 'slave get buffer ' + " ".join("{:02x}".format(ord(c)) for c in readed) logging.info(info) #记录INFO级别的日志 if readed[1:2] == '\x10': #第二位为十六进制10 cmdline = readed[:8] crc = struct.pack('>H', self.crc16(cmdline)) #对readed的前八位进行校验,并将它用字符串进行返回,根据>H打包 cmdline = cmdline + crc[1:] + crc[:1] # print 'get write'," ".join("{:02x}".format(ord(c)) for c in result) self.ser.write(cmdline) value = readed[9:11] # print " ".join("{:02x}".format(ord(c)) for c in value) # value = value[1:] + value[:1] # print " ".join("{:02x}".format(ord(c)) for c in value) value = struct.unpack('>H', value)[0] # self._goto_direction(value) self.get_direction = value elif readed[1:2] == '\x03': for_invert = struct.pack('>I', self.direction) value = for_invert[2:] + for_invert[:2] readed = '\x01\x03\x04' + value crc = struct.pack('>H', self.crc16(readed)) readed = readed + crc[1:] + crc[:1] logger.info('slave write' + hex2str(readed)) self.ser.write(readed) warning = "get direct {} {}".format( self.direction, self.get_direction) logger.warning(warning) self.ser.close()
def test_send_translater(): send = SendTranslater() sends = send('PLAT1', 'xstart', 35000) sendsstr = " ".join("{:02x}".format(ord(c)) for c in sends) print(sendsstr) assert sendsstr.upper() == "01 10 00 C8 00 02 04 00 01 88 B8 C9 EB" sends = send('PLAT2', 'xstart', 'stop') sendsstr = " ".join("{:02x}".format(ord(c)) for c in sends) print(sendsstr) assert sendsstr.upper() == "02 10 00 C8 00 01 02 00 00 A2 E8" sends = send('PLAT1', 'xstart', 'rest') sendsstr = " ".join("{:02x}".format(ord(c)) for c in sends) print(sendsstr) assert sendsstr.upper() == "01 10 00 E6 00 01 02 00 02 31 97" read = ReadTranslater() readstr = hex2str(read('up1')) assert readstr.upper() == "01 03 00 F0 00 01 84 39"
def plat_motor_reset(self): cmd = self.send_translater('PLAT1', 'xstart', 'rest') logger.info('mode send cmd' + hex2str(cmd)) self.ser.write(cmd)