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);
?>