I'm pretty new with the Raspberry enviroment, but I managed to write a code in Python 2, watching a lot of videos and post on this website. What I'm trying to do is a simple GUI with an ON/OFF switch for turning ON/OFF a projector.
I managed to create a window with a button that says on/off and an exit button as well. For turning on or off the projector, I use another codes, I'll put them below. I'm using a USB to Serial HUB RS232 converter,it has 4 RS232 Ports.
The entire thing works like this so far, When I start it and press the button, it sends the codes for turn the projector off, but it only works once, when I press it again it doesn't do anything, like it doesn't load the code for on/off anymore.
Ultimately, I want to do this for 4 projectors, so my thinking is to replicate the codes for on/off and changing the port name:'\dev\ttyUSB0 for USB1, USB2 and so on.
Any idea on how to help me solve this is gratefully accepted!
The commands for turning on/off the projector are these:
Baud rate: 9600 Data Length: 8 bits Parity bit: None Stop bit: 1 bit Communication mode Full duplex Turning On the projector: 02h 00h 00h 00h 00h 02h Turning Off the projector: 02h 01h 00h 00h 00h 03h
This is my code so far
from Tkinter import * import tkFont import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(40, GPIO.OUT) GPIO.output(40, GPIO.LOW) win = Tk() myFont = tkFont.Font(family = 'Helvetica', size = 36, weight = 'bold') def ledON(): print("LED button pressed") if GPIO.input(40) : ledButton["text"] = "ON" import SerialOFF else : ledButton["text"] = "OFF" import SerialON def exitProgram(): print("Exit") GPIO.cleanup() win.quit() win.title("NEC Control") win.geometry('800x480') exitButton = Button(win, text = "Exit", font = myFont, command = exitProgram, height =2 , width = 6) exitButton.pack(side = BOTTOM) ledButton = Button(win, text = "Iniciate", font = myFont, command = ledON, height = 2, width =8 ) ledButton.pack() mainloop()
And these are the codes for switching on/off the projector, for the Off part is the same, I just changed the command line and the ON parts
ON import time import serial ser = serial.Serial( port='/dev/ttyUSB0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS ) ser.close() ser.open() ser.isOpen() ON=bytearray([0x02,0x00,0x00,0x00,0x00,0x02]) ser.write(ON) print(ser.name) ser.close()