8 Channel USB Relay with MacHIDMBS

  1. last week

    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

    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

    @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.

  2. Christian S

    Aug 12 Pre-Release Testers, Xojo Pro Europe (Germany)

    Are you sure it is a HID Device at all?

  3. 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.

  4. Derk J

    Aug 12 Pre-Release Testers, Xojo Pro

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

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

  6. 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

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

    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\n");
    return 1;
    }
    mask = (unsigned char)(1U << (relaynum-1));
    cmd2 = (unsigned char)relaynum;
    if (is_on) {
    cmd1 = 0xFF;
    maskval = mask;
    } else {
    cmd1 = 0xFD;
    maskval = 0;
    }
    }

  7. Derk J

    Aug 12 Pre-Release Testers, Xojo Pro
    Edited last week by Derk J

    @Julian-Christoph&nbsp;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

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

  9. Derk J

    Aug 12 Pre-Release Testers, Xojo Pro Answer
    Edited last week by Derk J

    @Julian-Christoph&nbsp;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.

  10. 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)

  11. Christian S

    Aug 13 Pre-Release Testers, Xojo Pro Europe (Germany)

    Great!

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

  12. 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

  13. Christian S

    Aug 14 Pre-Release Testers, Xojo Pro Europe (Germany)

    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:

    // 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)

  14. 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)

or Sign Up to reply!