doupin8555 2015-10-07 22:53
浏览 79

fwrite错误 - 快速写入串行输出的问题

Firstly, I apologize if this issue has been posted before, I couldn't find the answer anywhere.

I'm writing a one page website that uses JavaScript, Ajax and PHP to take key presses and send them out via serial port. I'm testing it on EasyPHP and WAMP, I'm yet to test on a Linux server.

My problem is that when keys are pressed rapidly I get the error below. Warning: fopen(com4): failed to open stream: Permission denied in \keypresstest.php on line 7

If I press keys slowly, around 500ms intervals, it all works fine.

Furthermore, following the error above, PHP does not set my $fp variable, so it also returns errors: Warning: fwrite() expects parameter 1 to be resource, boolean given in \keypresstest.php on line 9 Warning: fclose() expects parameter 1 to be resource, boolean given in \keypresstest.php on line 10

I think that there is slow 'release' of the serial port resource which it's defining as 'Resource id #3' and if I could some how speed this up or spawn another instance when needed i might be able to resolve the issue. I hope someone reading this might have some advice.

My code is below, I've removed some components to keep this code more concise

HTML File

<script> <!-- call php script here -->
function sendKey(str) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = document.getElementById("txtHint").innerHTML + xmlhttp.responseText;
}
}
xmlhttp.open("GET", "keypresstest.php?q=" + str, true);
xmlhttp.send();
}
</script>

<script> // page js key listener script
document.addEventListener('keydown', function(event) { // things happen as keys are pressed
if (!arrayContainsCheck(keysHeld, event.keyCode)) { // check if the key is already being held, this is so keydown events don't continue to propagate whilst a key is held
if(event.keyCode == 38 || event.keyCode == 87) { // up/w keys, send output to php script "f"
sendKey("f");
}     keysHeld.push(event.keyCode);
}
});

document.addEventListener('keyup', function(event) {
document.getElementById("lightSwitch").style.backgroundColor = "blue";
if(event.keyCode == 38 || event.keyCode == 87) { // up/w keys, send output to php script "g"
document.getElementById("upArrow").style.border = "1px solid red";
sendKey("g");
}
keyRelease(keysHeld, event.keyCode);
});
</script>

PHP File

<?php
$q = $_REQUEST["q"]; // get the q parameter from URL
// Output q parameter to com4 serial port
echo "q is $q <br />";
echo "defining fp... <br />";
$fp = fopen("com4", "w");
echo "fp defined as " . $fp . " .<br />";
fwrite($fp, $q);
fclose($fp);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料