Stormbringer
Posts: 19
Joined: Sat Jan 12, 2013 6:31 pm

PHP_serial and Arduino readPort() "funnyness"

Sat Jan 12, 2013 7:01 pm

Hi,
I'm working in a project which I need to send comands to an Arduino, and Arduino responds with data from sensors. I'm using PHP under Apache and php_serial class.

Well, the problem is, send data to arduino with this works:
$serial->sendMessage("some data");
But receive data with:
$read = $serial->readPort();

Don't work "everytime".

I made some tests and realized "funny" thing. As I reboot Raspberry, readport NEVER works until I open arduino SDK, open the serial monitor and close serial monitor. After close serial monitor readPort() works everytime, but, turn off raspberry, all this procedure need to be remade.

This happen with someone else? There's something I can do to solve this?
My test code is simple:

Code: Select all

<?
include "phpserial/php_serial.class.php";

$serial = new phpSerial; 


$serial->deviceSet("/dev/ttyUSB0"); //is correct

$serial->confBaudRate(115200);//matches arduino
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1); 


$serial->deviceOpen();
$serial->serialflush();
$serial->sendMessage($_GET['mess']); 
$read = $serial->readPort(); 
$serial->deviceClose(); 
echo "I receive: ".nl2br($read);



?>
Usage: http://raspberryIP/testserial.php?mess=anymessage
arduino test code:

Code: Select all

String linha = "";
char c;
int led=13;
int aux=1;
void setup(){
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  Serial.println("awaiting...");
  digitalWrite(led, HIGH);
}

void loop(){
  if(Serial.available()>0){
    c = Serial.read();
    linha.concat(c);
  }
  if(linha !=""){
     Serial.print(linha); //returns exactly what is received
    linha = "";
    if (aux ==0){//just i on off led to know the data was received
      digitalWrite(led, HIGH);
      aux=1;    
    }else{
      digitalWrite(led, LOW);
      aux=0;
    }
  }
}
Oh, the permission of read/write /dev/ttyUSB0 I think is ok, I used this:
usermod -a -G dialout www-data
to give to apache the same permission of dialout group.

My question is: What can I do to don´t need enter sdk, open and close serial monitor every time I turn on the raspberry.

Thanks(and sorry about my poor English)

Stormbringer
Posts: 19
Joined: Sat Jan 12, 2013 6:31 pm

Re: PHP_serial and Arduino readPort() "funnyness"

Mon Jan 14, 2013 5:20 pm

Another "sad" thing is, when I put a lot of "sendMessage", eventually Raspiberry Pi crashes... But this is a "side" problem, I'm able to put a "delay" on my code to wait till serial is available again...

The major problem really is the readPort without open the Arduino SDK's serial monitor.


thanks

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: PHP_serial and Arduino readPort() "funnyness"

Mon Jan 14, 2013 5:50 pm

Have you seen these http://www.phpclasses.org/discuss/package/3679/ - couple of others having similar issues...

Someone has used it, but not sure how succesfully http://******al-class/

Off the cuff stuff based on some past experience in serial comms (not php or arduino):
- does the flush have to be called after opening? would removing it have any effect?
- how fast does the arduino reply? would it help adding a delay between write and read, or maybe read in a loop until got something?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

dan_fritz
Posts: 13
Joined: Tue Jul 31, 2012 3:36 pm
Location: Belgium
Contact: Website

Re: PHP_serial and Arduino readPort() "funnyness"

Sun Jan 20, 2013 6:02 pm

-rst- wrote:Have you seen these http://www.phpclasses.org/discuss/package/3679/ - couple of others having similar issues...

Someone has used it, but not sure how succesfully http://www.


Off the cuff stuff based on some past experience in serial comms (not php or arduino):
- does the flush have to be called after opening? would removing it have any effect?
- how fast does the arduino reply? would it help adding a delay between write and read, or maybe read in a loop until got something?
I created the ArduinoPi and have quite some experience with php and serial port. I created a working API that you use with PHP or Python. More explanation can be found here:

http://www.


Also note that I didnt connect my Arduino through USB with the Raspberry Pi, I used the breakout pins (and they always work, so no funny stuff anymore). You can find all the information (connection, electronics) in the link above.

So that answers the question, use my library and API, any question about it ask me ;)
Created of the ArduinoPi, a PHP/Python API for controlling an Arduino with the Raspberry Pi.
More info: http://www.fritz-hut.com/all-projects/arduinopi/

Stormbringer
Posts: 19
Joined: Sat Jan 12, 2013 6:31 pm

Re: PHP_serial and Arduino readPort() "funnyness"

Sun Jan 20, 2013 6:24 pm

-rst- wrote:Have you seen these http://www.phpclasses.org/discuss/package/3679/ - couple of others having similar issues...

Someone has used it, but not sure how succesfully http://www

Off the cuff stuff based on some past experience in serial comms (not php or arduino):
- does the flush have to be called after opening? would removing it have any effect?
- how fast does the arduino reply? would it help adding a delay between write and read, or maybe read in a loop until got something?
Hi,

