sorry my mistake it seems you need to send “:FE050000FF00FE\r\n” to turn the relay 1 on
and for \r\n you need to send the real characters, so chr(13)+chr(10)
sum up: “:FE050000FF00FE”+chr(13)+chr(10)
I tried with:
SerialConnection1.Device = SerialDevice.At(0)
SerialConnection1.ConnectSerialConnection1.Write(“:FE050000FF00FE”)+chr(13)+chr(10)
but still no luck
it is connecting but not responding to the input
SerialConnection1.Write ":FE050000FF00FE”+chr(13)+chr(10)
got it, but still not responding
SerialConnection1.Write “:FE050000FF00FE”+chr(13)+chr(10)
again, you should try it first with a serial terminal app like coolterm, putty or gtkterm, all are free and all platforms
please try
SerialConnection1.Write DecodeHex(“FE050000FF00FE”)+chr(13)+chr(10)
or also the same
SerialConnection1.Write DecodeHex(“FE050000FF00FE0D0A”)
some more obscure documentation for this device …
http://wiki.sainsmart.com/index.php/101-70-208
but as interesting all the codes to send to the device are showed, on the first page of the pdf
so I would try to send :
SerialConnection1.Write DecodeHex(“3A46453035303030304646303046450D0A”)
It is working
instructions will follow so otherscan get it working too
How to get the Sainsmart 16 Channel USB relay board working with Xojo
No driver installation needed
1) Wire up the relay board
Photo:
a) remove the jumper at location 4
b) it must receive 9-36v DC power input at location 3 (watch for polarity)
c) hook up USB cable at location 5
To test everything is working:
Drag a serialconnection into the window (I called mine SerialConnection1)
Change it’s settings to:
Baud: 9600
Bits: 8 data bits
Parity: None
Stop Bit: One
XON: off
CTS: off
DTR: off
In same window create a button and paste this code in the mouse down event:
Try
SerialConnection1.Device = SerialDevice.At(1)
SerialConnection1.Connect
MessageBox(“The serial connection is open.”)
Catch error As IOException
MessageBox(“The serial connection could not be opened.”)
End Try
This will establish the connection to the board
Communicating with the board:
Create a button and paste one of these in the mouse down event:
Open relay 1:
SerialConnection1.Write DecodeHex(“:3A46453035303030304646303046450D0A”)
Close relay 1:
SerialConnection1.Write DecodeHex(“:3A46453035303030303030303046440D0A”)
Here are the full command list:
Good luck
I suspect that should be:
SerialConnection1.Write “:” + DecodeHex(“3A46453035303030304646303046450D0A”)
ditto for the other call.
me too the “:” is unknown to the decodehex function
You are right, the : does absolutely nothing, but funny enough it works both with or without the :
Yet again thank you so much for the help
next step is to make a global method (in a module) that takes the relay number and the state on-off as a parameter, and that does all the work.
I have been trying to create a global method in a module that could be called, but can’t get it working.
I have a module called: RelayControl
and a method called Relay1On
Could you be so kind and point me in the right direction how a method that I could call to turn on relay1 could look?
The code for turning on relay 1 is:
SerialConnection1.Write DecodeHex(“3A46453035303030304646303046450D0A”)
You should pass the serial object on the Window into the Relay1On Method. Assuming you name the parameter SerialConnection1, then the code that you showed would be the only code in the method and should work.
something like this placed in a module should do the job
Public Sub RelayControl(relayNumber as Integer, state as Boolean)
Dim s As New SerialConnection
If s=Nil Then Return
s.Baud = Serial.Baud9600
s.Bits = Serial.Bits8
s.Parity = SerialConnection.Parities.None
s.StopBit = SerialConnection.StopBits.One
s.XON = False
s.CTS = False
s.DTR = False
Try
s.Device = SerialDevice.At(1)
s.Connect
'MessageBox(“The serial connection Is open.”)
Catch error As IOException
MessageBox("The serial connection could Not be opened.")
Return
End Try
Dim outString As String = "3A46453035303030"
Dim nextString As String
Select Case relayNumber
Case 1
If state Then
nextString = "30303030304644"
Else
nextString = "30303030304644"
End If
Case 2
If state Then
nextString = "31464630304644"
Else
nextString = "31303030304643"
End If
Case 3
If state Then
nextString = "32464630304643"
Else
nextString = "32303030304642"
End If
Case 4
If state Then
nextString = "33464630304642"
Else
nextString = "33303030304641"
End If
Case 5
If state Then
nextString = "34464630304641"
Else
nextString = "34303030304639"
End If
Case 6
If state Then
nextString = "35464630304639"
Else
nextString = "35303030304638"
End If
Case 7
If state Then
nextString = "36464630304638"
Else
nextString = "36303030304637"
End If
Case 8
If state Then
nextString = "37464630304637"
Else
nextString = "37303030304636"
End If
Case 9
If state Then
nextString = "38464630304636"
Else
nextString = "38303030304635"
End If
Case 10
If state Then
nextString = "39464630304635"
Else
nextString = "39303030304634"
End If
Case 11
If state Then
nextString = "41464630304634"
Else
nextString = "41303030304633"
End If
Case 12
If state Then
nextString = "42464630304633"
Else
nextString = "42303030304632"
End If
Case 13
If state Then
nextString = "43464630304632"
Else
nextString = "43303030304631"
End If
Case 14
If state Then
nextString = "44464630304631"
Else
nextString = "44303030304630"
End If
Case 15
If state Then
nextString = "45464630304630"
Else
nextString = "45303030304646"
End If
Case 16
If state Then
nextString = "46464630304646"
Else
nextString = "46303030304645"
End If
Case 17 // set all relays at once
If state Then
nextString = "30303031303032464646464533"
Else
nextString = "30303031303032303030304531"
End If
End Select
outString = outString + nextString + "0D0A"
s.Write DecodeHex( outString)
s.Close
End Sub
you can add at the end of the select…case :
Case 17 // set all relays at once
If state Then
nextString = "30303031303032464646464533"
Else
nextString = "30303031303032303030304531"
End If
Else
MessageBox("Unknown relay number: " +Str( relayNumber))
End Select
I have implemented the code (with minor changes), and the connection is established but the relay does not respond nomatter what I try. (Also tried with your original code suggestion)
Can you help figure out what is missing or what I’m not seeing?
Code in the Method RelayControl (inside the module: Relay)
Parameters: relayNumber as Integer, state as Boolean
Scope: Global
Dim s As New SerialConnection
If s = Nil Then Returns.Baud = Serial.Baud9600
s.Bits = Serial.Bits8
s.Parity = SerialConnection.Parities.None
s.StopBit = SerialConnection.StopBits.One
s.XON = False
s.CTS = False
s.DTR = FalseTry
s.Device = SerialDevice.At(1)
s.Connect
'MessageBox(“The serial connection is open.”)
Catch error As IOException
MessageBox(“The serial connection could Not be opened.”)
Return
End TryDim outString As String = “3A46453035303030”
Dim nextString As String
Dim endString As String = “0D0A”Select Case relayNumber
Case 1
If state Then
nextString = “30464630304645”
Else
nextString = “30303030304644”
End If
Case 2
If state Then
nextString = “31464630304644”
Else
nextString = “31303030304643”
End If
Case 3
If state Then
nextString = “32464630304643”
Else
nextString = “32303030304642”
End If
Case 4
If state Then
nextString = “33464630304642”
Else
nextString = “33303030304641”
End If
Case 5
If state Then
nextString = “34464630304641”
Else
nextString = “34303030304639”
End If
Case 6
If state Then
nextString = “35464630304639”
Else
nextString = “35303030304638”
End If
Case 7
If state Then
nextString = “36464630304638”
Else
nextString = “36303030304637”
End If
Case 8
If state Then
nextString = “37464630304637”
Else
nextString = “37303030304636”
End If
Case 9
If state Then
nextString = “38464630304636”
Else
nextString = “38303030304635”
End If
Case 10
If state Then
nextString = “39464630304635”
Else
nextString = “39303030304634”
End If
Case 11
If state Then
nextString = “41464630304634”
Else
nextString = “41303030304633”
End If
Case 12
If state Then
nextString = “42464630304633”
Else
nextString = “42303030304632”
End If
Case 13
If state Then
nextString = “43464630304632”
Else
nextString = “43303030304631”
End If
Case 14
If state Then
nextString = “44464630304631”
Else
nextString = “44303030304630”
End If
Case 15
If state Then
nextString = “45464630304630”
Else
nextString = “45303030304646”
End If
Case 16
If state Then
nextString = “46464630304646”
Else
nextString = “46303030304645”
End If
Case 17 // set all relays at once
If state Then
nextString = “3030303031303032464646464533”
Else
nextString = “3030303031303032303030304531”
End If
Else
MessageBox("Unknown relay number: " +Str( relayNumber))
End Selectdim finalString As String
finalString = outString + nextString + endString
s.Write DecodeHex(finalString)
s.XmitWait
s.Close
I call it from a simple push button with the mousedown event:
RelayControl(1, true)
Got it working
but it seems to be rather inconsistent in the way it operates.
For example calling the same relay to turn off several times in a row can actually cause it to turn on.
Or calling realy1 on, then after few seconds off sometimes are inresponsive. Then trying again turns it off.
I think your code works flawless but the relay is simply not of good enough quality?
Didn’t you need to prefix the string with “:”?