-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommunication.py
103 lines (82 loc) · 2.66 KB
/
communication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Coder : ENG.Mahmoud | Eng.Gehad
Version : v2.0B
version Date : 19 / 5 / 2023
Code Type : python
Title : Smart Parking System
Interpreter : cPython v3.11.0 [Compiler : MSC v.1933 AMD64]
"""
import sys
import serial
import time
import tkinter as tk
import serial.tools.list_ports
com = None
def com_page(error=0):
page = tk.Tk()
page.geometry(f"400x150+500+200")
page.title("Controller Com page")
page.iconbitmap('icon_1.ico')
if(error==1):
tk.Label(page, text='Wrong com', font=('Arial', 10),fg='red').place(x=5, y=5)
l_e = tk.Label(page,text='Enter Com Port',font=('Arial', 14, 'bold'))
l_e.place(x=130,y=20)
entry = tk.Entry(page,)
entry.place(x=110,y=60,height=25,width=200)
global com
com = None
def get_val():
global com
com = entry.get()
page.destroy()
b = tk.Button(page,text="Enter",
font=('Arial', 14, 'bold'),
bg='#04B400',
fg='white',
borderwidth=0,command=get_val)
b.place(x=180,y=100)
page.mainloop()
ports = list(serial.tools.list_ports.comports())
for port in ports:
if "Arduino" in port.description:
com = port.device
if(com==None): com_page()
while(1):
try:
ser = serial.Serial(com, 9600)
break
except:
com_page(1)
if(com==None): break
if(com==None): sys.exit()
def prepare_for_parknig(park_n):
# open serial communication port
# the COM number differs from one device to another
time.sleep(3)
# time_delay to ensure the serial port is ready for communication
data_sent= '1' + chr(park_n)
ser.write(data_sent.encode('Ascii'))
ser.flush() #wait to ensure data is sent
while True:
if(ser.in_waiting): # wait until data is available on cpu serial port
data_received=str(ser.read(1),'UTF-8')
# the serial.read is capable of reading up to 100 bytes
if(data_received=='D'): break
def park(park_n):
time.sleep(3)
data_sent = '2' + chr(park_n)
ser.write(data_sent.encode('Ascii'))
ser.flush()
while True:
if (ser.in_waiting):
data_received = str(ser.read(1), 'UTF-8')
if (data_received == 'D'): break
def getcar(park_n):
time.sleep(3)
data_sent= '3'+ chr(park_n)
ser.write(data_sent.encode('Ascii'))
ser.flush()
while True :
if(ser.in_waiting):
data_received = str(ser.read(1),'UTF-8')
if(data_received=='D'): break