# 8 Channel USB Relay with MacHIDMBS

**URL:** <https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177>\
**Category:** Extras\
**Created:** [August 12, 2017, 10:19am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177 "2017-08-12T10:19:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 12, 2017, 10:19am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/1 "2017-08-12T10:19:28Z")

</div>

Hi all,

I just got myself an 8 Channel USB Relay that I intend to control with the MacHIDMBS PlugIn. The relay looks similar to this one: [https://www.sainsmart.com/sainsmart-4-channel-12-v-usb-relay-board-module-controller-for-automation-robotics-1.html](https://www.sainsmart.com/sainsmart-4-channel-12-v-usb-relay-board-module-controller-for-automation-robotics-1.html)

I am able to establish a connection successfully and read out properties from the relay such as ProductID, VendorID and SerialNumber using MacHIDMBS.

However does anyone have experience what exactly to do to get the individual relays switching? According to the technical documentation it expects an 0x50 at the beginning (I assume this has been done through MacHIDMBS.Connect). It is ready to receive commands after 0x51. I am trying to send messages via MacHIDMBS.SendMessage and MacHIDMBS.SendMessageMemory, however the relay never reacts.  
Does anyone have experience with those relays on Xojo?

Thanks a lot,  
Julian

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 12, 2017, 10:20am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/2 "2017-08-12T10:20:12Z")

</div>

Are you sure it is a HID Device at all?

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 12, 2017, 11:16am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/3 "2017-08-12T11:16:37Z")

</div>

Well, I thought so. I used the example “USB HID Devices Mac” coming with the MBS USB Plug-In. My relay is listed there and following the comment in the example the “MacHIDMBS” class could be used to talk to it.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [August 12, 2017, 12:08pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/4 "2017-08-12T12:08:48Z")

</div>

It has an FTDI chip. So it could be a serial device

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 12, 2017, 12:34pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/5 "2017-08-12T12:34:10Z")

</div>

I tried with the SerialControl, however when listing all Serial-connected devices on my MacBook, it does not appear there.

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 12, 2017, 12:42pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/6 "2017-08-12T12:42:50Z")

</div>

It basically is this relay and documentation by the V-USB team exists, however just as .dll.  
[http://vusb.wikidot.com/project:driver-less-usb-relays-hid-interface](http://vusb.wikidot.com/project:driver-less-usb-relays-hid-interface)

The .dll shows how to handle the relay, however this I would love to realize in Xojo directly.

[quote]static int rel\_onoff( USBDEVHANDLE dev, int is\_on, int relaynum )  
{  
unsigned char buffer[10];  
int err = -1;  
unsigned char cmd1, cmd2, mask, maskval;

```
if ( relaynum < 0 && (-relaynum) <= 8 ) {
    mask = 0xFF;
    cmd2 = 0;
    if (is_on) {
       cmd1 = 0xFE;
       maskval = (unsigned char)( (1U << (-relaynum)) - 1 );
    } else {
        cmd1 = 0xFC;
        maskval = 0;
    }
} else {
    if ( relaynum <= 0 || relaynum > 8 ) {
        printerr("Relay number must be 1-8\

```

");  
return 1;  
}  
mask = (unsigned char)(1U \<\< (relaynum-1));  
cmd2 = (unsigned char)relaynum;  
if (is\_on) {  
cmd1 = 0xFF;  
maskval = mask;  
} else {  
cmd1 = 0xFD;  
maskval = 0;  
}  
}  
[/quote]

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [August 12, 2017, 2:38pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/7 "2017-08-12T14:38:32Z")

</div>

> [@345191:@Julian-Christoph Huss](#):
>
> I tried with the SerialControl, however when listing all Serial-connected devices on my MacBook, it does not appear there.

You’d need ftdi vcp drivers i guess. Not sure of apple provides those for you

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 12, 2017, 2:48pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/8 "2017-08-12T14:48:05Z")

</div>

Thanks, Derk! Let me have a look for those.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [August 12, 2017, 4:50pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/9 "2017-08-12T16:50:13Z")

</div>

