arthur64 Posted Sunday at 01:50 PM Share Posted Sunday at 01:50 PM 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) Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/ Share on other sites More sharing options...
gizmola Posted Monday at 05:06 PM Share Posted Monday at 05:06 PM We would need to see some code, as well as a link to the documentation. One obvious thing to try would be to open the connection using "r+" instead of "w+". Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1658991 Share on other sites More sharing options...
arthur64 Posted 22 hours ago Author Share Posted 22 hours ago $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 Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1659156 Share on other sites More sharing options...
gizmola Posted 21 hours ago Share Posted 21 hours ago I say again: did you try to fopen with "r+"? If you fopen with "w" that is write only mode, and you can't read from it. Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1659159 Share on other sites More sharing options...
kicken Posted 3 hours ago Share Posted 3 hours ago (edited) 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 3 hours ago by kicken Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1659229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.