Active4 years, 11 months ago
Php-serial not working. The serial_connect.php class is php-serial. Browse other questions tagged php windows serial-port arduino or ask your own question. Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART): Serial.
i want to pass a number from php to arduino. currently, i am able to communicate between php and arduino via serial port. however, when i try to pass a number(int) from php to arduino, the arduino can't identify it. then i try to pass an character(char) it is able to detect. i have tried multiple times and i don't know what is wrong with the code. below is my code.
PHP CODE
here is the arduino code
Anyone can tell me what is wrong? The led doesn't light up at when i send 1. however when i send 'A', the led is up.
Anthony Russell
6,97188 gold badges4848 silver badges8686 bronze badges
JHJLJHJL
2 Answers
Serial.read()
returns an int
, not a char
(as shown here in the documentation). So you're saying,
'but Anthony, it compiles yo... '
True, it does compile, AND EVEN RUNS!
But it's because you are comparing the ASCII value of the
char
'1' to the int
value from Serial.read();
As you can see in the image below, the number 1's decimal value is equal to 49.
So when you are saying
you are esentially saying
Where as you want to say
ASCII TABLE
So the caveat here is it really also depends on how you are sending it also. If you send as one datatype, receive in another and compare it to another. You're going to have spooky results.
All this being said, please, please please.... For the love of god.... Parameterize your insert in php. This is like the poster child code for PHP SQL Injection.
Anthony RussellAnthony Russell6,97188 gold badges4848 silver badges8686 bronze badges
as a quick check can you do this to make sure you're dealing with the right data types (i.e. if you're sending a string or an int and checking for either or.
ok now to try to cut the long story short. i tested this stuff myself and the following code is functional.What I noticed was that the serial monitor in the arduino IDE needed to be open for this to work as expected. When it wasn't open the correct data wasnt being read nor was the PHP script exiting.I'm thinking the problem here is that your arduino environment is trying to communicate to the device over the same serial port that your PHP is trying to talk over too. You'll probably have to investigate how to disable that communication while youre trying to use your PHP script(s)
mytemyte
Not the answer you're looking for? Browse other questions tagged phparduinoserial-communication or ask your own question.
Active6 years, 8 months ago
I have installed xampp in fedora 13.I am trying to communicate with microcontroller through serial port using php serial class.My code is example.php
The php script gets executed but gives the following warnings .
Arduino Software Serial
Warning: Specified serial port is not valid in /opt/lampp/htdocs/xampp/php_serial.class.php on line 147Warning: Unable to set the baud rate : the device is either not set or opened in /opt/lampp/htdocs/xampp/php_serial.class.php on line 241Warning: Unable to set parity : the device is either not set or opened in /opt/lampp/htdocs/xampp/php_serial.class.php on line 295
... I have used the command : chmod 0777 /dev/ttyUSB0 to give permissions . I have also tried to add the apache user 'prudhvi' to the dialout group by using command : $ usermod -a -G dialout prudhvi
But it doesnt work . When I send a command directly from the terminal using the command : echo 1 > /dev/ttyUSB0 it works and '1' is transmitted to the serial port . But using php I get the above warnings .
I have used the '$whoami' to check name of user and added that user 'prudhvi' to the dialout group . It still doesnt work . Please help me guys.
prudhvi09
prudhvi09prudhvi09
4 Answers
I did this once with Debian to control an Arduino board with a PHP script and initially ran into the same problem.
Arduino Serial Example
In Debian, you need to add the Apache user to the dialout group in order to allow it to make serial connection requests. I would assume the same is true for Fedora.
In Debian the command is:
However I believe Fedora names the Apache user as apache instead. I don't have a Fedora machine to test on, but I would assume the command you need to run is:
You will then need to restart your xampp server.
See the following for reference:
Neal
nealio82nealio822,51911 gold badge1212 silver badges1919 bronze badges
Could you post the lines near / related to '/opt/lampp/htdocs/xampp/php_serial.class.php on line 147'?
I suspect that you are trying to set the device incorrectly (as Marc indicated). Either that or the port is already in use from other testing you are conducting at the same time. I'm not sure if the script you are running provides errors specific to ports you are trying to attach to already being in use.
PilipoPilipo
First test a hello world type php script to testyour basic installation.
Then verify the web server / php engine is running as a user which is in a group allowed to access the applicable /dev/ttyWHATEVER device file corresponding to the serial port. It would be surprising if that were true by default - you'll probably have to add it to the 'dialout' or similar group.
Add some fault checking / reporting to your code.
Chris StrattonChris Stratton36.3k55 gold badges6666 silver badges106106 bronze badges
Credit goes to Marc B's comment for causing me to look this up, and he's dead on: http://www.phpclasses.org/browse/file/17926.html
I believe that calling
$serial->deviceSet('/dev/ttyUSB0');
will fix it, but you may have to modify the source of php_serial.class.php
to work on /dev/ttyUSB
instead of /dev/ttyS
.zebediah49zebediah49