Unable to write to /dev/ttyUSB0 using fopen or php_serial.class.php

I'm trying to rewrite this C script into PHP.(sample)

// send packet to network
tty_fd=open(buf50, O_RDWR | O_NOCTTY | O_SYNC);
write(tty_fd,&packet2send,34);
close(tty_fd);

This C script also reads data from /dev/ttyUSB0 in Hexadecimal but i'm having trouble with write(in php).

I've been able to successfully read, but I'm afraid writing isn't working - or I have been unable to see the content of what I write.

I've used fopen:

$request_packet_ok = dechex(ord("~")). " 02 ff ff 07 ff ab 2c 4d 65 73 73 61 67 65 20 43 43 20 23 31 2f 34 20 20 20 20 ff ff d2 d7 00 00 b5 7a\r"; //Means "Message CC #1/4" on the thermostat


$device = "/dev/ttyUSB0";

if (is_writable($device)) {
    echo "\n".'The file is writable'."\n";

    $fp =fopen($device, "w+");
    if( !$fp) {
        echo "Error";die();
    }

    while(true){

    echo "\n".'writing: '.$request_packet_ok;
    fwrite($fp, $request_packet_ok);
    $packet='';
    $var =  fread($fp, 35);
    for ($i=0; $i < strlen($var);$i++){
        $packet .=  dechex(ord( substr($var,$i,1) ) );
    }
    var_dump($packet);
}
fclose($fp);

i've also tried using the php_serial.class.php:

define('PACKET_SIZE_LIMIT',70);
$serial = new phpSerial;
$serial->deviceSet('/dev/ttyUSB0');
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("rts/cts");
$serial->deviceOpen();
$starttime = microtime(true);


$sof='~';
$packet = '';
$output = '';
while (true) {
    $read = $serial->readPort();
    if ($read) {


        while (strlen($output) < PACKET_SIZE_LIMIT){
            for($i = 0; $i <strlen($read);$i++){
                $each_char = substr($read,$i,$i+1);

                if ( strlen(dechex(ord($each_char))) != 2 ){
                    $output.= sprintf('%02d',dechex(ord($each_char)));
                }else{
                    $output.= dechex(ord($each_char));
                }

            }
             $read = $serial->readPort();
        }

        echo $output;

    }


}


$serial->deviceClose();

The purpose is to send messages to a thermostat using PHP rather than using C code.

Since I am able to Read the packets being sent by the antenna using php, I'm assuming I can access /dev/ttyUSB0 to write but for some reason, I can never see the content I had sent/write to the antenna (which sends the text message to the thermostat display - that doesn't happen). I also don't get any errors.. at all!

I'm looking to write to /dev/ttyUSB0 and then read what I wrote from it (to check that it works - as a test).

I am getting no permission denied, and the antenna looks like the image attached (plugged it to windows to show you, it needs to run on Ubuntu)

i have tried using the following commands to see the content at no avail (when I run the php code on that machine using root privileges):

screen /dev/ttyUSB0 19200
od -x < /dev/ttyUSB0

How can I send a message using php via /dev/ttyUSB0 and receive that very message? or at least have some sort of simple test to confirm that what was sent, can actually be read.

Answers


In the C program you write(tty_fd,&packet2send,34); i. e. 34 bytes. In your PHP script you fwrite($fp, $request_packet_ok); where $request_packet_ok is a string of 105 Bytes. Obviously you are mistakenly sending the message in hexadecimal space-separated representation, while you should rather send it as raw bytes.


Need Your Help

How is F#'s static bound constraints implemented?

c# .net f# constraints language-lawyer

In F#, you can perform black-magic voodoo1 and perform static typed constraints to ensure a function is only called on types that have the member constraints. For example: