C:\Users\hmill>python -m pip install pyserial
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pyserial in c:\users\hmill\appdata\roaming\python\python312\site-packages (3.5)
# pyThread111924
# Nov 21,2024 timer function not working
# Time : 2:10pm
#11/9/2024
#PyQt5HMUSB071724.py
# oct 12, 2024
#C:\Users\admin\PycharmProjects\pyHMUSB_1
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow,QMessageBox,QDialog
from PyQt5.uic import loadUi
#from PyQt5.QtCore import QTimer
import sys
#from threading import Thread
import threading
import datetime
from threading import Event
import time
from time import sleep
from threading import Thread
import queue
#from pyHMUSB import add2,OpenPort,PortClose
#from myModuleSerial import add
#import pyHMUSB
from pyHMUSB import *
# seriasl Port
import keyboard
import serial
# initilizee
q =queue.Queue() # fifo
message ="hello"
q.put(message)
# Global variable for RS232 read
Gloabal_RS232Read =0
#../
#
event = Event()
timerEnable =1
class MainUIxy(QMainWindow):
def __init__(self):
super(MainUIxy, self).__init__()
loadUi("myTestQt5.ui", self) # Load the UI file
self.pushButtonXHome.clicked.connect(XHomeAxis)
self.XJOG_PB.clicked.connect(XJOGAxis)
#self.StartTimerPB.connect(StatTimerCallBack)
self.ThreadON_PB.clicked.connect(Thread_ON)
self.ThreadOFF_PB.clicked.connect(Thread_OFF)
self.PBStartTimer.clicked.connect(StatTimerCallBack)
baudrate = 115200
print(add2(7,4))
print(baudrate)
serialPort = serial.Serial(port='COM3', baudrate=baudrate, parity=serial.PARITY_NONE, bytesize=8, timeout=1,
stopbits=serial.STOPBITS_ONE)
def Thread_ON():
print("thread ON")
#OpenSerialPort()
t1 = Thread(target=myThreadRS232Function, args=(event,q))
t1.start()
print("thread Ok")
def Thread_OFF():
print("thread OFF")
print(q.get())
print(q.get())
timer.join()
serialPort.close()
# stop the worker thread
print('Main stopping thread')
event.set()
# wait for the new thread to finish
t1.thread.join()
# Wait for the timer to finish
timer.join()
def StatTimerCallBack():
#timerEnable = 7
#print("set timer start")
#Thread(target=myThreadRS232Function, args=(event,q))
# Create a timer that triggers after 5 seconds
#timer = threading.Timer(1.0, my_function)
#timer.start()
print("CallBackTimerF")
while len(q.get()) > 0:
# length = len(q.get())
# if (length == 0):
# break
print(f"my_function: {q.get()} output")
# print("data Event triggered!")
# print(q.get())
# message = q.get()
# print(message['sr']['posx'])
# define myThreadRS232Function
def myThreadRS232Function(event,q):
global Gloabal_RS232Read
while True:
Gloabal_RS232Read += 1
#print(f'{Gloabal_RS232Read}')
message = serialPort.readline()
length = len(message)
if (length > 0):
print(f"myThread: {message} ok")
q.put(message)
#print(message['sr']['posx'])
#sleep(1)
if event.is_set():
break
def my_function():
print("my_function-Timer event")
timer = threading.Timer(3.0, my_function)
timer.start()
def callback_function():
length = len(q.get())
if (length > 0):
print("data Event triggered!")
print(q.get())
message =q.get()
message['sr']['posx']
def XHomeAxis():
serialString = ""
serialPort.write(bytearray('{"gc":"G28.2 X0"}''\r\n','ascii'))
print("test")
print(Gloabal_RS232Read)
while True:
try:
message = serialPort.readline()
print(message)
if keyboard.is_pressed('q'):
print("quit")
break
except KeyboardInterrupt:
break
XPosition =0.300
def XJOGAxis(): #F6
serialString = ""
global XPosition
XPosIncrement = 0.200
#round(XPosition, 2)
serialPort.write(bytearray('{"gc":"G1 F150 X'+str(round(XPosition,2)) +' Y0.000 A0.000"}''\r\n', 'ascii'))
#serialPort.write(bytearray('{"gc":"G1 F150 X0.400 Y0.000 A0.000"}''\r\n', 'ascii'))
#serialPort.write(bytearray('{"gc":"G28.2 X0"}''\r\n', 'ascii'))
XPosition = XPosition +XPosIncrement
print("XJOG-test")
while True:
try:
message = serialPort.readline()
print(message)
if keyboard.is_pressed('q'):
print("quit")
break
except KeyboardInterrupt:
break
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainUIxy()
ui.show()
app.exec_()
print("done-1")
serialPort.close()
# wait for the new thread to finish
t1.thread.join()
# Wait for the timer to finish
timer.join()
#REFERENCE
#https://superfastpython.com/stop-a-thread-in-python/
References
\Users\admin\PycharmProjects\pyHMUSB_1
../
./
./
python nested dicionary find pairs
https://www.geeksforgeeks.org/python-nested-dictionary/
../
## declaring variables
name = "Datacamp"
type_of_company = "Educational"
## enclose your variable within the {} to display it's value in the output
print(f"{name} is an {type_of_company} company.")
../
https://discuss.python.org/t/what-actually-while-true-statement-mean-can-someone-pull-me-out-this/15382/3../
# PYReadSerialPort
# date 11/11/2024
import sys
import time
import serial
ser = serial.Serial("COM5", 9600)
try:
ser.write(bytearray('{"gc":"G28.2 X0"}''\r\n','ascii'))
#x = ser.readline()
#print(x)
except:
print ("error")
# exit()
if(ser.is_open):
try:
while(True):
reading = ser.readline()
print(reading)
time.sleep(0.25)
except Exception:
print("exception serial read")
else:
print("notOpne")
print("done")
#REFERENCE
#serialPort = serial.Serial(port='COM5', baudrate=baudrate, parity=serial.PARITY_NONE, bytesize=8, timeout=1,
# stopbits=serial.STOPBITS_ONE)
pip install pyserial
sudo apt-get install pyserial ; rasperry pi
C:\Users\hmill>pip install pyserial
Defaulting to user installation because normal site-packages is not writeable
Collecting pyserial
Downloading pyserial-3.5-py2.py3-none-any.whl.metadata (1.6 kB)
Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
Installing collected packages: pyserial
WARNING: The scripts pyserial-miniterm.exe and pyserial-ports.exe are installed in 'C:\Users\hmill\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pyserial-3.5
C:\Users\hmill>python -m pip install pyserial
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pyserial in c:\users\hmill\appdata\roaming\python\python312\site-packages (3.5)
[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
# PYReadSerialPort
# date 11/11/2024
import sys
# seriasl Port
#import keyboard
import serial
#serialPort = serial.Serial(port='COM5', baudrate=baudrate, parity=serial.PARITY_NONE, bytesize=8, timeout=1,
# stopbits=serial.STOPBITS_ONE)
try:
ser = serial.Serial("COM5", 9600)
print("port Open")
except:
print ("error")
exit()
print("done")