Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threads_serailport_mgr
- import time
- RED_ON = 0x11
- RED_OFF = 0x21
- RED_BLINK = 0x41
- YELLOW_ON= 0x12
- YELLOW_OFF = 0x22
- YELLOW_BLINK = 0x42
- GREEN_ON = 0x14
- GREEN_OFF = 0x24
- GREEN_BLINK = 0x44
- BUZZER_ON = 0x18
- BUZZER_OFF = 0x28
- BUZZER_BLINK = 0x48
- # Example usage
- if __name__ == "__main__":
- # Define the settings for COM5 exactly as you specified.
- port_settings = {
- 'COM5': {
- 'baudrate': 9600,
- 'bytesize': 8, # 8 bits per byte.
- 'parity': 'N', # No parity.
- 'stopbits': 1, # 1 stop bit.
- 'timeout': None, # Blocking mode (no timeout).
- 'xonxoff': False, # No software flow control.
- 'rtscts': False, # No hardware (RTS/CTS) flow control.
- 'dsrdtr': False # No hardware (DSR/DTR) flow control.
- }
- }
- # Create an instance of the serial port manager with the defined settings.
- mgr = threads_serailport_mgr.serial_port_mgr(port_settings)
- mgr.start() # Start the thread that opens the ports and monitors for incoming data.
- # Allow a short interval for the connection to be established.
- time.sleep(1)
- # Prepare your bytes data. (Ensure it's a bytes object)
- data_to_send = b'RED_ON, COM5!'
- # Send data in bytes to COM5.
- mgr.send_data('COM5', data_to_send)
- # Keep the script running for a little while to allow data reception or further actions.
- time.sleep(2)
- # When finished, stop the manager which disconnects all ports and stops the thread.
- mgr.stop()
- ''' This code works but when implemented as is the amps looses connections and won't respond
- https://www.adafruit.com/product/5125
- import serial
- import time
- serialPort = 'COM5' # Change to the serial/COM port of the tower light
- #serialPort = '/dev/USBserial0' # on mac/linux, it will be a /dev path
- baudRate = 9600
- RED_ON = 0x11
- RED_OFF = 0x21
- RED_BLINK = 0x41
- YELLOW_ON= 0x12
- YELLOW_OFF = 0x22
- YELLOW_BLINK = 0x42
- GREEN_ON = 0x14
- GREEN_OFF = 0x24
- GREEN_BLINK = 0x44
- BUZZER_ON = 0x18
- BUZZER_OFF = 0x28
- BUZZER_BLINK = 0x48
- def sendCommand(serialport, cmd):
- serialport.write(bytes([cmd]))
- if __name__ == '__main__':
- mSerial = serial.Serial(serialPort, baudRate)
- # Clean up any old state
- sendCommand(mSerial, BUZZER_OFF)
- sendCommand(mSerial, RED_OFF)
- sendCommand(mSerial, YELLOW_OFF)
- sendCommand(mSerial, GREEN_OFF)
- # turn on each LED set in order
- sendCommand(mSerial, RED_ON)
- time.sleep(0.5)
- sendCommand(mSerial, RED_OFF)
- sendCommand(mSerial, YELLOW_ON)
- time.sleep(0.5)
- sendCommand(mSerial, YELLOW_OFF)
- sendCommand(mSerial, GREEN_ON)
- time.sleep(0.5)
- sendCommand(mSerial, GREEN_OFF)
- # beep!
- sendCommand(mSerial, BUZZER_ON)
- time.sleep(0.1)
- sendCommand(mSerial, BUZZER_OFF)
- # Use the built-in blink modes
- sendCommand(mSerial, RED_BLINK)
- time.sleep(3)
- sendCommand(mSerial, RED_OFF)
- sendCommand(mSerial, YELLOW_BLINK)
- time.sleep(3)
- sendCommand(mSerial, YELLOW_OFF)
- sendCommand(mSerial, GREEN_BLINK)
- time.sleep(3)
- sendCommand(mSerial, GREEN_OFF)
- # Please be kind, re-wind!
- sendCommand(mSerial, BUZZER_OFF)
- sendCommand(mSerial, RED_OFF)
- sendCommand(mSerial, YELLOW_OFF)
- sendCommand(mSerial, GREEN_OFF)
- mSerial.close()
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement