目前共有2篇帖子。 内容转换:不转换▼
 
点击 回复
381 1
【實例】PHP通過串口通信讀取從單片機發來的DS18B20溫度數據
一派护法 十九级
1楼 发表于:2015-6-30 10:12
單片機端程序:https://zh.arslanbar.net/post.php?t=22504
PHP程序:
<?php
define("DSNEG", 4);
define("DSERR", 6);
function BIT($n) {
    return 1 << $n;
}
function decodeTemperature($flags, $TN, $TD) {
    if ($flags & BIT(DSERR)) {
        $value = "Error";
    } else {
        $value = $TN * 1.00 + $TD * 0.01;
        if ($flags & BIT(DSNEG)) {
            $value = -$value;
        }
    }
    return $value;
}

function getTemperature($fp) {
    fwrite($fp, "\x83");
    $flag = fread($fp, 1);
    $data = fread($fp, 3);
    $temperature = decodeTemperature(ord($data{0}), ord($data{1}), ord($data{2}));
    return $temperature;
}
?>
<!doctype html>
<html>
<head>
<title>Temperatures</title>
</head>

<body>
<?php
$fp = fopen("/dev/ttyUSB0", "a+");
for ($i = 1; $i <= 30; $i++) {
    $temperature = getTemperature($fp);
    echo "<b>Temperature $i:</b> $temperature&deg;C<br>\n";
    usleep(1E5);
}
fclose($fp);
?>
</body>
</html>
一派护法 十九级
2楼 发表于:2015-6-30 10:13
輸出結果:
Temperature 1: 27.43°C
Temperature 2: 27.43°C
Temperature 3: 27.43°C
Temperature 4: 27.43°C
Temperature 5: 27.43°C
Temperature 6: 27.43°C
Temperature 7: 27.37°C
Temperature 8: 27.37°C
Temperature 9: 27.37°C
Temperature 10: 27.37°C
Temperature 11: 27.37°C
Temperature 12: 27.37°C
Temperature 13: 27.37°C
Temperature 14: 27.37°C
Temperature 15: 27.37°C
Temperature 16: 27.37°C
Temperature 17: 27.37°C
Temperature 18: 27.37°C
Temperature 19: 27.37°C
Temperature 20: 27.37°C
Temperature 21: 27.37°C
Temperature 22: 27.37°C
Temperature 23: 27.37°C
Temperature 24: 27.37°C
Temperature 25: 27.43°C
Temperature 26: 27.43°C
Temperature 27: 27.43°C
Temperature 28: 27.43°C
Temperature 29: 27.43°C
Temperature 30: 27.43°C

回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:381 回复数:1
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2015-6-30 10:13
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。