I'm having this weird output when I read hex characters in PHP, wherein PHP fetches its data from Arduino serial..
当我在PHP中读取十六进制字符时,我得到了这个奇怪的输出,其中PHP从Arduino系列中获取数据。
Here's the output..
这是输出. .
ÿ ÿ^A ÿ^A
I wrote a user space application which I use to send data to the Arduino.
我编写了一个用户空间应用程序,用于向Arduino发送数据。
Here's the sample code of my userspace application:
下面是我的userspace应用程序的示例代码:
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:
下面是我用来从Arduino获取数据的示例PHP代码:
$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..
当我运行它,在页面上显示的数据是一团糟:y y y ^ ^。我需要检索这些数据:0x01、0x01、0xFF、0x3F、0x00、0x3F、0x08,它们将显示在我的网站上。
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
?
为什么显示像y y y ^ ^ ?
1 个解决方案
#1
2
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.
当您编写(fd、data[0]、8)时,您正在发送一个二进制数据流,即编码为0x01、0x01等的字符,而不是数据的ASCII表示。
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).
根据您的目标是什么,您应该将它们作为ASCII字符串发送。要在PHP中处理它们,这是最简单的方法(但是如果每8位传输4位,就会损失一半带宽)。
for(int p=0; p<8; p++) {
fprintf(fd, "%02x", data[0][p]);
}
instead
而不是
write(fd, data[0], 8);
#1
2
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.
当您编写(fd、data[0]、8)时,您正在发送一个二进制数据流,即编码为0x01、0x01等的字符,而不是数据的ASCII表示。
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).
根据您的目标是什么,您应该将它们作为ASCII字符串发送。要在PHP中处理它们,这是最简单的方法(但是如果每8位传输4位,就会损失一半带宽)。
for(int p=0; p<8; p++) {
fprintf(fd, "%02x", data[0][p]);
}
instead
而不是
write(fd, data[0], 8);