-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import serial | ||
import serial.tools.list_ports | ||
import time | ||
|
||
def get_ports(): | ||
ports = serial.tools.list_ports.comports(include_links=False) | ||
if len(ports) == 0: | ||
raise Exception('failed to find a port') | ||
return ports | ||
|
||
def get_connection(): | ||
ports = get_ports() | ||
|
||
for port in ports: | ||
try: | ||
ser = serial.Serial("COM12", baudrate=9600, timeout=10) | ||
time.sleep(5) | ||
return ser | ||
except Exception as e: | ||
print(e) | ||
continue | ||
|
||
def set_temperature(conn, temp): | ||
to_send = '1 '+str(temp)+"\r\n" | ||
conn.write(to_send.encode()) | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
def set_hygro(conn, hygro): | ||
to_send = '2 '+str(hygro)+"\r\n" | ||
conn.write(to_send.encode()) | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
def get_expected_tmp(conn): | ||
conn.write(b"3\r\n") | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
def get_expected_hygro(conn): | ||
conn.write(b"4\r\n") | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
def get_actual_tmp(conn): | ||
conn.write(b"5\r\n") | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
def get_actual_hygro(conn): | ||
conn.write(b"6\r\n") | ||
c = conn.readline().rstrip() | ||
return float(c) | ||
|
||
if __name__ == "__main__": | ||
conn = get_connection() | ||
print("===DEFAULT COMMAND===") | ||
print("Temperature command before : ", get_expected_tmp(conn)) | ||
print("Hygro command before : ", get_expected_hygro(conn)) | ||
print("===UPDATING COMMANDS===") | ||
print("Setting temperature command to : ", set_temperature(conn, 25.0)) | ||
print("Setting hygro command to : ", set_hygro(conn, 60.0)) | ||
print("===COMMANDS AFTER UPDATING===") | ||
print("Temperature command after : ", get_expected_tmp(conn)) | ||
print("Hygro command after : ", get_expected_hygro(conn)) | ||
print("===CURRENT VALUES===") | ||
print("Current temperature : ", get_actual_tmp(conn)) | ||
print("Current Hygro : ", get_actual_hygro(conn)) |