harzsr
Posts: 2
Joined: Thu Sep 06, 2018 2:08 am

Send Hex Value to Serial from PHP

Thu Sep 06, 2018 2:18 am

Hi,

I am using PHP to execute some Output commands through Terminal to Serial Output
Its basically a button click and it executes this line
Terminal

Code: Select all

echo -e "\x02\x03\x08\x01\x09" > /dev/ttyAMA0
PHP Code

Code: Select all

echo exec("echo "\x02\x03\x08\x01\x09" > /dev/ttyAMA0");
Output

Code: Select all

02 03 08 01 09 0D 0A
The problem comes when I try to send 0A in the command
Both Terminal and PHP when executed

Terminal

Code: Select all

echo -e "\x02\x03\x09\x01\x0A" > /dev/ttyAMA0
PHP Code

Code: Select all

echo exec("echo "\x02\x03\x09\x01\x0A" > /dev/ttyAMA0");
Output

Code: Select all

02 03 09 01 0D 0A 0D 0A
I am not sure, why when I execute 0A it adds a 0D in between 01 and 0A. I know 0A is Line Feed and 0D is Carriage Return.

Is there any way to output only 0A without that 0D

Thank you

User avatar
topguy
Posts: 5500
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Send Hex Value to Serial from PHP

Sun Sep 09, 2018 11:52 am

You dont have the "-e" echo option in the PHP code.
I use "-en" option for these things, then it should not add any line-ensds and you can control that yourself.

If you learn have to work with bytearrays directly in PHP you could just open ttyACM0 directly and write to it.
https://blog-en.openalfa.com/how-to-wor ... ata-in-php ( a bit old guide but maybe still helpful )

Return to “Other programming languages”