(linux, nginx)
Im using ajax go get info from /dev/ttyUSB0 serial port
@fgets() is reading port until result !BUT! if the AJAX receives a 504 error from NGINX then the stream does not terminate properly via fclose func. Can i set time out for fgets? i tried stream_set_timeout() but that command doing something i dont need (i think so because after 9 seconds stream doesnt terminate and when i start ajax next time i can see the effect of this issue).
and if i get 504 error 5 times in a row and when i try to scan card next time i need like 4-5 scans to finally get a result. Looks like fgets is moving pointer to the next line each time i call him. Can you help me to resolve this problems?
(if i try to set ajax timeout to 0 or 999999 i keep getting 504 after some time)
code from ajax_port_check.php
$device = '/dev/ttyUSB0';
$open_mode = 'r';
$handle = @fopen($device, $open_mode);
if ($handle) {
stream_set_timeout($handle, 9);
$data = @fgets($handle);
@fclose($handle);
$response = [
'code' => 200,
'status' => 'ok',
'message' => 'Card successfully read',
'content' => $data,
'stream' => $handle . '',
];
} else {
$response = [
'code' => 403,
'status' => 'error',
'message' => 'File not fount / Access denied',
];
}
exit(json_encode($response));
main page with ajax
function scan_card(seconds) {
var timer = startTimeout(seconds);
$.ajax({
url: "ajax_port_check.php",
type: "post",
success: function (response) {
response = JSON.parse(response);
console.log(response['content']);
stopTimeout(timer);
},
error: function () {
console.log('error');
stopTimeout(timer);
},
timeout: seconds * 1000
});
}