Receiving data from stamp using PHP
Carlo
Posts: 23
Hi,
I am trying to get my PHP script to receive some data from the BS2 using the serial port. I managed to do the opposite before (with your help), but now I am having problems with this code:
BS2 script:
' {$STAMP BS2}
' {$PBASIC 2.5}
Main:
SEROUT 16, 84, [noparse][[/noparse]DEC 100]
PAUSE 2500
GOTO Main
PHP script:
<?php
·· `mode com4: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
·· $fp = fopen ("COM4:", "r");
·· if (!$fp) {
······ echo "Uh-oh. Port not opened.";
·· } else {
······ $string=fgets($fp, 256);
··· echo $string;
······ fclose ($fp);
·· }
?>
Thanks for your help!!
Carlo
I am trying to get my PHP script to receive some data from the BS2 using the serial port. I managed to do the opposite before (with your help), but now I am having problems with this code:
BS2 script:
' {$STAMP BS2}
' {$PBASIC 2.5}
Main:
SEROUT 16, 84, [noparse][[/noparse]DEC 100]
PAUSE 2500
GOTO Main
PHP script:
<?php
·· `mode com4: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
·· $fp = fopen ("COM4:", "r");
·· if (!$fp) {
······ echo "Uh-oh. Port not opened.";
·· } else {
······ $string=fgets($fp, 256);
··· echo $string;
······ fclose ($fp);
·· }
?>
Thanks for your help!!
Carlo
Comments
MAIN:
SEROUT 16, 84+16384, [noparse][[/noparse]"Hello There",0]
PAUSE 10000
GOTO MAIN
Then you should get 'Hello There' every 10 seconds. The ending 'null' zero can be replaced with a 13 for '\n'.
Now, you ARE trying to open COM4: -- so your computer MUST have a 'COM4:' on it. Baud, Parity, etc are correct.
I know this is not the appropriate forum to ask for help with PHP, but you guys are definitely more helpful than those on the other forums!! Thanks again!
Carlo
Solution: Disconnect the DTR signal, or add two 0.1 uF capacitors to the DTR circuit.
I know you are setting "XON" to disabled -- but the PC hardware (and Windows) sometimes does stuff you don't want without telling you.
Maybe it's not possible to use PHP to get data from the BS2. Unless you or someone else has any suggestion I guess I'll try something else.
Thanks a lot for your help!!
Carlo
There may be a way though, if you run the script with the command line executable, though it'll be slow and I'm not sure how you'd open a com port.
or possibly another way, is using an app I saw on the parallax cd, it sent serial data to an ip...which you could then open as a socket stream and grab the data....
I'll have to check the cd when I get home...
Mark
What I was trying to say before is that this code I used to send information over the Serial port works:
`mode com4: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
$fp = fopen ("COM4:", "w+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
$string = "30\n";
fputs($fp, $string);
echo $string;
fclose ($fp);
}
I tried modifying this code, as I wrote before, but it doesn't work and I was wondering if any one of you could understand why.
I would really apreciate if you could solve this problem!! I would be able to complete a huge part of my project!!
Thanks again.
Carlo
Mark
This looks very similar, bit slightly differnt variant...
http://www.php.net/manual/en/function.dio-tcsetattr.php
Mark
If I do this tonight, I'll let ya know how it worked....
I would try it but with no stamp near the php machine, and it would be some time before I could. That is one bonus of using ethernet, lot longer cables. One thing for sure '/dev/ttyS0' isn't going to get you anywhere on windows. If replacing that with COMx works, it should be easy. That is my biggest complaint with php on windows. The commands sometimes need tweaking that is not very well documented in the php manuals. I guess I should be happy that it runs at all on windows.
Mark
`mode com4: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
This WILL execute on a windows box running php 4.3.2. You can verify it by using the MODE on the command line. A full set of parameters looks more like this
`mode com1: BAUD=9600 PARITY=N data=8 stop=1 TO=OFF XON=OFF ODSR=OFF OCTS=OFF DTR=OFF RTS=OFF IDSR=OFF`;
You can get more info with the MODE /? on command line. I had better luck getting a string from the Stamp with the TO=ON but it takes forever, TOs every time. It seemed to help if the string ends with an EOF. Ending with a LF and CR didn't seem to help much. At best, if the stamp was repeating the string I would get parts of it. Changing the FIFO buffers up/down/off didn't seem to help either. Also tried fgets, fgetc, fread none seemed to work. Next idea was to use the command line to get it done,
exec("copy /y com1 c:\apache\htdocs\serialin.txt");
then read it in as a normal file.
This would get the whole stream....most times.....if the string ended with EOF and was repeated. I also tested this with RTS/CTS handshaking and it reduced the time and eliminated the waiting with TO=ON but then received nothing. This could be my handshaking code on the stamp but, looking at the serial output with hyper term and it looked/worked fine. I searched what’s seems like the whole planet for information on serial port access via php with only a few bits of information, especially under windows.
Sending to the com port seems to work fine with the previously posted code.
I sent the last two days, (all *!%^&@ day), trying to get this to work. My thoughts are that php or dos is expecting something in the format of the string to tell it the start and end, just a guess. My final decision was to call it quits with direct access to the serial port. I got a copy of SerialIP Server that converts the serial port over to telnet. Then just open a socket in php and read/write as needed. There is a ton of software packages around to do this conversion, so far this one is working well buts it’s a bit early to tell.
Anyway hope this saves some else the frustration.
Btw I tested all this on a windows 2K box running php 4.3.2 and Apache.