serialWrite function in Bonescript/javascript

Does anyone know an easier way to send a packet of hex data out the serial port. Here is what works for me

var dio6Hi = [0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xC0, 0xA9, 0x99, 0xFF, 0xFE, 0x02, 0x44, 0x36, 0x05, 0x72 ];

b.serialWrite(port, [
dio6Hi[0] ,dio6Hi[1] ,dio6Hi[2] ,dio6Hi[3] ,dio6Hi[4] ,dio6Hi[5] ,dio6Hi[6] ,dio6Hi[7] ,dio6Hi[8] ,dio6Hi[9],
dio6Hi[10],dio6Hi[11],dio6Hi[12],dio6Hi[13],dio6Hi[14],dio6Hi[15],dio6Hi[16],dio6Hi[17],dio6Hi[18],dio6Hi[19] ]);

This will write all of the data in variable dio6Hi out of the UART in order that I want. But I would like to do this or something like it:

for (var i = 0; i < dio6Hi.length; i++) {
b.serialWrite(port, [dio6Hi[i]] );
}

or even this:

b.serialWrite(port, [dio6Hi[0]] );

b.serialWrite(port, [dio6Hi[1]] );

b.serialWrite(port, [dio6Hi[2]] );

.

.

.

b.serialWrite(port, [dio6Hi[18]] );
b.serialWrite(port, [dio6Hi[19]] );

Either of these do not work they only send out the first byte in dio[0] and then stop. Is there any way with the serialWrite function to index the variable with a “for” or other command in order to simplify sending out an array of numbers?

Does anyone know an easier way to send a packet of hex data out the serial
port. Here is what works for me

    var dio6Hi = [0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00,
0x40, 0xC0, 0xA9, 0x99, 0xFF, 0xFE, 0x02, 0x44, 0x36, 0x05, 0x72 ];

    b.serialWrite(port, [
        dio6Hi[0] ,dio6Hi[1] ,dio6Hi[2] ,dio6Hi[3] ,dio6Hi[4] ,dio6Hi[5]
,dio6Hi[6] ,dio6Hi[7] ,dio6Hi[8] ,dio6Hi[9],

dio6Hi[10],dio6Hi[11],dio6Hi[12],dio6Hi[13],dio6Hi[14],dio6Hi[15],dio6Hi[16],dio6Hi[17],dio6Hi[18],dio6Hi[19]
]);

This will write all of the data in variable dio6Hi out of the UART in
order that I want. But I would like to do this or something like it:

    for (var i = 0; i < dio6Hi.length; i++) {
        b.serialWrite(port, [dio6Hi[i]] );
    }

or even this:

      b.serialWrite(port, [dio6Hi[0]] );
      b.serialWrite(port, [dio6Hi[1]] );
      b.serialWrite(port, [dio6Hi[2]] );

               .
               .
               .
      b.serialWrite(port, [dio6Hi[18]] );
      b.serialWrite(port, [dio6Hi[19]] );

Either of these do not work they only send out the first byte in dio[0] and
then stop. Is there any way with the serialWrite function to index the
variable with a "for" or other command in order to simplify sending out an
array of numbers?

I believe your primary issue here is that the calls to b.serialWrite()
are asynchronous and you aren't waiting for the writes to complete.
I'm not really sure why you'd want to have individual calls to write
each byte, but you could try something like:

var i = 0;
mySerialWrites();
function mySerialWrites() {
  if(i < dio6Hi.length) {
    b.serialWrite(port, [dio6Hi[i]], mySerialWrites);
    i++;
  }
}

I haven't tested this, but I think it might be your issue. Let us know.

I only showed the individual write as an example that also does not work, it only performs the very first serialWrite and ignores the rest.
Your example does work with various delays between each byte, but this will work for me. I might still use the:

b.serialWrite(port, [
dio6Hi[0] ,dio6Hi[1] ,dio6Hi[2] ,dio6Hi[3] ,dio6Hi[4] ,dio6Hi[5] ,dio6Hi[6] ,dio6Hi[7] ,dio6Hi[8] ,dio6Hi[9],
dio6Hi[10],dio6Hi[11],dio6Hi[12],dio6Hi[13],dio6Hi[14],dio6Hi[15],dio6Hi[16],dio6Hi[17],dio6Hi[18],dio6Hi[19] ]);

command since it writes the bytes consecutive with no delay between each byte and so is a little more efficient. At 9600 baud it took just over 20ms to send out the data without the delays between bytes and just under 200ms with the delays.

Thanks for the quick response. I learn something every time you post :slight_smile:

John,

I think the bigger issue is why would you do this ? If you’re going to send the data out the serial port anyhow, why not just send the data as its available ?

How about an explanation as to why you would do this, and how you would use it in an application. I will however say this. What you’re doing is terribly inefficient on memory usage.

In my app, the data is all available at the same time. The data goes to a wireless module that takes it in a specific packetized form. There is a header, control byes address, command bytes, data and a checksum at the end of the packet. If there is too long of a time between bytes in the packet the destinations node may time out. The system will be sending packets to many different nodes in the Wireless Mesh network and all of the nodes will get all of the packets but only act on the ones with it’s specific address. The longer between each byte the longer the timeout in the destination nodes needs to be. If the transmitter sends them all back to back I do not need to mess with the default timeout settings in the destination nodes. Right now I only have a total of 3 nodes for development, but eventually there may be hundreds and then more efficient packet spacing may be important. So basically it is more bandwidth efficient to have no delay between bytes.

Also I may have to deal with system power in the future, and the longer the TX is on the more power it uses. A TX time of 200ms is almost 10 times more power than a TX time of 20ms.

With only 20 bytes of data at a time , with most of the data is constant values, I don’t think memory will be an issue.