def getPortNo(type, ipaddress): slaveid = 1 file_path = '/var/www/html/openWB/ramdisk/solaredgeport_' + str(type) # ModBus is configured to listen on port 502 or 1502, actual port is autodetected and cache in ramdisk port = 0 # port not defined ramdiskInitialized = False # assume ramdisk cache is not set if os.path.isfile(file_path): with open(file_path, 'r') as f: try: data = f.read() (ipaddr, port) = data.split(':') if (ipaddress == ipaddr): ramdiskInitialized = True return port else: print('changed ip addr, old: %s, new: %s' % (ipaddr, ipaddress)) os.remove(file_path) except: pass # ramdisk file does not exist, proceed with autodetection port = 502 # try port 502 first client = ModbusTcpClient(ipaddress, port=port) try: resp = client.read_holding_registers(40000, 1, unit=slaveid) if resp.registers[0] == 21365: if not ramdiskInitialized: # if we reach this line, we were able to read ModBus registers # ramdisk is not set, so persist port value now for next time with open(file_path, 'w') as f: f.write(ipaddress + ':' + str(port)) return port # we were unable to read registers from port 502 so try 1502 next except ConnectionException: port = 1502 client.port = port try: resp = client.read_holding_registers(40000, 1, unit=slaveid) if resp.registers[0] == 21365: # now it worked, persist port number in ramdisk if not ramdiskInitialized: with open(file_path, 'w') as f: f.write(ipaddress + ':' + str(port)) return port except: # no connection, remove cache file if any if os.path.isfile(file_path): os.remove(file_path) return port
def run(self): # 통신 설정 SERVER_HOST = str(self.IP_address) c = ModbusTcpClient() c.host = self.IP_address c.port = SERVER_PORT channelNum = self.channelNum pre_regs = [] ch0_switch_on_triger = '' ch0_tp_triger = '' ch0_busy_triger = '' ch0_err_triger = '' ch0_done_triger = '' while True: # open or reconnect TCP to server if not c.connect(): print("해당 Server와 연결되지 않았습니다. " + SERVER_HOST + ":" + str(SERVER_PORT)) myWindow.label_4.setText('Connection Fail') c.close() break else: myWindow.pushButton.setText('접속 종료') myWindow.label_4.setText('Connected') try: #--- ch0 input register # status ch0_status_regs = c.read_holding_registers(16384 + channelNum, 10) # input data in buffer ch0_input_buffer_regs = c.read_holding_registers(16464 + channelNum*80, 50) except: print("데이터 획득 중 통신이 끊기거나 오류가 발생했습니다.") else: ch0_status = boolean_def(int(ch0_status_regs.registers[0])) ch0_switch_on = ch0_status[4] if ch0_switch_on == 'True': if ch0_switch_on_triger != ch0_switch_on: myWindow.label_6.setStyleSheet("color: green;" "background-color: #7FFFD4") ch0_switch_on_triger = ch0_switch_on else : if ch0_switch_on_triger != ch0_switch_on: myWindow.label_6.setStyleSheet("color: black;" "background-color: #FFFFFF") ch0_switch_on_triger = ch0_switch_on ch0_tp = ch0_status[6] if ch0_tp == 'True': if ch0_tp_triger != ch0_tp: myWindow.label_7.setStyleSheet("color: green;" "background-color: #7FFFD4") ch0_tp_triger = ch0_tp else : if ch0_tp_triger != ch0_tp: myWindow.label_7.setStyleSheet("color: black;" "background-color: #FFFFFF") ch0_tp_triger = ch0_tp ch0_busy = ch0_status[1] if ch0_busy == 'True': if ch0_busy_triger != ch0_busy: myWindow.label_10.setStyleSheet("color: green;" "background-color: #7FFFD4") ch0_busy_triger = ch0_busy else : if ch0_busy_triger != ch0_busy: myWindow.label_10.setStyleSheet("color: black;" "background-color: #FFFFFF") ch0_busy_triger = ch0_busy ch0_err = ch0_status[2] if ch0_err == 'True': if ch0_err_triger != ch0_err: myWindow.label_13.setStyleSheet("color: red;" "background-color: #FA8072") ch0_err_triger = ch0_err else : if ch0_err_triger != ch0_err: myWindow.label_13.setStyleSheet("color: black;" "background-color: #FFFFFF") ch0_err_triger = ch0_err ch0_done = ch0_status[0] if ch0_done == 'True': if ch0_done_triger != ch0_done: myWindow.label_14.setStyleSheet("color: green;" "background-color: #7FFFD4") ch0_done_triger = ch0_done else : if ch0_done_triger != ch0_done: myWindow.label_14.setStyleSheet("color: black;" "background-color: #FFFFFF") ch0_done_triger = ch0_done if ch0_input_buffer_regs.registers[0] != 61440: if ch0_input_buffer_regs.registers != pre_regs: pre_regs = ch0_input_buffer_regs.registers Read_data = [] for i in range(50): test_n = intToBytes(ch0_input_buffer_regs.registers[i]) Read_data.append(chr(test_n[2])) Read_data.append(chr(test_n[3])) rfidDataAll = ''.join(Read_data[0:100]) curTime = dataLogging('./Datalog', rfidDataAll) rfidData = ''.join(Read_data[0:10]) print(rfidData) loggedDataView(rfidData, curTime, rfidDataAll) else: print("HERE") # ReadTable(Read_data) # sleep 'n'second before next polling time.sleep(0.02) if stop == True: print("Break!") c.close() break
def run(self): # 통신 설정 SERVER_HOST = str(self.IP_address) c = ModbusTcpClient() c.host = self.IP_address c.port = SERVER_PORT pre_regs = [0, 0, 0, 0] preStatusValue = [0, 0, 0, 0] while True: # open or reconnect TCP to server if not c.connect(): print("해당 Server와 연결되지 않았습니다. " + SERVER_HOST + ":" + str(SERVER_PORT)) myWindow.label_4.setText('Connection Fail') c.close() break else: myWindow.pushButton.setText('접속 종료') myWindow.label_4.setText('Connected') try: status_regs = [] input_buffer_regs = [] for channelNum in range(4): # status status_regs.append(c.read_holding_registers(16384 + channelNum, 10)) # input data in buffer input_buffer_regs.append(c.read_holding_registers(16464 + channelNum*80, 51)) except: print("데이터 획득 중 통신이 끊기거나 오류가 발생했습니다.") else: for channelNum in range(4): statusValue = int(status_regs[channelNum].registers[0]) # Status check with previous status if statusValue != preStatusValue[channelNum] : preStatusValue[channelNum] = statusValue ch_status = boolean_def(statusValue) switch_on = ch_status[4] tp = ch_status[6] busy = ch_status[1] err = ch_status[2] done = ch_status[0] infoString = 'channelNum : [' + str(channelNum) + ']' + ' switch_on: ' +\ str(switch_on) + ', tp: ' + str(tp)+ ', busy: ' + str(busy) +\ ', err: ' + str(err) + ', done: ' + str(done) infoLog.info(infoString) # Read Data check with previous status if input_buffer_regs[channelNum].registers[0] != 61440: if input_buffer_regs[channelNum].registers != pre_regs[channelNum]: pre_regs[channelNum] = input_buffer_regs[channelNum].registers Read_data = [] for i in range(50): test_n = intToBytes(input_buffer_regs[channelNum].registers[i]) Read_data.append(chr(test_n[2])) Read_data.append(chr(test_n[3])) rfidDataAll = ''.join(Read_data[0:100]) dataString = 'channelNum[' + str(channelNum) + '] :' + rfidDataAll dataLog.info(dataString) else: print("HERE") # ReadTable(Read_data) # sleep 'n'second before next polling time.sleep(0.02) if stop == True: print("Break!") c.close() break