Any experiences with php_serial.class?

Hi,

I tried to use php_serial.class Community Trademarks - Alter to communicate with my Arduino.
I do not understand one thing.

When I send:
$serial->sendMessage("0");
Arduino receive 48 - ok, It looks like ASCII.

When I send:
$serial->sendMessage("1");
I expect 49, but Arduino receive 177?

Could somebode please explain my what is going on?

I am doing the same thing!

I have a couple of questions:

  1. how do you know what Arduino is receiving?
  2. would you like to post some php code?
  1. would you like to post some php code?

Here you have some code:

require("php_serial.class.php"); 
     
    //Initialize the class 
    $serial = new phpSerial(); 

    //Specify the serial port to use... in this case COM1 
    $serial->deviceSet("COM7"); 
     
    //Set the serial port parameters. The documentation says 9600 8-N-1, so 
    $serial->confBaudRate(9600); //Baud rate: 9600 
    $serial->confParity("none");  //Parity (this is the "N" in "8-N-1") 
    $serial->confCharacterLength(8); //Character length     (this is the "8" in "8-N-1") 
    $serial->confStopBits(1);  //Stop bits (this is the "1" in "8-N-1") 
    $serial->confFlowControl("none");
      //Device does not support flow control of any kind, 
      //so set it to none. 

    //Now we "open" the serial port so we can write to it 
    $serial->deviceOpen(); 

    //Issue the appropriate command according to the serial relay 
    //board documentation 
  
      //to turn relay number 1 on, we issue the command  
      //  $serial->sendMessage(chr(10)); // start transmission
      $serial->sendMessage("1");
      //$serial->sendMessage(chr(13)); // end transmission

     
  
    //We're done, so close the serial port again 
    $serial->deviceClose();

or here: Community Trademarks - Alter

But how you already know it does not work how is expected.
I tried with PHP Serial extension and it works much better: http://www.soft3k.com/PHP-serial-extension-p11417.htm

If you use a Linux server you can look at this: http://www.arduinoprojects.com/2008/04/21/controlling-arduino-via-usb-using-php/

The php serial class has worked flawlessly for our applications, so we need to see your Arduino sketch, eg, the part thats giving you the strange output.

This extension

http://www.soft3k.com/PHP-serial-extension-p11417.htm

Costs $32 and it isn't necessary to pay that since the serial class is available for "free".

This might be the Arduino routine your missing. It checks for the header [$serial->sendMessage(chr(10));] first, then reads until it finds the end [$serial->sendMessage(chr(13));]

