Sending commands to COM port in VBA for Tower Light

192 Views Asked by At

I have been attempting to control a USB Tower Light that I bought from Adafruit. (part # 5125) see https://www.adafruit.com/product/5125?gclid=EAIaIQobChMIwOrro5Dm_QIVNcmUCR3gCALAEAQYAiABEgKy9_D_BwE

I have the light installed on my windows Computer using the USB port (COM5). I have tried using two comm modules that I have to send commands to the light. Both are giving the same results. I am making connections with the port and sending data to the light. I could post the code of my comm modules here, but it is quite lengthy.

I am programming in the latest version of MS Access using VBA.

The light is responding, but not correctly. I have tried using the HEX function in VBA. I have tried to use constants. I have tried to send the codes directly in quotes.

I can get it to blink and change colors, but just not correctly. It just seems like some of my codes are getting jumble or concatenated some how.

Could you possibly make a suggestion as to what I am doing wrong or point me in the right direction?

Thank you in advance.

Gratefully,

Ira Saunders

Below is the code they give you in Python.

Example for Adafruit USB tower light w/alarm don't forge to pip install pyserial or pip3 install pyserial """


import serial
import time

 serialPort = 'COM57'  # 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()

Below is my code:

Private Sub Command0_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strError  As String
Dim strData   As String


Dim RED_ON As String
RED_ON = Hex(11)  '"0x11"
Dim RED_OFF As String
RED_OFF = Hex(21) '"0x21"
Const RED_BLINK = "0x41"

Const YELLOW_ON = "0x12"
Dim YELLOW_OFF As String
YELLOW_OFF = Hex(22) '0x22
'YELLOW_BLINK = 0x42

'GREEN_ON = 0x14
Dim GREEN_OFF As String
GREEN_OFF = Hex(24) '0x24
Dim GREEN_BLINK As String
GREEN_BLINK = Hex(44)

'BUZZER_ON = 0x18
'BUZZER_OFF = 0x28
'BUZZER_BLINK = 0x48
' Initialize Communications
lngStatus = CommOpen(5, "COM" & CStr(5), "baud=9600 parity=N data=8 stop=1")
If lngStatus <> 0 Then
' Handle error.
    lngStatus = CommGetError(strError)
    MsgBox "COM Error: " & strError
End If


' Set modem control lines.
lngStatus = CommSetLine(5, LINE_RTS, True)
lngStatus = CommSetLine(5, LINE_DTR, True)

' Write data to serial port.
'MsgBox CommWrite(5, Hex(12))  'Blinking Orange
'MsgBox CommWrite(5, Hex(42))  'blinking red
'MsgBox CommWrite(5, Hex(22))  'does nothing
'MsgBox CommWrite(5, Hex(44))  'blinking yellow
'MsgBox CommWrite(5, Hex(10))  'blinking red
'MsgBox CommWrite(5, Hex(18))   'nothing
'MsgBox CommWrite(5, Hex(21))   'blinking green
MsgBox CommWrite(5, Hex(24))    'does nothing

CommClose (5)
End Sub
1

There are 1 best solutions below

0
Gustav On

Try declaring those constants as numeric values:

Const GREEN_ON     As Long = &H14
Const GREEN_OFF    As Long = &H24
Const GREEN_BLINK  As Long = &H44