arthur64 Posted August 24, 2025 Share Posted August 24, 2025 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 August 25, 2025 Share Posted August 25, 2025 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 August 28, 2025 Author Share Posted August 28, 2025 $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 August 28, 2025 Share Posted August 28, 2025 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 August 28, 2025 Share Posted August 28, 2025 (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 August 28, 2025 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...
arthur64 Posted September 9, 2025 Author Share Posted September 9, 2025 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! Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1659630 Share on other sites More sharing options...
kicken Posted September 9, 2025 Share Posted September 9, 2025 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. Quote Link to comment https://forums.phpfreaks.com/topic/330335-lcus-8-usb-relay-module-8-channel/#findComment-1659631 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.