Shop OBEX P1 Docs P2 Docs Learn Events
Receiving data from stamp using PHP — Parallax Forums

Receiving data from stamp using PHP

CarloCarlo Posts: 23
edited 2006-01-04 00:16 in BASIC Stamp
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

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-04-23 18:36
    If your BS2 is doing:

    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.
  • CarloCarlo Posts: 23
    edited 2005-04-23 19:36
    I tried what you told me but it doesn't seem to work. There must be something wrong witht the PHP code because the browser remains stuck and doesn't display anything. I am thinking that PHP can't interpret what it is receiving from the serial port.

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-04-24 13:46
    One other possibility is that PHP is holding the 'DTR' signal HIGH, which keeps the BS2 in reset.

    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.
  • CarloCarlo Posts: 23
    edited 2005-04-26 22:19
    I don't think this is the problem because you already raised this eventuality when I was trying to input commands from the computer and in the end that wasn't the problem. In addition I am not sure what you asked me to do (keep in mind my knowledge in this field is close to nothing).


    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
  • voodoofishvoodoofish Posts: 67
    edited 2005-04-28 21:03
    well, I'm fairly familiar with php, and I don't believe that what you're doing will work. The fopen function works with streams, but very specific streams like sockets and such.

    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
  • CarloCarlo Posts: 23
    edited 2005-04-28 21:13
    Thanks for your reply, I wasn't hoping anyone would look into this poroblem anymore.

    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
  • voodoofishvoodoofish Posts: 67
    edited 2005-04-28 21:49
    well from the link I found below, you're not executing the line that sets your com port....maybe that was something that was omitted?

    Mark

    This looks very similar, bit slightly differnt variant...
    http://www.php.net/manual/en/function.dio-tcsetattr.php
  • OrionOrion Posts: 236
    edited 2005-04-28 22:06
    You mentioned that you got it to work the opposite way before, correct? I would think that if you where able to send data to the comm port via php you should be able to read it. I looked into this a while ago and could not find squat about serial comms with php. Can’t help you there. I have gotten php to talk to the bs though http. I use a siteplayer to hold web pages with all my variables. Then with php I parse in the page to add in graphics and manipulate the data into human friendly numbers. The bs is monitoring the siteplayer status. If someone views a page, it checks for changes and runs with it.
  • OrionOrion Posts: 236
    edited 2005-04-28 22:13
    voodoofish said...
    well from the link I found below, you're not executing the line that sets your com port....maybe that was something that was omitted?

    Mark

    This looks very similar, bit slightly differnt variant...
    http://www.php.net/manual/en/function.dio-tcsetattr.php
    Interesting, it must be possible.· Wonder how well that code runs on a windows php version?
  • voodoofishvoodoofish Posts: 67
    edited 2005-04-28 22:20
    I don't know, but I was considering trying it just to see what happens. If so, I might use this to collect data with the ping range finder for testing purposes...

    Mark

    If I do this tonight, I'll let ya know how it worked....
  • OrionOrion Posts: 236
    edited 2005-04-28 22:36
    Sweet...

    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.
  • voodoofishvoodoofish Posts: 67
    edited 2005-04-28 23:19
    I hear ya orion, I've gotten used to doing the tweaking on boths platforms. Still, the documention has gotten better, especially the online docs with all the comments....those can be invaluable...

    Mark
  • OrionOrion Posts: 236
    edited 2006-01-04 00:16
    Bringing this back from the long dead but, I had some time recently to test php with the serial port and have some more input.


    `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.
Sign In or Register to comment.