Uncomment Serial communication

This commit is contained in:
KwanghoKim 2023-12-01 14:13:25 +09:00
parent 5140141eaa
commit 34d7a02a7c
1 changed files with 20 additions and 11 deletions

View File

@ -15,8 +15,8 @@ if os.system("lsmod | grep max7301") == 0:
# For PION-D3W-035(Power Controller) No.1, UT35A(Temperature Controller) No.1 # For PION-D3W-035(Power Controller) No.1, UT35A(Temperature Controller) No.1
_serial_port_A = serial.Serial('/dev/ttyMAX1', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True) _serial_port_A = serial.Serial('/dev/ttyMAX1', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
_serial_port_A.rs485_mode = serial.rs485.RS485Settings() _serial_port_A.rs485_mode = serial.rs485.RS485Settings()
# _serial_port_A.close() _serial_port_A.close()
# del _serial_port_A del _serial_port_A
client1 = ModbusClient(method='rtu', port='/dev/ttyMAX1', baudrate=38400, client1 = ModbusClient(method='rtu', port='/dev/ttyMAX1', baudrate=38400,
parity='N', stopbits=1, bytesize=8, timeout=1) parity='N', stopbits=1, bytesize=8, timeout=1)
@ -25,8 +25,8 @@ client1 = ModbusClient(method='rtu', port='/dev/ttyMAX1', baudrate=38400,
# For PION-D3W-035(Power Controller) No.2, UT35A(Temperature Controller) No.1 # For PION-D3W-035(Power Controller) No.2, UT35A(Temperature Controller) No.1
_serial_port_B = serial.Serial('/dev/ttyMAX0', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True) _serial_port_B = serial.Serial('/dev/ttyMAX0', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
_serial_port_B.rs485_mode = serial.rs485.RS485Settings() _serial_port_B.rs485_mode = serial.rs485.RS485Settings()
# _serial_port_B.close() _serial_port_B.close()
# del _serial_port_B del _serial_port_B
client2 = ModbusClient(method='rtu', port='/dev/ttyMAX0', baudrate=38400, client2 = ModbusClient(method='rtu', port='/dev/ttyMAX0', baudrate=38400,
parity='N', stopbits=1, bytesize=8, timeout=1) parity='N', stopbits=1, bytesize=8, timeout=1)
@ -34,8 +34,8 @@ client2 = ModbusClient(method='rtu', port='/dev/ttyMAX0', baudrate=38400,
# For PION-D3W-035(Power Controller) No.3, UT35A(Temperature Controller) No.1 # For PION-D3W-035(Power Controller) No.3, UT35A(Temperature Controller) No.1
_serial_port_C = serial.Serial('/dev/ttyMAX2', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True) _serial_port_C = serial.Serial('/dev/ttyMAX2', 38400, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout = 1, rtscts=True)
_serial_port_C.rs485_mode = serial.rs485.RS485Settings() _serial_port_C.rs485_mode = serial.rs485.RS485Settings()
# _serial_port_C.close() _serial_port_C.close()
# del _serial_port_C del _serial_port_C
client3 = ModbusClient(method='rtu', port='/dev/ttyMAX2', baudrate=38400, client3 = ModbusClient(method='rtu', port='/dev/ttyMAX2', baudrate=38400,
parity='N', stopbits=1, bytesize=8, timeout=1) parity='N', stopbits=1, bytesize=8, timeout=1)
@ -47,7 +47,7 @@ _serial_port_D.rs485_mode = serial.rs485.RS485Settings()
# del _serial_port_A # del _serial_port_A
client4 = ModbusClient(method='rtu', port='/dev/ttyMAX3', baudrate=9600, client4 = ModbusClient(method='rtu', port='/dev/ttyMAX3', baudrate=9600,
parity='E', stopbits=1, bytesize=8, timeout=0.5) parity='N', stopbits=1, bytesize=8, timeout=0.5)
client1.connect() client1.connect()
client2.connect() client2.connect()
@ -87,7 +87,6 @@ def Read_Temperature(client, ch_num):
result = client.read_holding_registers(address=2003, count=2, slave=id) result = client.read_holding_registers(address=2003, count=2, slave=id)
if not result.isError(): if not result.isError():
# print(f'Temperature: {ch_num} {result.registers}') # print(f'Temperature: {ch_num} {result.registers}')
# value = (result.registers[0] * 256 + result.registers[1])/10
value = (result.registers[0])/10 value = (result.registers[0])/10
else: else:
print(f'Temperature Err: {ch_num} \n {result}') print(f'Temperature Err: {ch_num} \n {result}')
@ -219,10 +218,19 @@ def Read_GasDetector(client, ch_num):
def Set_FlowPump(client, value): def Set_FlowPump(client, value):
id = 31 id = 31
register_value = int(((value * 0.32) + 4) * 2500) # print(f'Set_FlowPump In!: {value}')
val = int(float(value) * 1000)
register_value = val * 8 + 10000
# print(f'register_value: {register_value}')
client.write_registers(address=40004, values=register_value, slave=id) client.write_registers(address=40004, values=register_value, slave=id)
try:
result = client.read_holding_registers(address=40004, count=1, slave=id)
# print(f'result: {result.registers}')
except Exception as e:
print(e)
def Publish_mqtt(data): def Publish_mqtt(data):
send_mqtt_data = json.dumps(data, indent=4) send_mqtt_data = json.dumps(data, indent=4)
@ -246,6 +254,8 @@ def Command_Read():
with open(f'{ROOT_PATH}/control.json') as f: with open(f'{ROOT_PATH}/control.json') as f:
cmd = json.load(f) cmd = json.load(f)
# print(cmd)
if cmd['type'] != 'null': if cmd['type'] != 'null':
if 'start' in cmd['type'] or 'change' in cmd['type']: if 'start' in cmd['type'] or 'change' in cmd['type']:
if cmd['cmd']['precAc'] != 'null': if cmd['cmd']['precAc'] != 'null':
@ -298,5 +308,4 @@ while True:
# print(f'diff: {diff}') # print(f'diff: {diff}')
if diff < 3: if diff < 3:
time.sleep(3.0 - diff) time.sleep(3.0 - diff)