still under development for arduino grbl
Dec 19, 2020
Dec 13, 2020
Dec 6, 2020
Dec 4, 2020
Freecad
Dec 3, 2020
This repository is for those who would like to learn how to use the Python module PySerial and Arduino.
There will be a corresponding Arduino (.ino) and Python (.py) file for each part. Each part builds upon the previous.
View the YouTube Play List for step by step instruction and detailed instructions:
https://www.youtube.com/playlist?list=PLb1SYTph-GZJb1CFM7ioVY9XJYlPVUBQy
All code
Code
saves = open('data.txt', 'w')
saves.write(path)
-----------------------------------------
def chooseDir():
global path
path = filedialog.askdirectory(title="Choose a download directory")
tk.Label(root, text=path, bg='gray').place(x=240,y=300)
saves()
---------------------------------------------------
saves = open('data.txt', 'w')
--------------------------------------------
tk_buttom
Button(root,text="Download video", width=20, bg='black',fg="white", command=downloadVideo).place(x=240, y=130)
Button(root,text="Choose location", width=20, bg='black',fg="white", command=chooseDir).place(x=240, y=160)
Label_3=Label(root, text="Curent location: ", font=("bold"), bg='gray')
Label_3.place(x=250, y=245)
Dec 1, 2020
Serial python
The response always ends with the FF byte. So i want to read the bytes into a buffer untill i encounter FF. Than the buffer should be printed and the function should return.
import serial
s_port = 'COM1'
b_rate = 2400
#method for reading incoming bytes on serial
def read_serial(ser):
buf = ''
while True:
inp = ser.read(size=1) #read a byte
print inp.encode("hex") #gives me the correct bytes, each on a newline
buf = buf + inp #accumalate the response
if 0xff == inp.encode("hex"): #if the incoming byte is 0xff
print inp.encode("hex") # never here
break
return buf
#open serial
ser = serial.Serial(
port=s_port,
baudrate=b_rate,
timeout=0.1
)
while True:
command = '\x05\x06\x40\x00\x02\x05\xF6\x5C' #should come from user input
print "TX: "
ser.write(command)
rx = read_serial(ser) print "RX: " + str(rx)
===============================================================
python serial read
ตอนนี้จะเกี่ยวกับการอ่านค่า ที่ได้รับจาก serial port
ตัวอย่าง code
#! /usr/bin/python
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=19200, timeout=1.0)
i = 1
while 1:
port.write(chr(i))
rcv = port.read(1) ================== (1)
if rcv != '':================== (2)
ans = ord(rcv) ================== (3)
if ans == 0x61:
print "Status Ready"
else:
print rcv
else: ================== (4)
print "Loss communication"
(1) คืออ่านค่าจาก serial port ใส่ในตัวแปร rcv
(2) ตรวจสอบว่าค่า rcv ที่อ่านมา มีค่าอะไรอยู่หรือไม่ ถ้ามีก็ทำต่อไป ถ้าไม่มีก็กระโดดไปที่ (4)
(3) เปลี่ยนตัวแปร rcv ที่เป็น string ให้เป็น int เพื่อที่เราจะสามารถเปรียบเทียบค่ากับ hex ที่เราต้องการได้
ที่มา http://stackoverflow.com/questions/12929593/how-to-test-for-hexidecimal-output-on-serial-port-in-python