> [@345206:@Julian-Christoph Huss](#):
>
> Thanks, Derk! Let me have a look for those.

Found this for you, on the web:

Communication Parameters:  
8 Data, 1 Stop, No Parity  
Baud rate : 9600

Commands:  
FIRST channel commands:  
OFF command: FF 01 00 (HEX) or 255 1 0 (DEC)  
ON command: FF 01 01 (HEX) or 255 1 1 (DEC)

SECOND channel commands:  
OFF command: FF 02 00 (HEX) or 255 2 0 (DEC)  
ON command: FF 02 01 (HEX) or 255 2 1 (DEC)

THIRD channel commands:  
OFF command: FF 03 00 (HEX) or 255 3 0 (DEC)  
ON command: FF 03 01 (HEX) or 255 3 1 (DEC)

FOURTH channel commands:  
OFF command: FF 04 00 (HEX) or 255 4 0 (DEC)  
ON command: FF 04 01 (HEX) or 255 4 1 (DEC)

That’s for the four channel version.

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 13, 2017, 12:54am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/11 "2017-08-13T00:54:47Z")

</div>

Great, thanks, Derk. It is working now. I actually still use the MacHIDMBS class and then send the following commands as you suggested.

dim mb as New MemoryBlock(2)

'switching relays ON with 255  
mb.byte(0)=255  
mb.byte(1)=4 'relay number 1-8

'switching relays OFF with 253  
mb.byte(0)=253  
mb.byte(1)=4 'relay number 1-8

m.SendMessageMemory(mb,0,2)

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 13, 2017, 6:32pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/12 "2017-08-13T18:32:37Z")

</div>

Great!

Please check the HIDAPI classes for doing this cross platform:  
[http://monkeybreadsoftware.net/pluginpart-hidapi.shtml](http://monkeybreadsoftware.net/pluginpart-hidapi.shtml)

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 14, 2017, 2:28am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/13 "2017-08-14T02:28:59Z")

</div>

Thanks, Christian!

On Mac it works well with the HIDAPI classes. I transfer the same memoryblock through HIDAPIDeviceMBS.SendFeatureReport and receive back an “8” from Mac.

On Windows however I supposed it would work the same, however it returns me a “-1” as soon as I transfer the same memoryblock. Whether using HIDAPIDeviceMBS.SendFeatureReport or HIDAPIDeviceMBS.Write makes no difference.

Any idea what I should do differently for Windows applications?

Thank you,  
Julian

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 14, 2017, 8:30am UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/14 "2017-08-14T08:30:21Z")

</div>

I think Windows is more picky about the block sizes.  
So can you ask for Report size and than make sure your block matches?

and block may need to be 1 bigger to put the report ID in the first byte:

[code] // Send a Feature Report to the device  
dim buf as new MemoryBlock(17)

buf.UInt8Value(0) = 2 // First byte is report number  
buf.UInt8Value(1) = &ha0  
buf.UInt8Value(2) = &h0a  
dim r as integer  
r = d.SendFeatureReport(buf)  
[/code]

---

<div class="post-metadata">

