'''
pyHM_Rport
11/27/2-24
task read serial prot
'''
# QT Designer interface
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow,QMessageBox,QDialog
from PyQt5.uic import loadUi
# treading
import threading
from threading import Event
# QT Interface gUI
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.ThreadON_PB.clicked.connect(Thread_ON)
self.ThreadOFF_PB.clicked.connect(Thread_OFF)
self.PBStartTimer.clicked.connect(StarTimerCallBack)
# QT Callback
def XHomeAxis():
print("XHomeAxis\n")
def XJOGAxis(): #
print("XJOG-test reqested \n")
def Thread_ON():
print("thread requested\n")
def Thread_OFF():
print("thread OFF\n")
def my_function():
print("my_function-Timer event\n")
def StarTimerCallBack():
print("StatTimerCallBack")
# Runnning app
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainUIxy()
ui.show()
app.exec_()
REFRENCE
Wed, 11/27/2024
../
def myThreadRS232Function(event,q):
global Gloabal_RS232Read
print("thread Active\n")
while True:
Gloabal_RS232Read += 1
#print(f'{Gloabal_RS232Read}')
message = serialPort.readline()
length = len(message)
if (length > 0):
#print(type(message)) # converting
print("length =" + length)
output = codecs.decode(message) # display # outputprint(type(output))
print('Output: ' + output)
x = output.count('sr')
print(x)
if y > 0:
# convert to dic, convert python string to dictionaries
json_string = output
dictionary = json.loads(json_string)
print(dictionary['sr']['posx'])
print(f"myThread: {message} ok")
q.put(message)
11/27/24
# File pyJMDic
# Date Nov 25,2024
# Task test nested dic
#print(type(b'{r,"f",0137})
'''
x = b'{r,"f",0137}\n' # class bytes
print(type(x))
'''
# Program for converting bytes to string using decode()
# import required module
import codecs
import json
data = b'{r,"f",0137}\n'
data = b'{"sr":{"posx":0.700,"vel":0.00,"stat":3}}\n'
#data = b'{"qr":31,"qi":1,"qo":0}\n'
# display input
print(type(data))
# converting
output = codecs.decode(data)
# display output
print(type(output))
print('Output: ' +output)
x = output.count('sr')
print(x)
# convert to dic, convert python string to dictionaries
json_string = output
#json_string = '{"name": "John", "age": 30}'
dictionary = json.loads(json_string)
print(dictionary['sr']['posx'])
'''
pyHM_Bytes_112624
# test bytes
Input: 2
Output: b'\x00\x00'
..
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
'''
from ast import literal_eval
# Define a byte string
#byte_string = b'{r,"f",0137}'
byte_string = b'{"sr":{"posx":0.300,"vel":0.00,"stat":3}}\n'
#byte_string = b'{"qr":32,"qi":0,"qo":1}\n'
# Convert the byte string to a string using the decode() method
decoded_string = byte_string.decode("utf-8")
y = decoded_string.count("sr",1,5)
if y >0:
# Print the decoded string
print(f"output: {decoded_string}")
print(f'type: {type(decoded_string)}')
my_dict = literal_eval(byte_string.decode('utf-8'))
print(f'Dic: {my_dict}') # 👉️ {'first': 'bobby', 'last': 'hadz'}
print(f'type: {type(my_dict)}') # 👉️ <class 'dict'>
print(my_dict["sr"]["posx"])
#print(y)
# Convert Bytes to Dictionary in Python
'''
from ast import literal_eval
#my_bytes = b"{'first': 'bobby', 'last': 'hadz'}"
my_bytes = b'{"sr":{"posx":0.300,"vel":0.00,"stat":3}}\n'
my_dict = literal_eval(my_bytes.decode('utf-8'))
print(my_dict) # 👉️ {'first': 'bobby', 'last': 'hadz'}
print(type(my_dict)) # 👉️ <class 'dict'>
print(my_dict["sr"]["posx"])
'''
REFERENCE:
Tuesday, N
ov 26,2024
../
../
Python String Methods
Syntax
string.count(value, start, end)
Parameter Values
Parameter Description
value Required. A String. The string to value to search for
start Optional. An Integer. The position to start the search. Default is 0
end Optional. An Integer. The position to end the search. Default is the end of the string
https://www.w3schools.com/python/python_ref_string.asp
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
print(x)
../
# Convert Bytes to Dictionary in Python
https://github.com/bobbyhadz/python-convert-bytes-to-dictionary/blob/main/main.py
from ast import literal_eval
my_bytes = b"{'first': 'bobby', 'last': 'hadz'}"
my_dict = literal_eval(my_bytes.decode('utf-8'))
print(my_dict) # 👉️ {'first': 'bobby', 'last': 'hadz'}
print(type(my_dict)) # 👉️ <class 'dict'>
..//
# Define a byte string
byte_string = b"hello world"
# Convert the byte string to a string using the decode() method
decoded_string = byte_string.decode("utf-8")
# Print the decoded string
print(decoded_string)
Output:
Hello, world!
../
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python bytes() method
https://www.geeksforgeeks.org/python-bytes-method/
Example:
Input: 2
Output: b'\x00\x00'
../
Convert Integer to Bytes
Output:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
../
# python code to demonstrate
# int to bytes
number = 12
result = bytes(number)
print(result)
../
Input: Python
Output: b'Python'
Convert String to Bytes
# python code demonstrating
# int to bytes
str = "Welcome to Geeksforgeeks"
arr = bytes(str, 'utf-8')
print(arr)
utput:
b'Welcome to Geeksforgeeks'
../
timer
nov 25, 2024
string seraching
# File pyJMDic
# Date Nov 25,2024
# Task test nested dic
#print(type(b'{r,"f",0137})
x = b'{r,"f",0137}\n' # class bytes
print(type(x))
thread requested
thread Active
thread done
XJOG-test
myThread: b'{r,"f",0137}\n' ok
b'"":{}:[1,36,]"r":"ox".00sy00"psz"0.0,poa":.00,fed:100,vl:.9"nt:1"or:,dit:,"rm":,"tat"5}\n'
myThread: b'{s{ps:00,"po":0.0,o:00"s00"e"5.0"e"39,ui",co"1"s"0fo1s:}{"qr":31,"qi":1,"qo":0}\n' ok
myThread: b'{"sr":{"posx":0.300,"vel":0.00,"stat":3}}\n' ok
myThread: b'{"qr":32,"qi":0,"qo":1}\n' ok
x = 10
print(type(x))
# nov 16, 2024 sat
# multiProcessing the Rs232 port
# import support lib
import multiprocessing
import serial # seriasl Port
import time
# Qt Support
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow,QMessageBox,QDialog
from PyQt5.uic import loadUi
import sys
# Execute while Initialize
XPosition =0
# Seerial Module
baudrate = 115200
print(baudrate)
print("Portsetup")
#serialPort = serial.Serial(port='COM3', baudrate=baudrate, parity=serial.PARITY_NONE, bytesize=8, timeout=1,
#stopbits=serial.STOPBITS_ONE)
# Qt GUI Interface
class MainUIxy(QMainWindow):
def __init__(self):
print("loading Qt_GUI")
super(MainUIxy, self).__init__()
loadUi("myTestQt5.ui", self) # Load the UI file
self.pushButtonXHome.clicked.connect(XHomeAxis)
self.XJOG_PB.clicked.connect(XJOGAxis)
self.ThreadON_PB.clicked.connect(Thread_ON)
self.ThreadOFF_PB.clicked.connect(Thread_OFF)
def cal_square(number, q):
print("myThreadRS232Function")
#global Gloabal_RS232Read
#global reading
#if (serialPort.is_open):
while (True):
#reading = serialPort.readline()
#length = len(reading)
#time.sleep(0.25)
#if (length > 0):
#print(reading)
#q.put(reading)
# Check if 'inner' key exists inside 'outer'
message = {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
q.put(123)
q.put("hernan")
q.put('test')
q.put(message)
print(message)
q =multiprocessing.Queue()
def Thread_ON():
print("thread ON")
time.sleep(1.25)
#if serialPort.is_open:
print("target setup")
number = [2, 3, 4]
# q =multiprocessing.Queue()
p = multiprocessing.Process(target=cal_square, args=(number, q))
p.start()
p.join()
print("target done")
def Thread_OFF():
print("open port thread OFF")
while q.empty() is False:
print(q.get())
def XJOGAxis(): #F6
print("XJOGAxis")
#time.sleep(1.25)
serialString = ""
# global XPosition
global reading
XPosIncrement = 0.500
# 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(XPosition)
def XHomeAxis():
print("XHomeAxis")
serialString = ""
#serialPort.write(bytearray('{"gc":"G28.2 X0"}''\r\n','ascii'))
#print("test")
#print(Gloabal_RS232Read)
if __name__== "__main__":
# activate Qt_GUI
# if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainUIxy()
ui.show()
app.exec_()
#Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
#https://www.youtube.com/watch?v=sp7EhjLkFY4&t=36s
11/15/24
import multiprocessing
def cal_square(number, q):
message = {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
q.put(123)
q.put("hernan")
q.put('test')
q.put("lynn")
if __name__== "__main__":
number =[2,3,4]
q =multiprocessing.Queue()
p =multiprocessing.Process(target=cal_square, args=(number,q))
p.start()
p.join()
while q.empty() is False:
print(q.get())
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
https://www.youtube.com/watch?v=sp7EhjLkFY4&t=36s
Python dic
message = {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
print(type(message))
print(message['sr']['posx'])
https://www.programiz.com/python-programming/nested-dictionary
https://www.geeksforgeeks.org/python-convert-json-to-string/
https://www.w3schools.com/python/python_ref_string.asp
#Basic Nested Key
#Date 11/13/2024
message = {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
print(message)
print(type(message))
print(message['sr']['posx'])
posx_1 =message['sr']['posx']
print(posx_1)
# Check if 'inner' key exists inside 'outer'
if 'sr' in message and 'posx' in message['sr']:
nested_value = message['sr']['posx']
print('ok exist')
else:
print('none here in sr')
message_2 = {"qr":31,"qi":1,"qo":0}
print(message_2)
print(type(message_2))
posx_2 =message_2['sr']['posx']
print(posx_2)
#REFERENCE
#https://www.geeksforgeeks.org/check-if-a-nested-key-exists-in-a-dictionary-in-python/
11/12/2024 tuesday
..jog
Jog: F6
CNC_XYA_m, x: 0.200, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.200 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.100,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.200,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
Jog: F6
CNC_XYA_m, x: 0.300, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.300 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.300,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
Jog: F5
CNC_XYA_m, x: 0.200, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.200 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.300,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.200,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
Jog: F6
CNC_XYA_m, x: 0.300, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.300 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.200,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.300,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
Jog: F6
CNC_XYA_m, x: 0.400, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.400 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.300,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.400,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
Jog: F6
CNC_XYA_m, x: 0.500, y: 0.000, a: 0.000
OptimizeA
==> {"gc":"G1 F150 X0.500 Y0.000 A0.000"}
<== {"r":{},"f":[1,0,38,139]}
<== {"sr":{"posx":0.400,"vel":4.82,"stat":5}}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":0.500,"vel":0.00,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":0,"qo":1}
-- A_move, already there --
==> {"gc":"G28.3 A0.000"}
<== {"r":{},"f":[1,0,22,102]}
<== {"qr":32,"qi":1,"qo":1}
..//
liteplacer
B: Home X
Homing axis X, timeout value: 25000
==> {"gc":"G28.2 X0"}
<== {"r":{},"f":[1,0,18,77]}
<== {"sr":{"coor":0,"dist":1,"stat":9}}
<== {"qr":32,"qi":1,"qo":1}
<== {"qr":31,"qi":1,"qo":0}
<== {"sr":{"posx":-4.333,"feed":2000.00,"vel":2000.00}}
<== {"sr":{"posx":-10.833}}
<== {"sr":{"posx":-17.333}}
<== {"sr":{"posx":-23.833}}
<== {"sr":{"posx":-30.333}}
<== {"sr":{"posx":-36.833}}
<== {"sr":{"posx":-43.333}}
<== {"sr":{"posx":-49.833}}
<== {"sr":{"posx":-56.333}}
<== {"sr":{"posx":-62.667}}
<== {"sr":{"posx":-69.167}}
<== {"sr":{"posx":-75.667}}
<== {"sr":{"posx":-82.167}}
<== {"sr":{"posx":-88.666}}
<== {"sr":{"posx":-95.166}}
<== {"sr":{"posx":-101.666}}
<== {"sr":{"posx":-108.166}}
<== {"sr":{"posx":-114.666}}
<== {"sr":{"posx":-120.999}}
<== {"sr":{"posx":-127.499}}
<== {"sr":{"posx":-133.999}}
<== {"sr":{"posx":-140.221,"vel":1294.92}}
<== {"sr":{"posx":-140.666,"vel":0.16}}
<== {"sr":{"posx":-608.000,"feed":0.00,"vel":0.00,"coor":1,"dist":0,"stat":3}}
ReadyEvent stat
<== {"qr":32,"qi":1,"qo":1}
Homing X done.
../ end homming
..//jog
posx":-159.167
XJOG-test
b'{"r":{},"f":[1,0,36,137]}\n'
b'{"sr":{"posx":-159.166,"feed":150.00,"vel":3.99,"stat":5}}\n'
b'{"qr":31,"qi":1,"qo":0}\n'
b'{"sr":{"posx":-158.709,"vel":150.00}}\n'
b'{"sr":{"posx":-158.221}}\n'
b'{"sr":{"posx":-157.734}}\n'
b'{"sr":{"posx":-157.247}}\n'
b'{"sr":{"posx":-156.759}}\n'
b'{"sr":{"posx":-156.272}}\n'
b'{"sr":{"posx":-155.784}}\n'
b'{"sr":{"posx":-155.297}}\n'
b'{"sr":{"posx":-154.810}}\n'
b'{"sr":{"posx":-154.322}}\n'
b'{"er":{"fb":440.20,"st":204,"msg":"Limit switch hit - Shutdown occurred"}}\n'
b'{"qr":30,"qi":1,"qo":0}\n'
==4.845
..end jog
..//starting home
C:\Users\admin\PycharmProjects\pyHMUSB_1\.venv\Scripts\python.exe C:\Users\admin\PycharmProjects\pyHMUSB_1\PyQt5HMUSB071724.py
11
115200
thread ON
b'{"r":{},"f":[1,0,18,77]}\n'
b'{"sr":{"posx":0.000,"posy":0.000,"posz":0.000,"posa":0.000,"feed":0.00,"vel":0.00,"unit":1,"coor":0,"dist":1,"frmo":1,"stat":9}}\n'
b'{"qr":32,"qi":1,"qo":1}\n'
b'{"qr":31,"qi":1,"qo":0}\n'
b'{"sr":{"posx":-4.500,"feed":2000.00,"vel":2000.00}}\n'
b'{"sr":{"posx":-11.000}}\n'
b'{"sr":{"posx":-17.333}}\n'
b'{"sr":{"posx":-23.833}}\n'
b'{"sr":{"posx":-30.333}}\n'
b'{"sr":{"posx":-36.833}}\n'
b'{"sr":{"posx":-43.333}}\n'
b'{"sr":{"posx":-49.833}}\n'
b'{"sr":{"posx":-56.333}}\n'
b'{"sr":{"posx":-62.834}}\n'
b'{"sr":{"posx":-69.333}}\n'
b'{"sr":{"posx":-75.667}}\n'
b'{"sr":{"posx":-82.167}}\n'
b'{"sr":{"posx":-88.666}}\n'
b'{"sr":{"posx":-95.166}}\n'
b'{"sr":{"posx":-101.666}}\n'
b'{"sr":{"posx":-108.166}}\n'
b'{"sr":{"posx":-114.666}}\n'
b'{"sr":{"posx":-121.166}}\n'
b'{"sr":{"posx":-127.666}}\n'
b'{"sr":{"posx":-133.999}}\n'
b'{"sr":{"posx":-140.500}}\n'
b'{"sr":{"posx":-147.000}}\n'
b'{"sr":{"posx":-153.500}}\n'
b'{"sr":{"posx":-159.124,"vel":326.16}}\n'
b'{"sr":{"posx":-159.167,"vel":0.16}}\n'
b'{"sr":{"posx":-608.000,"feed":0.00,"vel":0.00,"coor":1,"dist":0,"stat":3}}\n'
b'{"qr":32,"qi":1,"qo":1}\n'
...end home
..//
starting
XJOG-test
b'{"r":{},"f":[1,0,36,137]}\n'
b'{"sr":{"posx":-99.333,"feed":150.00,"vel":3.99,"stat":5}}\n'
b'{"qr":31,"qi":1,"qo":0}\n'
b'{"sr":{"posx":-98.888,"vel":150.00}}\n'
b'{"sr":{"posx":-98.400}}\n'
b'{"sr":{"posx":-97.913}}\n'
b'{"sr":{"posx":-97.425}}\n'
b'{"sr":{"posx":-96.938}}\n'
b'{"sr":{"posx":-96.438}}\n'
b'{"sr":{"posx":-95.951}}\n'
b'{"sr":{"posx":-95.463}}\n'
b'{"sr":{"posx":-94.976}}\n'
b'{"sr":{"posx":-94.489}}\n'
b'{"sr":{"posx":-94.001}}\n'
b'{"er":{"fb":440.20,"st":204,"msg":"Limit switch hit - Shutdown occurred"}}\n'
b'{"qr":30,"qi":1,"qo":0}\n'
..end jog position
../
C:\Users\admin\PycharmProjects\pyHMUSB_1\.venv\Scripts\python.exe C:\Users\admin\PycharmProjects\pyHMUSB_1\PyQt5HMUSB071724.py
11
115200
thread ON
..starting home position
b'{"r":{},"f":[1,0,18,77]}\n'
b'{"sr":{"posx":0.000,"posy":0.000,"posz":0.000,"posa":0.000,"feed":0.00,"vel":0.00,"unit":1,"coor":0,"dist":1,"frmo":1,"stat":9}}\n'
b'{"qr":32,"qi":1,"qo":1}\n'
b'{"qr":31,"qi":1,"qo":0}\n'
b'{"sr":{"posx":-4.333,"feed":2000.00,"vel":2000.00}}\n'
b'{"sr":{"posx":-10.833}}\n'
b'{"sr":{"posx":-17.333}}\n'
b'{"sr":{"posx":-23.833}}\n'
b'{"sr":{"posx":-30.333}}\n'
b'{"sr":{"posx":-36.833}}\n'
b'{"sr":{"posx":-43.333}}\n'
b'{"sr":{"posx":-49.833}}\n'
b'{"sr":{"posx":-56.333}}\n'
b'{"sr":{"posx":-62.667}}\n'
b'{"sr":{"posx":-69.167}}\n'
b'{"sr":{"posx":-75.667}}\n'
b'{"sr":{"posx":-82.167}}\n'
b'{"sr":{"posx":-88.666}}\n'
b'{"sr":{"posx":-95.166}}\n'
b'{"sr":{"posx":-99.333,"vel":0.16}}\n'
b'{"sr":{"posx":-608.000,"feed":0.00,"vel":0.00,"coor":1,"dist":0,"stat":3}}\n'
b'{"qr":32,"qi":1,"qo":1}\n'
..end home position
..//