def __serial_event_callback(self, slot, val): #print(tpUtils.slot_int_to_str(slot), send_data) # いったん、バッファへ受信データ格納 pos = int((slot - 1) / 2) kind = self.__serial_info[pos]['recv_kind'] try: if kind == 'TIME': # 時間区切り self.__serial_info[pos]['lock'].acquire(1) self.__serial_recv_buf[pos].extend(val) except: raise finally: if kind == 'TIME': # 時間区切り self.__serial_info[pos]['lock'].release() # 受信方法による振り分け if kind == 'CHAR': # 文字区切り char = self.__serial_info[pos]['char'] self.__serial_recv_char(slot, char) elif kind == 'TIME': # 時間区切り pass # 別スレッドで対応 elif kind == 'LENG': # 固定長区切り leng = self.__serial_info[pos]['leng'] self.__serial_recv_leng(slot, leng) else: # 区切りなし send_data = self.__serial_recv_buf[pos][:] # 実copy #print(tpUtils.slot_int_to_str(slot), send_data, self.__serial_recv_buf) self.__serial_recv_buf[pos].clear() self.callback_send(tpUtils.slot_int_to_str(slot), Serial, json.dumps(send_data))
def start(self, callback_recv, callback_recv_dsr): """ 開始処理 """ # confからmodeを取得する if (self.host is None or self.host == ''): self.host = 'localhost' tp_config = TpConfig(self.host, self.slot, self.comm) setting = tp_config.get_setting() mode = setting['settings']['mode'] if mode == 'RS232': self.__setModeVal(1, 0) elif mode == 'RS422': self.__setModeVal(1, 1) elif mode == 'RS485': self.__setModeVal(0, 1) else: raise ValueError('Tibbit #02 Line error!') self.tcp_client = TcpClient(callback_recv) self.tcp_client.connect_by_conf_recv(self.host, self.slot, self.comm) # DSR slot_num = tpUtils.slot_str_to_int(self.slot) slot_num = slot_num + 1 slot_gpio = tpUtils.slot_int_to_str(slot_num) self.gpio_tcp_client = TcpClient(callback_recv_dsr) self.gpio_tcp_client.connect_by_conf_recv(self.host, slot_gpio, GPIO)
def __serial_recv_time_thread(self, slot, time_ms): """Serial受信時間区切り """ pos = int((slot - 1) / 2) while True: time.sleep(time_ms / 1000) if len(self.__serial_recv_buf[pos]): send_data = self.__serial_recv_buf[pos][:] # 実copy self.__serial_recv_buf[pos].clear() #print(tpUtils.slot_int_to_str(slot), send_data, self.__serial_recv_buf) self.callback_send(tpUtils.slot_int_to_str(slot), Serial, json.dumps(send_data))
def __serial_recv_leng(self, slot, leng): """Serial受信固定長区切り """ pos = int((slot - 1) / 2) while len(self.__serial_recv_buf[pos]): if len(self.__serial_recv_buf[pos]) <= leng: break else: send_data = self.__serial_recv_buf[pos][:leng] # 実copy del self.__serial_recv_buf[pos][:leng] #print(tpUtils.slot_int_to_str(slot), send_data, self.__serial_recv_buf) self.callback_send(tpUtils.slot_int_to_str(slot), Serial, json.dumps(send_data))
def __serial_recv_char(self, slot, char): """Serial受信文字区切り """ #print('__serial_recv_char', slot, char, len(char)) pos = int((slot - 1) / 2) if ord(char) in self.__serial_recv_buf[pos]: buf_pos = self.__serial_recv_buf[pos].index(ord(char)) send_data = self.__serial_recv_buf[pos][:buf_pos + 1] # 区切り文字含め、実copy #print(tpUtils.slot_int_to_str(slot), send_data, self.__serial_recv_buf) del self.__serial_recv_buf[pos][:buf_pos + 1] self.callback_send(tpUtils.slot_int_to_str(slot), Serial, json.dumps(send_data))
def __init__(self, slot, host=None): """ コンストラクタ """ self.slot = slot self.comm = Serial self.host = host # LINE C self.io_tcp_client = None # LINE G/H slot_num = tpUtils.slot_str_to_int(self.slot) slot_num = slot_num + 1 slot_gpio = tpUtils.slot_int_to_str(slot_num) self.gpio_tcp_client = TcpClient() self.gpio_tcp_client.connect_by_conf(self.host, slot_gpio, GPIO)
def __setModeVal(self, ch_a, ch_b): """ モードをセット """ slot_num = tpUtils.slot_str_to_int(self.slot) slot_num = slot_num + 1 slot_gpio = tpUtils.slot_int_to_str(slot_num) temp_tcp_client = TcpClient() temp_tcp_client.connect_by_conf(self.host, slot_gpio, GPIO) send_data = [] tmp_data = {} tmp_data["line"] = 'A' tmp_data["v"] = ch_a send_data.append(tmp_data) tmp_data = {} tmp_data["line"] = 'B' tmp_data["v"] = ch_b send_data.append(tmp_data) temp_tcp_client.send(json.dumps(send_data))
def __gpio_event_callback(self, slot, line, on): send_data = {'line': tpUtils.line_int_to_str(line), 'v': on} #print(tpUtils.slot_int_to_str(slot), send_data) self.callback_send(tpUtils.slot_int_to_str(slot), GPIO, json.dumps(send_data))