**Author:** ![Julian-Christoph\_Hus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Julian-Christoph\_Hus](https://forum.xojo.com/u/Julian-Christoph_Hus)\
**Post date:** [August 14, 2017, 1:03pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/15 "2017-08-14T13:03:27Z")

</div>

Thanks, Christian!

It is working now since - as you said - Windows expected an extra byte upfront. The solution below works well for Mac and Windows with the HIDAPI Class.

dim res as integer  
dim mb as New MemoryBlock(9)

mb.byte(0)=0 'always 0  
mb.byte(1)=255 '255: single on, 253: single off, 254: all on, 252: all off  
mb.byte(2)=1 'relay number (1-8)  
res=wDevice.SendFeatureReport(mb)

---

<div class="post-metadata">

**Author:** ![Donald\_McCluskey](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@Donald\_McCluskey](https://forum.xojo.com/u/Donald_McCluskey)\
**Post date:** [September 22, 2017, 12:46pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/16 "2017-09-22T12:46:56Z")

</div>

Hi Everyone,  
I recently purchased what looks like [the same relay-board module](http://www.ebay.com/itm/8-Channel-USB-Relay-Module-7-30V-10A-Soft-Auto-control-Relay-board-with-cable/321958198371?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649), so I was glad to see this conversation since mine has absolutely no specs or info with it.

I have not been successful in getting any kind of response from the board, aside from illuminating the USB LED on the board. When I unplug and re-plug the USB cable, I can hear all of the relays activating, so I’m pretty confident that it’s alive and well. Also, it’s pretty clear that this board is using COM5.

So here are my questions: am I supposed to have some sort of additional driver or the like, such as MacHIDMBS PlugIn that was mentioned in the initial posting or is the Xojo Serial control enough? I’m running on Windows, so I don’t have an option to use that. Is there a Windows version? Is it necessary when using the Xojo “Serial” control?

Regarding my code, I’m using the following (BTW, my Serial control is named TestSerial, which I hijacked from another Xojo example program):  
In the Window “Open” event, I execute the following code:

`
TestSerial.SerialPort = System.SerialPort( SerialPortPopup.ListIndex ) 'this is also from the hijacked code...
'Communication Parameters:
'8 Data, 1 Stop, No Parity
'Baud rate : 9600
TestSerial.Bits=Serial.Baud9600
TestSerial.Stop=Serial.StopBits1
TestSerial.Parity=Serial.ParityNone
TestSerial.Baud=Serial.Baud9600`

I’ve placed the following code in the “Action” event of a push button control… I’ve tried many variations of it, but this is pretty much what I’ve learned from this conversation. Please let me know where I’ve erred or if there’s something more that I need to do.

[code]Dim mb as new MemoryBlock(4)  
dim res as integer

'Initialize the communication with the module  
mb.byte(0)= &h0  
mb.byte(1)=&h50 '255: single on, 253: single off, 254: all on, 252: all off  
mb.byte(2)=&h51 'relay number (1-8)  
mb.Byte(3)=&h0  
TestSerial.write(mb)  
TestSerial.flush

'Attempt to turn on all relays  
mb.byte(0)= &h0  
mb.byte(1)=&h252 '255: single on, 253: single off, 254: all on, 252: all off  
mb.byte(2)=&h1 'relay number (1-8) - another bump to try to turn on relay 1  
mb.Byte(3)=&h0  
TestSerial.write(mb)  
TestSerial.flush[/code]

I would greatly appreciate any help that you could offer - my autopilot lawnmower hangs in the balance! Thanks in advance for your help,  
Don

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 22, 2017, 12:53pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/17 "2017-09-22T12:53:10Z")

</div>

How does device register with system?  
Does it sign up as serial device or as HID device? Or something else?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 22, 2017, 12:53pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/18 "2017-09-22T12:53:29Z")

</div>

Does it show up in System.SerialPort list?

---

<div class="post-metadata">

**Author:** ![Donald\_McCluskey](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@Donald\_McCluskey](https://forum.xojo.com/u/Donald_McCluskey)\
**Post date:** [September 22, 2017, 2:05pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/19 "2017-09-22T14:05:19Z")

</div>

Yes, the following code executes successfully and COM5 is added to the popup listbox when the code is executed. That would lead me to believe that it is a serial device. Is that a reasonable assumption?

[code]// Populate the popup menu with all of the  
// serial ports the system has installed.  
Dim count As Integer  
count = System.SerialPortCount

For i As Integer = 0 To count - 1  
SerialPortPopup.AddRow( System.SerialPort( i ).Name )  
Next[/code]

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 22, 2017, 5:42pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/20 "2017-09-22T17:42:42Z")

</div>

yes. So try Serial control.

---

<div class="post-metadata">

**Author:** ![Donald\_McCluskey](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@Donald\_McCluskey](https://forum.xojo.com/u/Donald_McCluskey)\
**Post date:** [September 22, 2017, 6:18pm UTC](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177/21 "2017-09-22T18:18:13Z")

</div>

The control is the Serial control (it’s the control that looks like a USB plug in the library). Is my code using something that conflicts with that? I’m in unfamiliar territory here, so I would greatly appreciate any direction you could give.  
Thanks

[Next page](https://forum.xojo.com/t/8-channel-usb-relay-with-machidmbs/39177.md?page=2)
