Reputation: 1313
I'm having this weird output when I read hex characters in PHP, wherein PHP fetches its data from Arduino serial..
Here's the output..
ÿ ÿ^A ÿ^A
I wrote a user space application which I use to send data to the Arduino.
Here's the sample code of my userspace application:
unsigned char data[2][7] = {{0x01,0x01,0xFF,0x3F,0x00,0x3F,0x08},
{0xFF,0x01,0x02,0xFF,0x00,0x2F,0x10}};
fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY);
if(fd<0)
exit(1);
if (strcmp(argv[1], "go up")==0) {
write(fd,data[0],8); // What I used to send data to my Arduino..
}
Here's the sample PHP code of what I'm using to fetch the data from the Arduino:
$sCon = $sConnect->SerialConnect("/dev/ttyS0", 9600);
shell_execute("test 'go up'"); //Test is the name of my user space application
usleep(100000)
$data .= fread($sCon, 25); // Get the data from the Arduino..
$sConnect->SerialClose($sCon);
Now when I run it, the data displayed on the page is messed up: ÿ ÿ^A ÿ^A
. I need to retrieve this data: 0x01,0x01,0xFF,0x3F,0x00,0x3F,0x08, which will be displayed on my website..
When I change this
write(fd,data[0],8); // Prints `ÿ ÿ^A ÿ^A`
to this
write(fd,"010101FF3F003F08",15); // Prints 010101FF3F003F08 in PHP
then I can retrieve the data without any problem...
Why is it displaying like ÿ ÿ^A ÿ^A
?
Upvotes: 2
Views: 624
Reputation: 719
When you do write(fd, data[0], 8)
, your are sending a stream of binary data, that is characters which are coded 0x01
, 0x01
, etc., not an ASCII representation of your data.
Depending on what is your goals, you should send them as an ASCII string. To process them in PHP, this is the easiest way (but it will lose half of your bandwidth with 4 useful bits per 8 bits transmitted).
for(int p=0; p<8; p++) {
fprintf(fd, "%02x", data[0][p]);
}
instead
write(fd, data[0], 8);
Upvotes: 2