riferimento: comandare robot da python
1. serve la libreria PySerial
- Downloading and Installing the PySerial Library
che si usa semplicemente così, per inviare il carattere 'a' al PSOC:
import serial
"""
usare una volta sola per aprire com4
PSOCSerialPort = serial.Serial("COM4", 115200)
"""
PSOCSerialPort.write('a')
"""
usare una volta sola per aprire com4
PSOCSerialPort = serial.Serial("COM4", 115200)
"""
PSOCSerialPort.write('a')
2. serve la libreria Tkinter
per fare l'interfaccia grafica.
import serial
from Tkinter import *
#preparare le definizioni dei comandi
def ledOn():
PSOCSerialPort.write('a')
def ledOff():
PSOCSerialPort.write('A')
#comando da emettere una volta per non avere errore:
#PSOCSerialPort = serial.Serial("COM4", 115200)
appWindow = Tk() # creates the application window (you can use any name)
appWindow.wm_title("LED Control") # displays title at the top left
# insert app widgets here (radiobuttons, checkboxes, buttons, etc.)
var = IntVar() # define var as an integer variable
onButton = Radiobutton(appWindow, text="LED ON", variable = var, value = 1, command=ledOn)
onButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window
offButton = Radiobutton(appWindow, text="LED OFF", variable = var, value = 2, command=ledOff)
offButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window
appWindow.mainloop() # main loop. From here, the GUI starts updating and responding to user inputs.
verrà creata questa GUI:

from Tkinter import *
#preparare le definizioni dei comandi
def ledOn():
PSOCSerialPort.write('a')
def ledOff():
PSOCSerialPort.write('A')
#comando da emettere una volta per non avere errore:
#PSOCSerialPort = serial.Serial("COM4", 115200)
appWindow = Tk() # creates the application window (you can use any name)
appWindow.wm_title("LED Control") # displays title at the top left
# insert app widgets here (radiobuttons, checkboxes, buttons, etc.)
var = IntVar() # define var as an integer variable
onButton = Radiobutton(appWindow, text="LED ON", variable = var, value = 1, command=ledOn)
onButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window
offButton = Radiobutton(appWindow, text="LED OFF", variable = var, value = 2, command=ledOff)
offButton.pack( anchor = W ) # this part adjusts the radiobuttons to the parent window
appWindow.mainloop() # main loop. From here, the GUI starts updating and responding to user inputs.
verrà creata questa GUI:
che manderà 'a' oppure 'A' a seconda della scelta
Nessun commento:
Posta un commento