Jump to content

Recommended Posts

I'm working on a new project to control my hottub.  I'm designing using a RasPi 3+ and an 8 chan relay board.  I have a php function that does "$fd = fopen (/dev/ttyUSB0, w+) and fprint (fd, "0x%s", an octal string);  and relay 1 turns on, turns off - as does 2 thru 8. on off on off on... life is good.  

My problem:  Mfg info says 0xFF will return the status of all 8 relays as in "00100110".  I have tried all sorts of combinations of fprint 0xFF which seems to go OK.  Then fgets, fscanf, fread and the code just hangs.   Has anyone gotten this to work?  Can someone offer some help?

Thanks in advance.  (I'll owe u a beer)

Link to comment
https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/
Share on other sites

$oo = 0 for turn off / 1 for turn on
$relay = 1 [...8]
	$on  = array ("", "\xa0\x01\x01\xa2", "\xa0\x02\x01\xa3",...  the second octet is the relay number, third is on/off, 4th cksum
$off = array ("", "\xa0\x01\x00\xa1", "\xa0\x02\x00\xa2",...
	$fd = fopen ("/dev/ttyUSB0", "w");
switch ($oo) {
      case 0: fprintf ($fd, "0x%s", $off[$relay]); break;   
      case 1: fprintf ($fd, "0x%s", $on[$relay]); break;
    }

The above code works fine.  Comm with mfg says 341 chip doesn't xmit only rec. ?? lost in translation.  Web pages seem to say "0xFF" will return 01001000 - ????

Thanks for any help

Based on my understanding of the datasheet I saw, I don't think your current code is correct, but it might still work if the boards firmware is ignoring unnecessary data.  The 0x you have at the front of your strings should not be there as far as my understanding goes, you should just be sending raw byte values for the on/off/query commands.

Did you look at the code in the previous thread I posted that was generated by the AI?  It appears to be correct based on my understanding of the datasheet I read.  You could use that as a starting point, then modify it to query the board also (AI could probably do it for you if you).  You'd create a new function that sends the "\xFF" byte, then read 8 bytes and check if they are equal to "\x00" or "\x01" to determine the relays status.

Also, as mentioned, you need to use r+ mode when opening the device to be able to read from it, w mode is a write-only mode, so you'd be unable to read the results using that mode.

 

Edited by kicken
  • 2 weeks later...

kicken:  removed the "0x" from the fprintf line - still works, so...  Ah, my initial web search supplied a Python script (about 90 lines) which is the basis of my php script.   Your "AI generated" - don't know where you posted but not here.  I asked AI for a script - still had to add the string "\xa0\x01\x01\xa2".   Adding a fgets just after fwrite - hangs.  I'm going to call it - no read!  

1 hour ago, arthur64 said:

Your "AI generated" - don't know where you posted but not here

In your previous thread.

1 hour ago, arthur64 said:

still had to add the string "\xa0\x01\x01\xa2"

The \x?? syntax is a way of specifying a character code using a hex code in a string.  This is correct, as it gets you the bytes you need to send to the device.  The 0x prefix is a was of indicating a number is in hex in code or documentation, but not something you want to literally send to the device usually.  I'm guessing your device just ignored it and moved on until it found the valid command following it.  Not all devices would do that though.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.