def get_updated_status(self, isServer): o = None o = get(self.thread_resp_queue, block=False) if o != None and isServer == True: if len(o) >= 6: self.read_finish_status, self.STATUS, self.ERROR, self.STATUS_MODBUS, self.STATUS_CONN, self.BUSY, self.CONN_ESTABLISHED = ( o ) self.send_dummy_ack_command() if o != None and isServer == False: if len(o) >= 6: self.read_finish_status, self.STATUS, self.ERROR, self.STATUS_MODBUS, self.STATUS_CONN, self.BUSY, self.CONN_ESTABLISHED = ( o ) if self.read_finish_status != 0: msg_to_send, exception = self.mod_client.construct_request_message( self.data_type, self.write_read, self.length, self.connection_params.single_write, self.start_address, self.TI, self.remote_host_id, ) put( self.thread_cmd_queue, 2, (self.disconnect, self.recv_time, self.conn_time, msg_to_send, exception), )
def close(self): if self.is_server == True and self.server_process != None: print("Terminating server ...") put(self.thread_cmd_queue, 3, "QUIT") if self.is_server == False and self.client_process != None: print("Terminating client ...") put(self.thread_cmd_queue, 3, "QUIT")
def call_modbus_client_process(self): TCP_REMOTE_IP = "127.0.0.1" TCP_REMOTE_IP = self.connection_params.rem_staddr TCP_REMOTE_PORT = self.connection_params.rem_tsap_id timeout = 0.001 if self.STATUS == NOT_STARTED: # start server if self.ENQ_ENR == 1: self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0000 self.STATUS_CONN = 0x0000 self.STATUS_FUNC = "MODBUSPN" self.IDENT_CODE = "NONE" if self.cpu.network_interface_type == 0: # IP self.client_process = Process( target=test_run_client_ip, args=( self.thread_resp_queue, self.thread_cmd_queue, self.connection_params.local_tsap_id, self.IDS_IP, TCP_REMOTE_IP, TCP_REMOTE_PORT, self.conn_time, self.cpu.local_id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.client_process.start() else: # thread_resp_queue,thread_cmd_queue,conn_time, local_id, remote_id, connection_id, thread_resp_arr,thread_cmd_arr self.client_process = Process( target=test_run_client_serial, args=( self.thread_resp_queue, self.thread_cmd_queue, self.conn_time, self.local_host_id, self.remote_host_id, self.connection_params.id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.client_process.start() self.get_updated_status(False) self.PREV_ENQ_ENR = self.ENQ_ENR elif self.STATUS != RUNNING: # completed if self.read_finish_status == 0: # for this call return the status params undisturbed # start new thread or resume the existing thread in the nxt call if self.ENQ_ENR == 1 and self.CONN_ESTABLISHED == True: msg_to_send, exception = self.mod_client.construct_request_message( self.data_type, self.write_read, self.length, self.connection_params.single_write, self.start_address, self.TI, self.remote_host_id, ) put( self.thread_cmd_arr, 2, (self.disconnect, self.recv_time, self.conn_time, msg_to_send, exception), ) self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0 self.STATUS_CONN = 0x0 self.BUSY = True self.CONN_ESTABLISHED = True elif self.ENQ_ENR == 1: self.send_dummy_ack_command() self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0 self.STATUS_CONN = 0x0 self.BUSY = True self.CONN_ESTABLISHED = False else: put(self.thread_cmd_queue, 3, "QUIT") self.read_finish_status = 1 elif self.STATUS == DONE and self.CONN_ESTABLISHED == True: # if disconnect is false if self.ENQ_ENR == 1: self.STATUS = RUNNING self.get_updated_status(False) else: # cases STATUS = DONE, CONN = False, STATUS = RECV_TIMEOUT_ERROR | CONN_TIMEOUT_ERROR | CLIENT_ERROR, CONN = False # all these cases imply thread exited - if pos in ENQ_ENR - restart new thread if self.ENQ_ENR == 1: self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0000 self.STATUS_CONN = 0x0000 self.STATUS_FUNC = "MODBUSPN" self.IDENT_CODE = "NONE" if self.client_process != None: self.client_process.join() del self.thread_resp_queue del self.thread_cmd_queue self.thread_resp_queue = Array("i", range(2000)) self.thread_cmd_queue = Array("i", range(2000)) self.thread_resp_arr = Array("i", range(2000)) self.thread_cmd_arr = Array("i", range(2000)) if self.cpu.network_interface_type == 0: # IP self.client_process = Process( target=test_run_client_ip, args=( self.thread_resp_queue, self.thread_cmd_queue, self.connection_params.local_tsap_id, self.IDS_IP, TCP_REMOTE_IP, TCP_REMOTE_PORT, self.conn_time, self.cpu.local_id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.client_process.start() else: self.client_process = Process( target=test_run_client_serial, args=( self.thread_resp_queue, self.thread_cmd_queue, self.conn_time, self.local_host_id, self.remote_host_id, self.connection_params.id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.client_process.start() self.get_updated_status(False) o = None o = get(self.thread_resp_arr, block=False) if o != None: ERROR_CODE = self.mod_client.process_response_message(o) self.read_finish_status = 0 if ERROR_CODE == NO_ERROR: self.STATUS = DONE self.ERROR = False if self.disconnect == True: self.read_finish_status = 1 self.STATUS = DONE self.CONN_ESTABLISHED = False put(self.thread_cmd_arr, 3, "QUIT") else: # set STATUS_MODBUS Based on returned error value self.read_finish_status = 1 self.STATUS = DONE self.ERROR = True self.STATUS_MODBUS = ERROR_CODE self.STATUS_CONN = 0x0 self.CONN_ESTABLISHED = False put(self.thread_cmd_arr, 3, "QUIT") self.PREV_ENQ_ENR = self.ENQ_ENR return self.STATUS elif self.STATUS == RUNNING: self.PREV_ENQ_ENR = self.ENQ_ENR self.get_updated_status(False) o = None o = get(self.thread_resp_arr, block=False) if o != None: ERROR_CODE = self.mod_client.process_response_message(o) self.read_finish_status = 0 if ERROR_CODE == NO_ERROR: self.STATUS = DONE self.ERROR = False if self.disconnect == True: self.read_finish_status = 1 self.STATUS = DONE self.CONN_ESTABLISHED = False put(self.thread_cmd_arr, 3, "QUIT") else: # set STATUS_MODBUS Based on returned error value self.read_finish_status = 1 self.STATUS = DONE self.ERROR = True self.STATUS_MODBUS = ERROR_CODE self.STATUS_CONN = 0x0 self.CONN_ESTABLISHED = False put(self.thread_cmd_arr, 3, "QUIT") return self.STATUS
def call_modbus_server_process(self): timeout = 0.001 if self.STATUS == NOT_STARTED: # start server if self.ENQ_ENR == 1: self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0000 self.STATUS_CONN = 0x0000 self.STATUS_FUNC = "MODBUSPN" self.IDENT_CODE = "NONE" if self.cpu.network_interface_type == 0: # IP self.server_process = Process( target=test_run_server_ip, args=( self.thread_resp_queue, self.thread_cmd_queue, self.connection_params.local_tsap_id, self.disconnect, self.recv_time, self.conn_time, self.IDS_IP, self.cpu.local_id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.server_process.start() else: self.server_process = Process( target=test_run_server_serial, args=( self.thread_resp_queue, self.thread_cmd_queue, self.disconnect, self.recv_time, self.conn_time, self.local_host_id, self.remote_host_id, self.connection_params.id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) self.server_process.start() self.get_updated_status(True) self.PREV_ENQ_ENR = self.ENQ_ENR elif self.STATUS != RUNNING: # completed if self.read_finish_status == 0: # for this call return the status params undisturbed # start new thread or resume the existing thread in the nxt call self.read_finish_status = 1 elif self.STATUS == DONE and self.CONN_ESTABLISHED == True: # if disconnect is false if self.ENQ_ENR == 1: self.STATUS = RUNNING self.get_updated_status(True) else: # cases STATUS = DONE, CONN = False, STATUS = RECV_TIMEOUT_ERROR | CONN_TIMEOUT_ERROR | CLIENT_ERROR, CONN = False # all these cases imply thread exited - if pos in ENQ_ENR - restart new thread if self.ENQ_ENR == 1: isrestart = False if self.cpu.network_interface_type == 0 or ( self.cpu.network_interface_type != 0 and self.STATUS != DONE ): isrestart = True self.STATUS = RUNNING self.ERROR = False self.STATUS_MODBUS = 0x0000 self.STATUS_CONN = 0x0000 self.STATUS_FUNC = "MODBUSPN" self.IDENT_CODE = "NONE" if self.server_process != None and isrestart == True: self.server_process.join() del self.thread_resp_queue del self.thread_cmd_queue self.thread_resp_queue = Array("i", range(2000)) self.thread_cmd_queue = Array("i", range(2000)) self.thread_resp_arr = Array("i", range(2000)) self.thread_cmd_arr = Array("i", range(2000)) if self.cpu.network_interface_type == 0: # IP self.server_process = Process( target=test_run_server_ip, args=( self.thread_resp_queue, self.thread_cmd_queue, self.connection_params.local_tsap_id, self.disconnect, self.recv_time, self.conn_time, self.IDS_IP, self.cpu.local_id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) print("Re-Started server process at " + str(time.time())) self.server_process.start() elif isrestart == True: self.server_process = Process( target=test_run_server_serial, args=( self.thread_resp_queue, self.thread_cmd_queue, self.disconnect, self.recv_time, self.conn_time, self.local_host_id, self.remote_host_id, self.connection_params.id, self.thread_resp_arr, self.thread_cmd_arr, self.conf_dir, ), ) print("Re-Started server process at " + str(time.time())) self.server_process.start() self.get_updated_status(True) o = None o = get(self.thread_resp_arr, block=False) if o != None: recv_data = o response, request_msg_params = self.mod_server.process_request_message(recv_data) if request_msg_params["ERROR"] == NO_ERROR: self.STATUS = DONE self.ERROR = False put(self.thread_cmd_arr, 1, response) if self.disconnect == True: self.CONN_ESTABLISHED = False self.BUSY = False self.read_finish_status = 1 else: if self.cpu.network_interface_type == 0: self.CONN_ESTABLISHED = True else: self.CONN_ESTABLISHED = False self.BUSY = True self.read_finish_status = 0 else: # set STATUS_MODBUS Based on returned error value print("ERROR on processing request msg") self.read_finish_status = 1 self.STATUS = DONE self.ERROR = True self.CONN_ESTABLISHED = False self.BUSY = False self.STATUS_MODBUS = request_msg_params["ERROR"] self.STATUS_CONN = 0x0 put(self.thread_cmd_arr, 3, "QUIT") self.unit = request_msg_params["unit"] self.TI = request_msg_params["ti"] self.data_type = request_msg_params["data_type"] self.write_read = request_msg_params["write_read"] self.start_address = request_msg_params["start_address"] self.length = request_msg_params["length"] self.PREV_ENQ_ENR = self.ENQ_ENR return self.STATUS elif self.STATUS == RUNNING: self.PREV_ENQ_ENR = self.ENQ_ENR self.get_updated_status(True) o = None o = get(self.thread_resp_arr, block=False) if o != None: recv_data = o response, request_msg_params = self.mod_server.process_request_message(recv_data) if request_msg_params["ERROR"] == NO_ERROR: self.STATUS = DONE self.ERROR = False put(self.thread_cmd_arr, 1, response) if self.disconnect == True: self.CONN_ESTABLISHED = False self.BUSY = False self.read_finish_status = 1 else: if self.cpu.network_interface_type == 0: self.CONN_ESTABLISHED = True else: self.CONN_ESTABLISHED = False self.BUSY = True self.read_finish_status = 0 else: # set STATUS_MODBUS Based on returned error value print("ERROR on processing request msg") self.read_finish_status = 1 self.STATUS = DONE self.ERROR = True self.CONN_ESTABLISHED = False self.BUSY = False self.STATUS_MODBUS = request_msg_params["ERROR"] self.STATUS_CONN = 0x0 put(self.thread_cmd_arr, 3, "QUIT") self.unit = request_msg_params["unit"] self.TI = request_msg_params["ti"] self.data_type = request_msg_params["data_type"] self.write_read = request_msg_params["write_read"] self.start_address = request_msg_params["start_address"] self.length = request_msg_params["length"] return self.STATUS
def send_dummy_ack_command(self): put(self.thread_cmd_queue, 1, [1])