-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.py
304 lines (280 loc) · 9.5 KB
/
snippets.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# --------------------------------------------------------------
# SNIPPETS
# --------------------------------------------------------------
import functools
def move(func):
"""Process parameters of correlating axis"""
@functools.wraps(func)
def wrapper_move(*args, **kwargs):
value = func(*args, **kwargs)
return value
return wrapper_move
@move
def move_x(mm_wanted_x, v_wanted_x): # linear moving X
print('Linear moving X=' + mm_wanted_x + '[mm] ' + 'with Feedrate=' + v_wanted_x + '[mm/min]')
str.encode('F' + str(v_wanted_x) + ' G0 X ' + str(mm_wanted_x) + '\n')
def move_y(mm_wanted_y, v_wanted_y): # linear moving Y
print('Linear moving Y=' + mm_wanted_y + '[mm] ' + 'with Feedrate=' + v_wanted_y + '[mm/min]')
str.encode('F' + str(v_wanted_y) + ' G0 X ' + str(mm_wanted_y) + '\n')
def move_z(mm_wanted_z, v_wanted_z): # linear moving Z
print('Linear moving Z=' + mm_wanted_z + '[mm] ' + 'with Feedrate=' + v_wanted_z + '[mm/min]')
raw_command_z = str.encode('F' + str(v_wanted_z) + ' G0 Z ' + str(mm_wanted_z) + '\n')
print(move_x('50', '2000'))
print(move_y('150', '1500'))
print(move_z('50', '1000'))
# python3 -m serial.tools.list_ports
# homing = 'G28\n' # better solution to encode string to bytes
# ser.write(str.encode(my_dict[data.data]))
# raw_command = move_printer(bytes([mm_wanted_x]))
# raw_command = move_printer(mm_wanted_x.to_bytes(2, byteorder='little'))
# print('inline bytes waiting: ' + str(ser.in_waiting))
# print('Do some magic')
# print('Home:')
# ser.write(str.encode(my_dict[data.data]))
# print('Wait 1s')
# time.sleep(1)
# print('Move X!')
# ser.write(bytes(b'G0 X100 F0\n'))
# ser.write(bytes(b'G91'))
# print('Move X!')
# print(move_printer(bytes(mm_wanted_x)))
# ser.write(str.encode(my_dict[data.data]))
# print('Moving!')
# print('Call Read positions:')
# ser.write(bytes(b'M114\n'))
# print('Read serial:')
# print(ser.read())
# print('Move Y!')
# ser.write(bytes(b'G0 Y100.0 F0\n'))
# # ser.write(bytes(b'G90'))
# print('Sleep for 1s')
# time.sleep(1)
# print('Move -X!')
# ser.write(bytes(b'G0 X-100.0 F0\n'))
# print('Home again:')
# # ser.write(bytes(b'G28\n'))
# ser.write(str.encode(my_dict[data.data]))
# print('Wake up')
# print('inline bytes waiting: ' + str(ser.in_waiting))
# time.sleep(1)
# print('Sleep for 1s')
# time.sleep(1)
# print('inline bytes waiting: ' + str(ser.in_waiting))
# raw_command = str.encode('G0 X 100 ' + 'F0\n')
# my_dict = {'homing': 'G28\n',
# 'run': 'do',
# 'taille': 1.75}
# usf.homing_printer() # homing printer
# print('serial is open: ' + str(ser.is_open))
# usf.open_serial_port() # open serial port
# print('Printer online')
# print('inline bytes waiting: ' + str(ser.in_waiting))
# time.sleep(8)
# ser.write(b'G2 I20 J20\n') # Move in a complete clockwise circle with the center offset
# time.sleep(6) # from the current position by [20, 20]
# ser.write(b'G2 I-20 J20\n')
# time.sleep(6)
# ser.write(b'G2 I-20 J-20\n')
# time.sleep(6)
# ser.write(b'G2 I20 J-20\n')
# ports = list(serial.tools.list_ports.comports())
# for p in ports:
# short = usf.get_port()
# sw = short.device
# port_name = short.strip(' - Ultimaker 2.0')
# string = usf.get_port()
# port_name = string.strip(' - Ultimaker 2.0')
# print(string)
# duty = 0.0
# try:
# while 1:
# while duty < 1.0:
# ser.write(bytes(b'M42 S0\n'))
# led.write(duty)
# duty += 0.05
# time.sleep(0.1)
# while duty > 0.0:
# led.write(duty)
# duty -= 0.05
# time.sleep(0.1)
# except KeyboardInterrupt:
# pass
# ser.reset_input_buffer()
# ser.reset_output_buffer()
# ser.write(bytes(b'M105\n'))
# ser_bytes = ser.readline()
# print(ser_bytes)
# a = codecs.decode(ser_bytes, "ISO-8859-1").decode(ser_bytes, "UTF-8")
# print(a)
# decoded_bytes = float(ser_bytes[0:len(ser_bytes) - 2].decode("ascii"))
# print(decoded_bytes)
# b = ser.read_until(terminator='\n')
# a = ser.readlines()
# print(a)
# time.sleep(3)
# b = a.decode('utf-8')
# a = ser.read_until(terminator='\n')
# print(b)
# # print(port_chosen)
# print('Enter your commands below.\r\nInsert "exit" to leave the application.')
# # get keyboard input
# user_in = input()
# if input == 'exit':
# ser.close()
# exit()
# else:
# # send the character to the device
# # (note that I append a \r\n carriage return and line feed to the characters - this is requested by my device)
# ser.write(str.encode(user_in + '\n'))
# out = ''
# # let's wait one second before reading output (let's give device time to answer)
# time.sleep(1)
# while True:
# bytes_to_read = ser.in_waiting
# ser.read(bytes_to_read)
# # while ser.inWaiting() > 0:
# # # b"abcde".decode("utf-8")
# # data = ser.read_until()
# # # out = data.decode('utf-8')
# # out = data.decode('latin-1')
# if bytes_to_read != '':
# print('>>' + out)
#
# print('Flushing Data')
# ser.flush()
# time.sleep(7)
# ser.write(bytes(b'M105\n'))
# ser_bytes = ser.readline()
# print(ser_bytes)
# a = codecs.decode(ser_bytes, "ISO-8859-1").decode(ser_bytes, "UTF-8")
# print(a)
# decoded_bytes = float(ser_bytes[0:len(ser_bytes) - 2].decode("ascii"))
# print(decoded_bytes)
# b = ser.read_until(terminator='\n')
# a = ser.readlines()
# print(a)
# time.sleep(3)
# b = a.decode('utf-8')
# a = ser.read_until(terminator='\n')
# print(b)
# # print(port_chosen)
# print('Enter your commands below.\r\nInsert "exit" to leave the application.')
# # get keyboard input
# user_in = input()
# if input == 'exit':
# ser.close()
# exit()
# else:
# # send the character to the device
# # (note that I append a \r\n carriage return and line feed to the characters - this is requested by my device)
# ser.write(str.encode(user_in + '\n'))
# out = ''
# # let's wait one second before reading output (let's give device time to answer)
# time.sleep(1)
# while True:
# bytes_to_read = ser.in_waiting
# ser.read(bytes_to_read)
# # while ser.inWaiting() > 0:
# # # b"abcde".decode("utf-8")
# # data = ser.read_until()
# # # out = data.decode('utf-8')
# # out = data.decode('latin-1')
# if bytes_to_read != '':
# print('>>' + out)
#
# print('Flushing Data')
# ser.flush()
# time.sleep(7)
# time.sleep(6) # logg some data
# data = [] # empty list to store the data
# for i in range(50):
# b = ser.readline() # read a byte string
# string_n = b.decode(encoding='UTF-8', errors='strict') # decode byte string into Unicode
# string = string_n.rstrip() # remove \n and \r
# flt = float(string) # convert string to float
# print(flt)
# data.append(flt) # add to the end of data list
# time.sleep(0.1) # wait (sleep) 0.1 seconds
#
# for line in data:
# print(line)
# p = printcore('/dev/ttyACM0', 250000)
# print('Sending M105')
# p.send_now('M154 S1')
# time.sleep(7)
# print('Sending G28')
# p.send_now('G28')
# p.send_now('M84')
# time.sleep(7)
# p.disconnect()
# movement = {}
# time.sleep(6)
# brightness_wanted = '30' # set LED brightness str[0-255]
# ser.write(usf.set_light(brightness_wanted))
# movement.mm_wanted_y = '-100' # move y-axis
# movement.v_wanted_y = '2500' # set feedrate
# ser.write(bytes(b'G91\n'))
# ser.write(usf.move_printer_y(movement)
# ser.write(bytes(b'G90\n'))
# ser.write(bytes(b'M114\n'))
# ser.write(bytes(b'M105\n'))
# time.sleep(0.1) # give the printer time to receive data
# printer_feedback = ser.readall()
# printer_feedback_decoded = printer_feedback.decode('utf-8')
# printer_feedback_decoded_list = printer_feedback_decoded.split()
# print("Temperature: ", printer_feedback_decoded_list[1])
# while True:
# try:
# ser.write(bytes(b'M105\n'))
# time.sleep(2) # give the printer time to receive data
# printer_feedback = ser.readall()
# printer_feedback_decoded = printer_feedback.decode('utf-8')
# printer_feedback_decoded_list = printer_feedback_decoded.split()
# print("Temperature: ", printer_feedback_decoded_list[1])
# with open("test_data.csv", "a") as f:
# writer = csv.writer(f, delimiter=",")
# # writer.writerow([time.time(), printer_feedback_decoded_list[1]])
# writer.writerow([time.strftime("%Y-%m-%d %H:%M:%S", gmtime()), printer_feedback_decoded_list[1]])
# except KeyboardInterrupt:
# print("Keyboard Interrupt")
# break
# print('Flushing Data')
# ser.flush()
# time.sleep(5)
# ser.write(bytes(b'M105\n'))
# ser.write(bytes(b'M117 Hello World!\n'))
# time.sleep(0.75)
# ser.write(bytes(b'G0 X 100\n'))
# ser.write(bytes(b'M105\n'))
# time.sleep(8)
# ser.write(bytes(b'M105\n'))
# time.sleep(0.75)
# ser.write(bytes(b'M105\n'))
# time.sleep(0.75)
# ser.write(bytes(b'M105\n'))
# time.sleep(0.75)
# ser.write(bytes(b'M114\n'))
# time.sleep(1)
# "Try to read line from buffer"
# a = ser.readall()
# b = a.decode('utf-8')
# print('decoded readall: ')
# print(b)
# ser_bytes = ser.readall()
# print('ser_bytes')
# print(ser_bytes)
# a = ser_bytes.decode('latin-1')
# print(a)
# # decoded_bytes = float(ser_bytes[2:len(ser_bytes) - 2].decode("ascii"))
# decoded_bytes = ser_bytes[3:len(ser_bytes) - 2].decode("ascii")
# print(decoded_bytes)
# time.sleep(7)
# brightness_wanted = '125' # set LED brightness str[0-255]
# ser.write(usf.set_light(brightness_wanted))
# ser.write(usf.homing_printer()) # home X,Y,Z
# ser.write(bytes(b'M155 S1\n'))
# time.sleep(0.75)
# a = ser.readall()
# b = a.decode('utf-8')
# print('decoded readall: ')
# print(b)