I will read those links, thanks :D.
Well, I tried flush before too, without success.
Arduino reply almost instantly using the SDK's "serial monitor", both ways, read and write, the problem it's only with the php_serial :(

Anyway I will try the links you posted :D. Thanks

Stormbringer
Posts: 19
Joined: Sat Jan 12, 2013 6:31 pm

Re: PHP_serial and Arduino readPort() "funnyness"

Sun Jan 20, 2013 6:28 pm

dan_fritz wrote: I created the ArduinoPi and have quite some experience with php and serial port. I created a working API that you use with PHP or Python. More explanation can be found here:

http://www.fritz

Also note that I didnt connect my Arduino through USB with the Raspberry Pi, I used the breakout pins (and they always work, so no funny stuff anymore). You can find all the information (connection, electronics) in the link above.

So that answers the question, use my library and API, any question about it ask me ;)
Yes, I'm trying with the usb conection, because the operational voltage... I dont know if the 5v of arduino can "fry" my Raspberry. I will need a level converter, right?
I know some new arduinos operate at 3.3... And in this case, even I2C protocol may be used without a level converter.

Thanks :D, I will take a look on your API, sounds good ;)

dan_fritz
Posts: 13
Joined: Tue Jul 31, 2012 3:36 pm
Location: Belgium
Contact: Website

Re: PHP_serial and Arduino readPort() "funnyness"

Sun Jan 20, 2013 9:26 pm

Stormbringer wrote:
dan_fritz wrote: I created the ArduinoPi and have quite some experience with php and serial port. I created a working API that you use with PHP or Python. More explanation can be found here:

http://www.fr

Also note that I didnt connect my Arduino through USB with the Raspberry Pi, I used the breakout pins (and they always work, so no funny stuff anymore). You can find all the information (connection, electronics) in the link above.

So that answers the question, use my library and API, any question about it ask me ;)
Yes, I'm trying with the usb conection, because the operational voltage... I dont know if the 5v of arduino can "fry" my Raspberry. I will need a level converter, right?
I know some new arduinos operate at 3.3... And in this case, even I2C protocol may be used without a level converter.

Thanks :D, I will take a look on your API, sounds good ;)
I've added a logic level shifter (you do need one unless you have a 3.3V Arduino uno. You can buy one at Adafruit/Sparkfun or you can create one yourself (cheaper and a bit of electronics).

So for using the external UART (breakout pins): http://-raspberry-pi/

Then you can read the steps for configuring the php serial: http://www.-class/

And then you can find the API information in the link I already posted. I should really put together one post describing everything :p
Created of the ArduinoPi, a PHP/Python API for controlling an Arduino with the Raspberry Pi.
More info: http://www.fritz-hut.com/all-projects/arduinopi/

bjhanna
Posts: 2
Joined: Thu Mar 07, 2013 12:15 am

Re: PHP_serial and Arduino readPort() "funnyness"

Thu Mar 07, 2013 12:21 am

I've only just started fooling around with the phpSerial class and noticed the same thing. It only works while I have the serial port open via Python. I'm guessing there's some kind of initialisation going on in python (and Arduino serial monitor) that isn't being done by the php class.

Stormbringer
Posts: 19
Joined: Sat Jan 12, 2013 6:31 pm

Re: PHP_serial and Arduino readPort() "funnyness"

Thu Mar 07, 2013 10:56 am

bjhanna wrote:I've only just started fooling around with the phpSerial class and noticed the same thing. It only works while I have the serial port open via Python. I'm guessing there's some kind of initialisation going on in python (and Arduino serial monitor) that isn't being done by the php class.
I just give-up phpserial and I'm using i2c protocol with a level converter ;)


BUT if you are able to start with python using php exec() before readport, I think this will work

bjhanna
Posts: 2
Joined: Thu Mar 07, 2013 12:15 am

Re: PHP_serial and Arduino readPort() "funnyness"

Tue Mar 12, 2013 10:40 pm

I found the problem in the phpSerial code. The section:

if (preg_match("@^COM(\d+):[email protected]", $device, $matches))
{
$device = "/dev/ttyS" . ($matches[1] - 1);
}

The device for my arduino is /dev/ttyACM0. If you change "/dev/ttyS" to "/dev/ttyACM" it works. I guess arduino serial monitor and python must map another device in the form of ttyS to the arduino when they open it.

However, another gotcha is that after opening the device you have to wait before sending a message. I used sleep(5) and it then seemed to work every time. Otherwise it only worked every second time.

Think I might try something a bit more reliable.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10860
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: PHP_serial and Arduino readPort() "funnyness"

Sun Jan 31, 2016 12:21 am

Ripped out/crippled several URL's pointing to a spam like site.
Just because I didn't want to delete this whole thread. Which I will do if another such "ninja" URL will pop up here! :roll:

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 2 guests