Sainsmart 16 Channel USB relay

Continuing the discussion from 8 Channel USB Relay with MacHIDMBS:

Hi everybody :slight_smile:

I have been roaming the forums and google to try and find a way to get the 16 channel USB relay from Sainsmart working with Xojo. With no success unfortunately.
Link to the relay: 16-Channel 9-36V USB Relay Module –

I’m getting so frustrated of my constant failures that I hope you can help me

Until now I have read/tried to my best abilities these guides:

  1. 8 Channel USB Relay with MacHIDMBS - Add-Ons - Xojo Programming Forum

  2. no documentation to disrupt your voyage of discovery. Avoid 3rd party software. (

But I’m still failing miserably :grimacing:

Thanks in advance for your much apriciated help :grin:

it would be really so much easier if you had an ethernet relay board …
like this one for example

1 Like

Hi Jean :slight_smile:

Due to my frustration I’m willing do buy them instead, but can you tell me how they are controlled from Xojo?

Maybe an example?

So I don’t end up buying more stuff I can’t use :sweat_smile:

you will assign an ip address to your board, then use a xojo native tcpsocket to talk to the board.
I don’t have an example ready, but it should be no big deal.
ethernet comms, serial comms have always been easy, and are available on every system for decades
with ethernet your board will be accessible from the other side of the world with almost no change.

usb with serial datas ( often have a fdti chip, or ch340) are talking like the serial.
real usb hid are a pain to talk to, you need a plugin (mbs here) and it will certainly break at the next major system update !

yeah I can’t even get the serialconnection open.
The documentation for it is very bad…
What I found online until now is:
Baud: 9600
Bits: 8 bit
Parity: None
Stop bit: one

But not working…
Need to point out though that it is my first time trying USB/Serial so I may be searching in the wrong alley :see_no_evil:

Phidgets with MBS plugins is a great way to do hardware with Xojo. An extensive range of high-quality, well-documented products, easy to control with MBS.

did you try first with some serial terminal app ? before trying to connect it to xojo …

I tried lsusb to make sure it could see it…
But following your advice I will buy some other relays

I would try to connect more deeply to the device
with putty or gtkterm

you could try to connect first with some serial apps like above post.
if it is serial over usb then it should connect with it
and you don’t need any plugin to connect to it with xojo, but a serial class will do.

if you get to ethernet board relays, then a tcpsocket class will be to use.


there is also a code snippet in the comments on the shop page you posted. Would only have to be translated to Xojo. It is also specified which driver should be used.

The HEX table from Sainsmart needs to be converted to ASCII chars. See sample Python code:
========== python code below =========
import serial
from time import sleep

port = 'COM13' # change this to YOUR usb port
ser = serial.Serial(port, 9600, timeout=5)

delay = 1
tap_delay = 2
ready_delay = 3
off_delay = .5

Sainsmart 16-Channel 9-36v USB Relay Module (CH341 chip)(sku# 101-70-208)
1. Requires CH341 Windows driver installed (see
2. The hex table provided by Sainsmart requires converting to ASCII chars (see 'usbrelay' below)
3. Format of 'usbrelay' two-dimensional array is:
 usbrelay = [row][ch-off, ch-on] so that the 2nd index selects ON/OFF value 
 while the 1st index selects the array row. 
 status = usbrelay[0][1] # row-0 (status)
 stat_ret = usbrelay[0][0] # row-0 (status return)
 ch_1_on = usbrelay[1][1] # row-1 (chan-1 off)
 ch_1_off = usbrelay[1][0] # row-1 (chan-1 off)
 ch_16_on = usbrelay[16][1] # row-16 (chan-16 on)
 ch_16_off = usbrelay[16][0] # row-16 (chan-16 off)
 all_on = usbrelay[17][1] # row-17 (all on)
 all_off = usbrelay[17][0] # row-17 (all off)

usbrelay = [[b':FE0100200000FF\r\n', b':FE0100000010F1\r\n'], # status & status return
 [b':FE0500000000FD\r\n', b':FE050000FF00FE\r\n'], # channel-1
 [b':FE0500010000FC\r\n', b':FE050001FF00FD\r\n'], # channel-2
 [b':FE0500020000FB\r\n', b':FE050002FF00FC\r\n'], # channel-3
 [b':FE0500030000FA\r\n', b':FE050003FF00FB\r\n'], # channel-4
 [b':FE0500040000F9\r\n', b':FE050004FF00FA\r\n'], # channel-5
 [b':FE0500050000F8\r\n', b':FE050005FF00F9\r\n'], # channel-6
 [b':FE0500060000F7\r\n', b':FE050006FF00F8\r\n'], # channel-7
 [b':FE0500070000F6\r\n', b':FE050007FF00F7\r\n'], # channel-8
 [b':FE0500080000F5\r\n', b':FE050008FF00F6\r\n'], # channel-9
 [b':FE0500090000F4\r\n', b':FE050009FF00F5\r\n'], # channel-10
 [b':FE05000A0000F3\r\n', b':FE05000AFF00F4\r\n'], # channel-11
 [b':FE05000B0000F2\r\n', b':FE05000BFF00F3\r\n'], # channel-12
 [b':FE05000C0000F1\r\n', b':FE05000CFF00F2\r\n'], # channel-13
 [b':FE05000D0000F0\r\n', b':FE05000DFF00F1\r\n'], # channel-14
 [b':FE05000E0000FF\r\n', b':FE05000EFF00F0\r\n'], # channel-15
 [b':FE05000F0000FE\r\n', b':FE05000FFF00FF\r\n'], # channel-16
 [b':FE0F00000010020000E1\r\n', b':FE0F0000001002FFFFE3\r\n']] # all channels

# # uncomment below for quick sequencing test
# for row in range(len(usbrelay)): # sequence through each row
# ser.write(usbrelay[row][1]) # turn ON
# sleep(delay/2)
# ser.write(usbrelay[row][0]) # turn OFF
# sleep(delay/2)

# below is example of turning single usbrelay channel on/off
def ch_9_on_off():
def ch_10_on_off():
def ch_11_on_off():
def ch_12_on_off():

# # below is example of using this usbrelay bd to control a 4-port usb switch.
# (usb_port_switching uses PAIR of usbrelay channels)
def usb_1_ON():
def usb_1_OFF():
def usb_2_ON():
def usb_2_OFF():
def usb_3_ON():
def usb_3_OFF():
def usb_4_ON():
def usb_4_OFF():

def main():


if __name__ == "__main__":

as it seems a ch341 usb to serial link, it should sure be working with xojo, using the serial class
no need for any usb plugin.

Thanks Norbert
I will give it a try :slight_smile:
Are there by chance anyone who can translate it to Xojo?

I can’t comment on this product, but I will give you a few tips for buying such electronics:

  1. Does the vendor provide good documentation?
  2. Does the vendor provide up to date working examples in a high level language?
  3. Is the product widely used?

This product does not even meet one of those criteria, so I would not even waste time on.

If all you need to do is connect the relay to a computer, then I would not use an ethernet delay, it’s just adding an extra layer of complexity. It should be something that you can connect to a port and use a terminal app to send AT type commands to it and receive human readable replays.

If I was to go looking for something like this, then this would be an example of the kind of product I’d pick out for further investigation on a Google search:

I’m not suggesting for a minute you should buy this one, it’s just the time of product I’d look further at from my initial Google search.

I will look into Numato, but I’m still keen on getting this one to work as Sainsmart is used by many and others got it working with Xojo (but unfortunately forgot to reveal how).
I just hope there is a bright mind that can point me in the right direction :slight_smile:

seeing the python program, it seems to me quite simple
you have to send the ascii string “FE0500000000FD” to turn the relay 1 off
send the “FE0500000000FE” to turn the relay 1 on
etc … for the other relays

it should be easy to try that first on a serial terminal app
then do it with xojo using a serial class.

I manage to get the port open. For the pros probably no problem, but for a first timer like me apparently no so easy :sweat_smile:

To find it I trew a popupmenu in the window and added this code to the open event:

For i As Integer = 0 To SerialDevice.LastIndex

it gave the output: /dev/ttyUSB0

Then I added a serialconnection to the window with the following settings:
Baud: 9600
Bit: 8 data bits
parity: none
stop bit: one
XON off
CTS off
DTR off

Added a button with this code in mousedown event:

SerialConnection1.Device = SerialDevice.At(0)
MessageBox(“The serial connection is open.”)
Catch error As IOException
MessageBox(“The serial connection could not be opened.”)
End Try

I will now try to send it commands
Thanks for all your great help
I hope that others searching the forum can also use it

The discussion you referred to on the Forum is for a different and dated product and at least had documentation available were as the one you actually bought has no documentation. Manufacturers regularly change the components in their products due to availability or cost and these components sometimes behave slightly differently. This is why as a software engineer it is important that you have access to this documentation so you can check these details. Sometimes you discover you need to slightly change the code because of a hardware change and so on.

The article you linked from Amazon is a hack and a bad one that only pushes the problem down the road at best.

Of course everyone has to decide how much time they want to put into something, but if you have no working high level language example for a particular electronics component and no documentation then you can expect a lot of frustration, little help and a lot of time wasted as you go down the various rabbit holes.

Use something like CoolTerm to do that, so you can explore and test the serial interface without having to write code every time.

1 Like

I have tried sending a command with:

but the relay does not respond

Any ideas how the code should look to turn on relay 1?