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
Python re
Nov 27, 2020
send one gcode
How to send one gcode command over USB?
import serial
ser = serial.Serial("/dev/ttyUSB0", 115200)
ser.write("G28\n")
I have read over 20 forum pages with simular problems and tried their answers such as changing the baud rate to 250000 and the following changes to the write function parameter:
ser.write("G28\r\n")
ser.write(b'G28\r\n')
ser.write(b'G28\n')
ser.write(b'G28')
ser.write("G28") have tried all these combinations, and I have also added:
time.sleep(5)
along with the relevant import statement for the time module at the top of my file. I added this line of code between my ser declaration and my ser.write function call.
I have also tried adding:
ser.close()
to see if that would make a difference but it has not, as I know this is best practice anyway.
No matter what combination of this code I used, when I run my python script my printer seems to restart (the screen changes from the home page to the opening wanhao logo and back to the home page)
I look forward to any help anyone can give me in regards to my code and what I may be doing wrong.
read_potentiometer
read_potentiometer.ino
import serial
import time
# set up the serial line
ser = serial.Serial('COM3', 9600)
time.sleep(2)
# Read and record the data
data =[] # empty list to store the data
for i in range(50):
b = ser.readline() # read a byte string
string_n = b.decode() # 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
ser.close()
read_potentiometer.ino
// potentiometer_read.ino
// reads a potentiometer and sends value over serial
int sensorPin = A0; // The potentiometer on pin 0
int ledPin = 13; // The LED is connected on pin 13
int sensorValue; // variable to stores data
void setup() // runs once when the sketch starts
{
// make the LED pin (pin 13) an output pin
pinMode(ledPin, OUTPUT);
// initialize serial communication
Serial.begin(9600);
}
void loop() // runs repeatedly after setup() finishes
{
sensorValue = analogRead(sensorPin); // read pin A0
Serial.println(sensorValue); // send data to serial
if (sensorValue < 500) { // less than 500?
digitalWrite(ledPin, LOW); } // Turn the LED off
else { // greater than 500?
digitalWrite(ledPin, HIGH); } // Keep the LED on
delay(100); // Pause 100 milliseconds
}
Nov 25, 2020
read/write
Read/Write
===========================
i = 1
while i < 600:
print(i)
i += 100
else:
#print("i is no longer less than 6")
print(i)
=================================
#for x in range(1,6,2):
#for x in range(1,6):
# print(x)
=============================
def my_function():
print("Hello from a function")
my_function()
====================================
f = open("demofile.txt", "r")
print(f.read())
=======================================
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
============================================
Nov 24, 2020
Python1
Python
__str__
if __name__ == '__main__':
import module/class
if/else
for loop
range