E_bme
E_bme

Reputation: 89

Communicate from Serial port -> Chrome app -> webpage. Webpage initiates.

like the question asks, I'm trying to communicate with a serial device through a chrome app, via a webpage. The objective is to turn on a switch with a button on a webpage, and make sure the switch is in fact on (serial response).
So far I have been able to turn on the switch fine, however I need to validate that it is in fact enabled.

My chrome app code:

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        if (request.request == 'info') {
            sendResponse(DEVICE_INFO);
        } else if (request.request == 'turn_off') {
            device_array.forEach(function(device){
                if (device.id == request.device_id){
                    device.send('f');
                }
            });

//INSTEAD OF "OK" I NEED IT TO ASK THE DEVICE TO CONFIRM OFF/ON STATUS
            sendResponse('OK'); 

        } else if (request.request == 'turn_on') {
            device_array.forEach(function(device){
                if (device.id == request.device_id){
                    device.send('n');
                }
            });
            sendResponse('OK');
        }
        return true;
    });

If I send a "status" query to the device, it's going to take a few milliseconds for it to respond "off" or "on" to the serial buffer. Any ideas on how to go about this? Thanks in advance.

Upvotes: 1

Views: 101

Answers (1)

E_bme
E_bme

Reputation: 89

Ultimately I got it to work by requesting the device status within the listener block of code. Below are the modifications, crude but it gets the job done. DEVICE_STATUS is being updated by an onReceive listener on the serial connection.

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        if (request.request == 'info') {
            sendResponse(DEVICE_INFO);
        } else if (request.request == 'turn_off') {
            device_array.forEach(function(device){
                if (device.id == request.device_id){
                    device.send('f');
                }
            });             
            var time_loop = 0;
            connection.send('s'); // s is for STATUS
            var timer = setInterval(device_is_off, 200);

            function device_is_off(){
                if (time_loop > 5){ //Serial Communication Timeout at 1sec 
                    sendResponse('ERROR ' + DEVICE_STATUS);
                    clearInterval(timer);
                    return;
                }
                if (DEVICE_STATUS == 0){
                    sendResponse('OK');
                    clearInterval(timer);
                    return
                }
                else time_loop++;
            }

        } else if (request.request == 'turn_on') {
            device_array.forEach(function(device){
                if (device.id == request.device_id){
                    device.send('n');
                }
            });             
            var time_loop = 0;
            connection.send('s'); // s is for STATUS
            var timer = setInterval(device_is_on, 200);

            function device_is_on(){
                if (time_loop > 5){
                    sendResponse('ERROR ' + DEVICE_STATUS);
                    clearInterval(timer);
                    return;
                }
                if (DEVICE_STATUS == 1){
                    sendResponse('OK');
                    clearInterval(timer);
                    return
                }
                else time_loop++;
            }
        }
        return true;
    });

Upvotes: 1

Related Questions