Aaron,
I decided to start with a RS232 implementation that is two way. That way I believe we can get a how-to guide that will help more users.
Below is the driver code for a digital IO board I had connected to my computer. It was controlled by HomeSeer. It exposes three commands:
- Init: open the port
- projectorLiftDown: Set one of the digital outputs HIGH
- projectorLiftUp: Set one of the digital outputs LOW
If there is uncertainty concerning the spec, let me know and I will make it clearer.
[code]const COMPORT=5 'Serial COM port number on computer
const HEADER=“A” 'Address of Weeder board. Set by DIP switches on the board.
'**********************************************************************
'** Init functions **
'**********************************************************************
sub startup()
'Open COM port
e=hs.OpenComPortex(COMPORT,“9600,n,8,1”,0,“”,“”, chr(3))
if e<> “” then
hs.WriteLog “Error”, “DigitalIO: Failed to open port “&COMPORT&”. Message=” & e
else
hs.WriteLog “DigitalIO”, “Open port “&COMPORT
str=hs.GetComPortData(COMPORT) 'Clean the Rs232 buffer
if str<>”” then hs.writeLog “Debug”, “DigitalIO: data in RS232 bugger that was not handled=”&str
end if
end sub
sub shutdown()
hs.CloseComPort(COMPORT)
end sub
'**********************************************************************
'** Support functions **
'**********************************************************************
function projectorLiftDown()
'Clean up RS232 receive buffer
str=hs.GetComPortData(COMPORT) 'Clean the Rs232 buffer
if str<>“” then hs.writeLog “Warning”, “DigitalIO: data in RS232 bugger that was not handled=”&str
'Send command
send("HA")
retval=getReply(50) 'it typically takes the digitalIO board (weeder) 20ms to reply. Here we wait 50 just in case
if retval<>"HA" then
hs.WriteLog "ERROR", "DigitalIO return value '"&retval&"' is invalid"
exit function
end if
end function
function projectorLiftUp()
'Clean up RS232 receive buffer
str=hs.GetComPortData(COMPORT) 'Clean the Rs232 buffer
if str<>“” then hs.writeLog “Warning”, “DigitalIO: data in RS232 bugger that was not handled=”&str
'Send command
send("LA")
retval=getReply(50) 'it typically takes the digitalIO board (weeder) 20ms to reply. Here we wait 50 just in case
if retval<>"LA" then
hs.WriteLog "ERROR", "DigitalIO return value '"&retval&"' is invalid"
exit function
end if
end function
function getReply(delayMS)
hsp.WaitMS delayMS
str=hs.GetComPortData(COMPORT)
if len(str)=0 then
hs.WriteLog “ERROR”, “DigitalIO (weeder) RS232 received empty string”
getReply= “null”
exit function
end if
if not left(str,1) = HEADER then
hs.WriteLog “ERROR”, “DigitalIO (weeder) RS232 start character should be " & HEADER & " The value '”& Asc(left(str,1)) &“’ was received”
getReply= “null”
exit function
end if
if not right(str,1) = chr(13) then
hs.WriteLog “ERROR”, “DigitalIO (weeder) RS232 end character should be ‘13’ The value '”& Asc(right(str,1)) &“’ was received”
getReply= “null”
exit function
end if
str = mid(str,2,len(str)-2)
hs.WriteLog “DigitalIO”, “Received from RS232:” & str
getReply= str
end function
Sub send(code)
hs.SendToComPort(COMPORT), HEADER & code & chr(13)
hs.WriteLog “DigitalIO”, “Port “&COMPORT&”:” & code
End Sub
'DigitalIO (weeder) driver (RS232)
'--------------------------------------------
’
’
'\\\\\\\\\\\
'\ Specifications
'\\\\\\\\\\\
'http://www.weedtech.com/wtdio-m.pdf
’
’
'\\\\\\\\\\\
'\ Format: Send command
'\\\\\\\\\\\
'HEADER + command & chr(13)
'- command = string of 1-6 bytes
'- chr(13) - the command is terminated with a carriage return
'- example: “A” & “HC” & chr(13) → set channel “C” to high “H” for weeder device with address “A”
’
’
'\\\\\\\\\\\
'\ Format: Response
'\\\\\\\\\\\
'HEADER + command & chr(13)
'- If “ECHO” is on (default), commands send to the weeder board is echoed back to the PC
’
'\\\\\\\\\\\
'\ Most used commands & reply formats
'\\\\\\\\\\\
’ “HA” → reply “HA” force port A high (projectorLift down)
’ “LA” → reply “LA” force port A low (projectorLift up)
[/code]