5
5
import time
6
6
import threading
7
7
8
+
9
+ #Definição dos estados e eventos do sistema
8
10
statesNum = 5
9
11
eventsNum = 7
10
12
23
25
"RESUME" :5 ,
24
26
"STOP" :6 }
25
27
28
+
29
+ #Criação da matriz de transição da máquina de estados
26
30
transitionMatrix = [[j for i in range (eventsNum )] for j in range (statesNum )]
27
31
28
32
transitionMatrix [states ["WAITING_TRAJ" ]][events ["TRAJ_LOADED" ]] = states ["WAITING_ORIGIN" ]
33
37
transitionMatrix [states ["PAUSED" ]][events ["RESUME" ]] = states ["RUNNING" ]
34
38
transitionMatrix [states ["PAUSED" ]][events ["STOP" ]] = states ["NOT_RUNNING" ]
35
39
40
+ #Habilitação dos botões para cada estado
36
41
buttonStates = [[] for _ in range (statesNum )]
37
42
38
43
buttonStates [states ["WAITING_TRAJ" ]]= [ctk .DISABLED , ctk .DISABLED , ctk .DISABLED , ctk .DISABLED , ctk .DISABLED , ctk .NORMAL , ctk .NORMAL ]
49
54
global linhaAtual
50
55
linhaAtual = 0
51
56
57
+
58
+ #Função de processamento das respostas seriais: gera eventos, realiza a transição de estados e altera a interface
52
59
def processResponse (response ):
53
60
global state
54
61
global linhaAtual
@@ -85,6 +92,7 @@ def processResponse(response):
85
92
86
93
buttons = buttonStates [state ]
87
94
95
+ #Atualização dos botões
88
96
start_button .configure (state = buttons [0 ])
89
97
pause_button .configure (state = buttons [1 ])
90
98
resume_button .configure (state = buttons [2 ])
@@ -93,17 +101,18 @@ def processResponse(response):
93
101
file_button .configure (state = buttons [5 ])
94
102
params_button .configure (state = buttons [6 ])
95
103
96
-
104
+ #Cálculo do LRC da comunicação MODBUS
97
105
def calculate_lrc (data ):
98
106
lrc = 0
99
107
for byte in data :
100
- lrc = (lrc + byte ) & 0xFF # Sum the bytes and keep only the least significant byte
108
+ lrc = (lrc + byte ) & 0xFF
101
109
lrc -= 0x3A
102
- lrc = ((lrc ^ 0xFF ) + 1 ) & 0xFF # Two's complement
110
+ lrc = ((lrc ^ 0xFF ) + 1 ) & 0xFF
103
111
104
112
lrc_string = hex (lrc )[2 :]
105
113
return lrc_string
106
114
115
+ #Inicialização do canal serial
107
116
def init_serial ():
108
117
global ser
109
118
ports = list (serial .tools .list_ports .comports ())
@@ -125,106 +134,96 @@ def init_serial():
125
134
else :
126
135
print ("COM7 is not available" )
127
136
137
+
138
+ #Função de envio de comandos simples
128
139
def send_command (command ):
129
140
global ser
130
141
global blockFlag
131
142
132
-
133
- blockFlag = True
134
- ser .write (command )
143
+ blockFlag = True #Bloqueia o serial enquanto envia
144
+ ser .write (command ) #Envia o comando
135
145
print ("Sent to COM7 ->" , command )
136
-
137
- # Optionally, read the response from the device (if needed)
138
- response = ser .read (16 ) # Adjust the number of bytes to read as needed
139
- blockFlag = False
140
- processResponse (response )
146
+ response = ser .read (16 ) #Lê a resposta
147
+ blockFlag = False #Libera o serial
148
+ processResponse (response ) #Processa a mensagem recebida
141
149
if response :
142
150
print ("Received response:" , response )
143
151
144
152
145
153
146
154
147
-
155
+ #Envio da string de trajetória completa
148
156
def send_trajectory ():
157
+ #Utiliza o parseGCode para gerar a string de trajetória a ser enviada
149
158
file , pointsNumber , trajectoryString = parseGCode ()
150
159
pointsNumber = "{:02X}" .format (pointsNumber )
151
160
global ser
152
- # Send the data
153
- message = b':0115'
154
- message += pointsNumber .encode ()
155
- message += trajectoryString .encode ()
156
- message += calculate_lrc (message ).encode ()
157
- message += b'\x0D \x0A '
158
- ser .write (message )
159
- print (message )
160
-
161
161
162
- # Optionally, read the response from the device (if needed)
162
+ message = b':0115' #Enderço e código da função 21 = 0x15
163
+ message += pointsNumber .encode () #Número de pontos enviado
164
+ message += trajectoryString .encode () #String de pontos que compõe a trajetória
165
+ message += calculate_lrc (message ).encode () #LRC para a mensagem
166
+ message += b'\x0D \x0A ' #Terminadores
167
+ ser .write (message ) #Envia a mensagem
163
168
time .sleep (3 )
164
- response = ser .read (100 ) # Adjust the number of bytes to read as needed
165
-
166
- processResponse (response )
169
+ response = ser .read (100 )
170
+ processResponse (response ) #Processa a resposta
167
171
168
172
if response :
169
173
print ("Received response:" , response )
170
- textbox .delete ("0.0" , "end" ) # delete all text
174
+ #Atualiza o texto no campo de código de G da interface
175
+ textbox .delete ("0.0" , "end" )
171
176
lineNumber = 0
172
177
for line in file :
173
178
textbox .insert (f"{ lineNumber } .0" , line )
174
179
lineNumber += 1
175
180
textbox .tag_remove ("highlight" , "0.0" , "end" )
176
181
182
+ #Envio dos parâmetros de controle
177
183
def send_params ():
178
184
global ser
179
- # Send the data
180
- message = b':010806'
185
+ message = b':010806' #Endereço, código da função de número de parâmetros
186
+ #Inclusão dos 6 parâmetros de controle
181
187
message += ("{:04.1f}" .format (float (kpA .get ("0.0" , "end" )))).encode ()
182
188
message += ("{:04.1f}" .format (float (kiA .get ("0.0" , "end" )))).encode ()
183
189
message += ("{:04.1f}" .format (float (kdA .get ("0.0" , "end" )))).encode ()
184
190
message += ("{:04.1f}" .format (float (kpB .get ("0.0" , "end" )))).encode ()
185
191
message += ("{:04.1f}" .format (float (kiB .get ("0.0" , "end" )))).encode ()
186
192
message += ("{:04.1f}" .format (float (kdB .get ("0.0" , "end" )))).encode ()
187
- message += calculate_lrc (message ).encode ()
188
- message += b'\x0D \x0A '
193
+ message += calculate_lrc (message ).encode () #Cálculo do LRC
194
+ message += b'\x0D \x0A ' #Terminadores
189
195
ser .write (message )
190
196
print (message )
191
-
192
-
193
- # Optionally, read the response from the device (if needed)
194
197
time .sleep (1 )
195
- response = ser .read (100 ) # Adjust the number of bytes to read as needed
196
-
197
- #processResponse(response)
198
-
198
+ response = ser .read (100 )
199
+ processResponse (response )
199
200
if response :
200
201
print ("Received response:" , response )
201
202
202
203
203
-
204
+ #Leitura da linha sendo executada
204
205
def request_currLine ():
205
- # Send the data
206
206
global ser
207
207
global linhaAtual
208
208
global blockFlag
209
209
210
210
if (not blockFlag ):
211
- ser .write (b':0103000379' + b'\x0D \x0A ' )
212
- response = ser .read (14 ) # Adjust the number of bytes to read as needed
211
+ ser .write (b':0103000379' + b'\x0D \x0A ' ) #Código para a função de ler REG_LINHA
212
+ response = ser .read (14 )
213
213
processResponse (response )
214
214
if response :
215
215
print ("Current line:" , linhaAtual )
216
216
217
217
218
218
219
219
220
- # Initialize the CustomTkinter application
220
+ #Inicialização da Interface Gráfica
221
221
app = ctk .CTk ()
222
222
app .geometry ("720x500" )
223
223
app .title ("COM7 Sender" )
224
224
ctk .set_appearance_mode ("dark" )
225
225
226
-
227
-
226
+ #Definição dos botões
228
227
start_button = ctk .CTkButton (app , text = "INICIAR" , command = lambda :send_command (b':0106000178' + b'\x0D \x0A ' ))
229
228
start_button .grid (row = 1 , column = 0 , pady = 10 , padx = 10 )
230
229
pause_button = ctk .CTkButton (app , text = "PAUSAR" , command = lambda :send_command (b':0106010177' + b'\x0D \x0A ' ))
@@ -240,8 +239,8 @@ def request_currLine():
240
239
params_button = ctk .CTkButton (app , text = "ENVIAR PARÂMETROS" , command = send_params )
241
240
params_button .grid (row = 5 , column = 2 ,columnspan = 3 , pady = 10 , padx = 10 )
242
241
242
+ #Atribuição do estado inicial aos botões
243
243
buttons = buttonStates [state ]
244
-
245
244
start_button .configure (state = buttons [0 ])
246
245
pause_button .configure (state = buttons [1 ])
247
246
resume_button .configure (state = buttons [2 ])
@@ -250,13 +249,10 @@ def request_currLine():
250
249
file_button .configure (state = buttons [5 ])
251
250
params_button .configure (state = buttons [6 ])
252
251
253
- # Create a StringVar to hold the label text
252
+ #Criação dos campos de texto de exibição da linha atual e do código G
254
253
linha_var = ctk .StringVar (value = "Linha atual: " + str (linhaAtual ))
255
-
256
- # Create a label to display the global variable value
257
254
linha_label = ctk .CTkLabel (app , textvariable = linha_var )
258
255
linha_label .grid (row = 0 , column = 1 , rowspan = 1 , pady = 10 , padx = 10 )
259
-
260
256
textbox = ctk .CTkTextbox (app , width = 300 , height = 400 )
261
257
textbox .grid (row = 1 , column = 1 ,rowspan = 7 , pady = 10 , padx = 10 )
262
258
textbox .tag_config ("highlight" , background = "#0077dd" )
@@ -301,8 +297,6 @@ def request_currLine():
301
297
302
298
303
299
304
-
305
-
306
300
def background_loop ():
307
301
while True :
308
302
if ((state == states ["RUNNING" ]) & (not blockFlag )):
0 commit comments