0

I'm building an app for Google Chrome that gets some bytes from device via COM (usb) port.

The code simple:

// connects the device
function connect(port_name) {
    chrome.serial.connect(port_name, {
        bitrate: 937500,
        dataBits: "eight",
        stopBits: "one",
        parityBit: "no"
    }, function(connectionInfo) {
        if (typeof (connectionInfo) !== 'undefined') {
            if (typeof (connectionInfo.connectionId) !== 'undefined' && connectionInfo.connectionId < 1) {
                console.log('Connection error #1');
            } else {
                sessionStorage.setItem('connection', connectionInfo.connectionId);
            }
        }
    })
}
// sends bytes to device
function send_bytes(bytes) {
    var bytes_to_send = new Uint8Array(bytes);
    var connection = Number(sessionStorage.getItem('connection'));
    chrome.serial.send(connection, (bytes_to_send).buffer, function(sent_data) {});
}
// recieves the data
chrome.serial.onReceive.addListener(function(data_recieved) {
    var arr = new Uint8Array(data_recieved.data);
    var final_hex = [];
    for (byte in arr) {
        final_hex.push(arr[byte].toString(16));
    }
    console.log('====== HEX ======');
    console.log(final_hex);
});

If I run next code sending data several times:

connect('COM5',15000); // we connected!
send_bytes([0x05, 0xAD, 0x1E, 0x00, 0x00, 0x00, 0xB6]); // bytes away! ( runs N times)

Usually I receive correct HEX sequence of 10 HEX, split in different events like this:

["FF", "FF"] ["0", "8B", "0"] ["1", "38", "38", "0", "FF"]
["FF"] ["FF"] ["0", "8B"] ["0", "1", "38", "38", "0"] ["FF"]

But sometimes sending the same command I get only part of the response, for example 9 HEX:

["FF"] ["FF"] ["0", "8B"] ["0", "1", "38", "38", "0"]

Then when I sending the same command I receive also the missing part like this 11 HEX:

["FF","FF", "FF"] ["0", "8B", "0"] ["1", "38", "38", "0", "FF"]

Testing the behaviour outside of this program the hardware always behaves as expected, returning the 10 values ​​each time.

Using the API look like the chrome.serial.onReceive.addListener is not always triggered and some HEX values ​​remain in the buffer. I believe I should always receive 10 values from the API..

(I need to fix the code or maybe there is the possibility to ask for the remaining values ​​of the buffer or to recall the event)

Francesco
  • 396
  • 3
  • 7
  • 17
  • Try to call [chrome.serial.flush(..)](https://developer.chrome.com/apps/serial#method-flush) – Titus Aug 17 '20 at 17:18
  • @Titus chrome.serial.flush(..) take out the HEX value when I have 11+ HEX values, but don't solve when I have 9- HEX values and I need the missing values – Francesco Aug 17 '20 at 17:26
  • See if there is any equivalent function (`flush`) that you can call in the other program. – Titus Aug 17 '20 at 17:31
  • Try changing [options](https://developer.chrome.com/apps/serial#type-ConnectionOptions) in connect() e.g. bufferSize, ctsFlowControl. – wOxxOm Aug 17 '20 at 18:09

0 Answers0