1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to read RS232 serial port using PHP on windows?

Discussion in 'PHP' started by consultjohan, Mar 14, 2007.

  1. #1
    I'm trying to port the following small QBASIC program (which works 100%) to PHP:

    OPEN "com1:2400,n,8,1,DS," FOR RANDOM AS #3
    OPEN "data.dat" FOR OUTPUT AS #2
    REM read 17 chars from the port
    scale$ = INPUT$(17, #3)
    PRINT scale$
    WRITE #2, scale$
    CLOSE #2
    CLOSE #3
    SYSTEM

    I'm trying this PHP function but it just hangs at the fgets() line:

    function read_port($port='COM1:', $length=17, $setmode=TRUE, $simulate='') {
    if ($simulate){
    $buffer = '"'.strval(rand(1000, 2000));
    return $buffer;
    }
    if ($setmode){ // note backtick on next line
    `mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off odsr=on octs=on dtr=on rts=on idsr=on`;
    }
    $fp = fopen ($port, "rb+");
    if (!$fp) {
    file_put_contents('debug1.log','COM1: could not open'."\n",FILE_APPEND);
    } else {
    $buffer = fgets($fp, $length); // <-- just hangs here!
    fclose ($fp);
    }
    return $buffer;
    }

    I'm using the line:

    $res = read_port('COM1:', 17, TRUE, SIMULATE_SCALE);

    to call it.

    Also a 14 day trail version of Virtual Serial Port Driver XP by Eltima which simulates my COM1/COM2 ports.

    Before I run the above function I first:

    `mode com1: baud=2400 parity=n data=8 stop=1 to=on xon=off odsr=on octs=on dtr=on rts=on idsr=on`;

    $fp = fopen ("COM2:", "wb+");
    $string='"+'.strval(rand(1000, 2000)).' '; // simulating device
    fputs ($fp, $string );
    fclose ($fp);

    Any help will be creatly appreciated! I've basically given up trying. If QBasic can do it perfectly then we must be able to make this work with PHP! I'm using PHP 5.2.1 on Windows XP SP2 & Apache 2
     
    consultjohan, Mar 14, 2007 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    What error do you get?
    Which bit doesnt work?

    What device are you trying to communicate with on the serial port?
     
    m0nkeymafia, Mar 18, 2007 IP
  3. beedk

    beedk Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Did you solve the problem??? - and if, could you post the solution :)

    Thanks in advance
    Bee
     
    beedk, Mar 7, 2008 IP
  4. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #4
    i did a similar project similar to this in my college. A device connected to printer/rs232 port and web application runs on the system which remotely accepts commands. why dont you use printer port or joystick port? it is much simpleer than RS232.
     
    olddocks, Mar 7, 2008 IP
  5. budhanes

    budhanes Peon

    Messages:
    245
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Could you have QBasic capture serial data and write it to the text file, than, have PHP read the text file? Looks like you are doing something similar with: "WRITE #2, scale$". Maybe do an fread on file 2???
     
    budhanes, Mar 7, 2008 IP