1

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()
1

It's irritating when you get 99% of the way there. I've done this myself (not in python - in LabVIEW!) I trust your projector doesn't have a network port? This would make your life a lot easier if you're controlling multiple projectors.

You have your commands for on and off correct, BUT you're not listening for the response from the projector. I don't think you can issue a subsequent command without first receiving the projector's response.

success: 22h 00h (ID1) (ID2) 00h (CKS)
failure: A2h 00h (ID1) (ID2) 02h (ERR1) (ERR2) (CKS)

Enjoy!!

0

Python will not actually import a module twice. If you really want to import it again, you should use reload. Altough I would rather make a on_off function, with the command as parameter.

def on_off(cmd):
    ser = serial.Serial(
     port='/dev/ttyUSB0',
     baudrate=9600,
     parity=serial.PARITY_NONE,
     stopbits=serial.STOPBITS_ONE,
     bytesize=serial.EIGHTBITS
    )

    ser.close()
    ser.open()

    cmd=bytearray(cmd)

    ser.write(cmd)
    print(ser.name)
    ser.close()

Then you call them as so:

on_of([0x02,0x00,0x00,0x00,0x00,0x02])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.