void checkSerial() {
  if(Serial.available() > 0) {          // if data available 
    if((val = Serial.read()) == 10) {   // check for header start 
      bytesread = 0; 
      while(bytesread<1) {              // read 1 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if(val == 13) { // check for header end  
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 
      if(bytesread == 1) {              // if 1 digit read is complete 
        incomingByte = int(code[0]);
        doSomething();
      } 
      bytesread = 0; 
      delay(50);                       // wait for a second 
    } 
  } 


}

Anything in between chr(10) and chr(13) gets stored in code[]. The above reads one character then calls doSomething() taking with it the value of incomingByte / code[0]

Anything in between chr(10) and chr(13) gets stored in code[]. The above reads one character then calls doSomething() taking with it the value of incomingByte / code[0]

My Arduino reads what PHP sends and display it on LCD using I2C (look at //MY PART OF CODE - Arduino code).

My PHP code:

require("php_serial.class.php"); 
$serial = new phpSerial();    
$serial->deviceSet("COM7"); 
 
 $serial->confBaudRate(9600); //Baud rate: 9600 
 $serial->confParity("none");  //Parity (this is the "N" in "8-N-1") 
 $serial->confCharacterLength(8); //Character length     (this is the "8" in "8-N-1") 
 $serial->confStopBits(1);  //Stop bits (this is the "1" in "8-N-1") 
 $serial->confFlowControl("none");
      
 $serial->deviceOpen(); 

 $serial->sendMessage(chr(10)); // start transmission
 $serial->sendMessage(0); //THIS IS WHAT I SEND
 $serial->sendMessage(chr(13)); // end transmission

 $serial->deviceClose();

and Arduino code:

void checkSerial() {
  if(Serial.available() > 0) {         
    if((val = Serial.read()) == 10) {   
      bytesread = 0; 
      while(bytesread<1) {              
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if(val == 13) {  
            break;                       
          } 
          code[bytesread] = val;                    
          bytesread++;                   
        } 
      } 
      if(bytesread == 1) {            
        incomingByte = int(code[0]);
        
                  //MY PART OF CODE
        char stringvalue[5];    
        lcd.clear();
        lcd.printIn(itoa(incomingByte, stringvalue, 10)); //convert string to char
                  //MY PART OF CODE
        bytesread = 0; 
      delay(50);                   
      } 
    } 
  }
}

When I send 0 (look at //THIS IS WHAT I SEND - php code) on my lcd I can see 48, so I think it works, I see ASCII code 48 = 0;
Byt when i send 1 (sendMessage(1); ) I expect 49 (ASCII 49 = 1) but my display shows 177!

Tell me why?
Maybe it works correctly only I get it wrong!?

I am learning a lot from you guys! Thanks!
Please continue to post and soon I will show you something smashing!

Anything in between chr(10) and chr(13) gets stored in code[]. The above reads one character then calls doSomething() taking with it the value of incomingByte / code[0]

My Arduino reads what PHP sends and display it on LCD using I2C (look at //MY PART OF CODE - Arduino code).

My PHP code:

require("php_serial.class.php"); 

$serial = new phpSerial();    
$serial->deviceSet("COM7");

$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none");  //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length     (this is the "8" in "8-N-1")
$serial->confStopBits(1);  //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none");
     
$serial->deviceOpen();

$serial->sendMessage(chr(10)); // start transmission
$serial->sendMessage(0); //THIS IS WHAT I SEND
$serial->sendMessage(chr(13)); // end transmission

$serial->deviceClose();



and Arduino code:


void checkSerial() {
 if(Serial.available() > 0) {        
   if((val = Serial.read()) == 10) {  
     bytesread = 0;
     while(bytesread<1) {              
       if( Serial.available() > 0) {
         val = Serial.read();
         if(val == 13) {  
           break;                      
         }
         code[bytesread] = val;                    
         bytesread++;                  
       }
     }
     if(bytesread == 1) {            
       incomingByte = int(code[0]);
       
                 //MY PART OF CODE
       char stringvalue[5];    
       lcd.clear();
       lcd.printIn(itoa(incomingByte, stringvalue, 10)); //convert string to char
                 //MY PART OF CODE
       bytesread = 0;
     delay(50);                  
     }
   }
 }
}




When I send 0 (look at //THIS IS WHAT I SEND - php code) on my lcd I can see 48, so I think it works, I see ASCII code 48 = 0;
Byt when i send 1 (sendMessage(1); ) I expect 49 (ASCII 49 = 1) but my display shows 177!

Tell me why?
Maybe it works correctly only I get it wrong!?

This

$serial->sendMessage(0); //THIS IS WHAT I SEND

Should be this

$serial->sendMessage("0"); //THIS IS WHAT I SEND

When you SendMessage(2) do you get "178"?
Are you calling Serial.begin(9600)?
Can you tell us what types "val", "code", and "incomingByte" are?
Try calling lcd.printIn(itoa(1, stringvalue, 10)). If that works, then you would have to suspect that incomingByte is not 1.

-- M

This returns 48
$serial->sendMessage("0");
and this returns 49
$serial->sendMessage("1");

We've used the class and Arduino code for months, you only need to remember to use quotation marks and the problem is solved. If not, post ALL of your code, including a link to the copy of serial_class.php your using, unless you used mine :slight_smile:

This returns 48
$serial->sendMessage("0");
and this returns 49
$serial->sendMessage("1");
We've used the class and Arduino code for months, you only need to remember to use quotation marks and the problem is solved.

Now I use quotation, and that is what I see on my LCD:

sendMessage("0"); - on LCD 48 //correct
sendMessage("1"); - on LCD 177
sendMessage("2"); - on LCD 178
sendMessage("3"); - on LCD 51 //correct
sendMessage("4"); - on LCD 180
sendMessage("5"); - on LCD 53 //correct
sendMessage("6"); - on LCD 54 //correct
sendMessage("7"); - on LCD 183

Some time ago I was trying the code from here: http://www.arduinoprojects.com/2008/04/21/controlling-arduino-via-usb-using-php

$fp =fopen("com7", "w");
fwrite($fp, chr(1));
flose($fp);

And I had the same problem.

I am using Windows server.
I am calling Serial.begin(9600).

I tried lcd.printIn(itoa(1, stringvalue, 10)) and I can see on my LCD 1.

Link to my rar package, there you can find my php code, arduino code, and the class I use: http://monrose.pl/arduino_php.rar

If not, post ALL of your code, including a link to the copy of serial_class.php your using, unless you used mine :slight_smile:

Where can I find your class?

Can you edit/copy/paste your code between [code tags] at the forum. Include your php and your Arduino sketch.

Here's a copy of the class we use, and it definitely gives us 48 & 49 on "0" and "1", cause I just checked it :slight_smile:

http://www.modxhost.com/php_serial.class.txt

Have you run a test several times to see if the result is always the same?

Are you tried using a LAMP stack? what version of php is your server running?

We use a MAMP stack on OSX, and WAMP on Windows XP, both work fine. We haven't tried in on Windows Server because its well, a Microsoft product ;D

Can you edit/copy/paste your code between [code tags] at the forum. Include your php and your Arduino sketch.

Here's a copy of the class we use, and it definitely gives us 48 & 49 on "0" and "1", cause I just checked it :slight_smile:

http://www.modxhost.com/php_serial.class.txt

Have you run a test several times to see if the result is always the same?

Are you tried using a LAMP stack? what version of php is your server running?

We use a MAMP stack on OSX, and WAMP on Windows XP, both work fine. We haven't tried in on Windows Server because its well, a Microsoft product ;D

I see we use the same class.
I've run a test several times and the result is always the same.
I am using Windows XP as a server not Windows Server (my mistake).
I am using Apache/2.2.6 (Win32) PHP/5.2.5.
I do not konw what LAMP stack is... I will check.

Interesting...

sendMessage("0"); - on LCD 48 //correct
sendMessage("1"); - on LCD 177
sendMessage("2"); - on LCD 178
sendMessage("3"); - on LCD 51 //correct
sendMessage("4"); - on LCD 180
sendMessage("5"); - on LCD 53 //correct
sendMessage("6"); - on LCD 54 //correct
sendMessage("7"); - on LCD 183

Note that 177 is 49 (the expected value) plus 0x80 (128). Each one of the incorrect values is wrong just in that one high-order bit. This leads me to wonder whether there might be a physical problem. We certainly can eliminate the php_serial component, since you saw the same error with the fopen/fwrite/fclose C sequence.

I just ran your (slightly modified - no LCD) code on my system. It seems to work fine here!

Mikal

Can you edit/copy/paste your code between [code tags] at the forum. Include your php and your Arduino sketch.

Here's a copy of the class we use, and it definitely gives us 48 & 49 on "0" and "1", cause I just checked it :slight_smile:

http://www.modxhost.com/php_serial.class.txt

Have you run a test several times to see if the result is always the same?

Are you tried using a LAMP stack? what version of php is your server running?

We use a MAMP stack on OSX, and WAMP on Windows XP, both work fine. We haven't tried in on Windows Server because its well, a Microsoft product ;D

I see we use the same class.
I've run a test several times and the result is always the same.
I am using Windows XP as a server not Windows Server (my mistake).
I am using Apache/2.2.6 (Win32) PHP/5.2.5.
I do not konw what LAMP stack is... I will check.

Your php version is ok. If you haven't already, get a copy of WAMP (WAMPserver.com) and install it. Then disconnect your LCD and run a debug in Arduino's serial monitor to see the data its receiving before its sent to the LCD.

Sorry I am a bit late on this thread but the original question appears to be:-

$serial->sendMessage("1");
I expect 49, but Arduino receive 177?

Now being a hardware sort of chap I like to work in hex so 177 is actually B1 in hex and an ascii 1 is 31 in hex, so in binary:-
10110001 - B1
00110001 - 31
So you see it's the top bit being set. This suggests to me that the transmit port is set 7 data 1 parity and the receive port to 8 data no parity. This would explain why every other code is correct.

Then disconnect your LCD and run a debug in Arduino's serial monitor to see the data its receiving before its sent to the LCD.

What exactly should I do?
I use my LCD display to check what arduino receive, becouse I can not use arduino's serial monitor and php serial class at the same moment.
So what do you mean with debug?

I would just AND the received character with 0x7F before you display it. It's dirty but should work.

So you see it's the top bit being set. This suggests to me that the transmit port is set 7 data 1 parity and the receive port to 8 data no parity. This would explain why every other code is correct.

I set up the transmit port with php:

 $serial->confBaudRate(9600); //Baud rate: 9600 
 $serial->confParity("none");  //Parity (this is the "N" in "8-N-1") ;
 $serial->confCharacterLength(8); //Character length     (this is the "8" in "8-N-1") ;
 $serial->confStopBits(1);  //Stop bits (this is the "1" in "8-N-1") ;
 $serial->confFlowControl("none");

That is very interesting. I thought that maybe there is sth with my serial port wrong, but I can transmit my sketches to arduino without any problems. I was trying my arduino with PHP Serial Extension and it was working fine.

Anyway, thanks for help!

Hey, this seems to be a parity problem.

sendMessage("0"); - on LCD 48 //correct
sendMessage("1"); - on LCD 177
sendMessage("2"); - on LCD 178
sendMessage("3"); - on LCD 51 //correct
sendMessage("4"); - on LCD 180
sendMessage("5"); - on LCD 53 //correct
sendMessage("6"); - on LCD 54 //correct
sendMessage("7"); - on LCD 183

All the values that come across the wire incorrectly have the odd parity
'0' = 48 = 0b00110000 (even)
'1' = 49 = 0b00110001 (odd: comes across wrongly as 0b10110001)
'2' = 50 = 0b00110010 (odd: ditto)
'3' = 51 = 0b00110011 (even)
'4' = 52 = 0b00110100 (odd)
'5' = 53 = 0b00110101 (even)
'6' = 54 = 0b00110110 (even)
'7' = 55 = 0b00110111 (odd)

It's as if PHP serial is setting a parity bit or something. I checked your syntax and everything seems kosher. Can you try removing that confFlowControl("none") line? The php example I saw didn't use that (http://www.modxhost.com/php-arduino-led-example.txt).

Mikal