very basic/stupid serial question (i hope)

when using serial.write(byte[]) is the whole array sent out ?

for example lets say I have out_byteBuffer[128] and sometimes I'm only storing two of bytes to the buffer and then I go serial.write(out_byteBuffer) i presume it writes the whole array out even if 126 of the bytes are null ?

and if you want to write out a certain fixed number(maxWrite) of bytes from the byte array you would do this ? :

        for(int i = 0; < maxWrite; i++){
        serial.write(out_byteBuffer[i]);
        }
Tagged:

Answers

  • edited June 2017 Answer ✓

    Putting serial.write aside:

    Yes, out_byteBuffer refers to the entire 128 byte array. out_byteBuffer[i] refers to a specific byte from that array.

    If you are trying to shorten your array or take a subset of it, you could loop through and pass i elements of the array, as you describe. However, you could just use the Array Functions listed in the reference such as shorten() or subset() and pass the result once.

    https://processing.org/reference/subset_.html

    Edit: that's a generic Java answer -- noticing this is an Arduino question, there may be a more specific answer.

  • Thanks once again @ jeremydouglass... Guessing a Mod moved my question to Arduino section as it was originally in Questions about code.... I'm not using an Arduino

    I just wanted someone to confirm I was understanding the array situation correctly with regard to writing data out the serial port and that I was not going mad. Have some code working If i use out_byteBuffer but not when using out_byteBuffer[i] and yes I have tried increasing/decreasing the number of bytes written with that method just to be sure.

    the subset() function looks interesting I'll give that a whirl.

  • Consider sharing the specific error message, the line on which it occurs, and if possible a short sketch demonstrating the problem.

    A guess is that you are passing byte to something that needs byte[] -- an array of length 1 isn't the same as a single value.

  • there is no error message unfortunately... the custom hardware part I'm communicating with just does not do what it should when sending a specfic number of bytes from the array rather than the whole array.

    The messages sent all contain a 5 byte "header", which basically consists of : SYNC ( in this case '!') NDB (number of data bytes, byte total except header) EDM (error detection method) SAB (source address) DAB ( destination address) CMD ( command from a pre set list) Then a variable number of data bytes, from 0 up to about 64. Then a Checksum/CRC value on the end if EDM method not set to none.

    The array "out_byteBuffer[]" is initialised at app start up as 128 in size...and differing amounts of relevant data are stored in it dependent on message length..the overall size of the array is always 128.

    you could say well just keep sending the whole array out each time if it works, but I have another problem which I think is related to all these unneeded/extra bytes tying up the MCU & serial which means the hardware is not always sending an ACK or other data back within the time frame that it should.

    still yet to try subset()

  • I will create an ArrayList and feed this dynamic array with the bytes to send. Before you send your data, you should retrieve the array[] version of this object by using the array() method. In this way, you are sending exactly what you want, no more, no less.

    What device are you sending this data to? Should you be checking for this ACK tokens? Are you working with the actual documentation of your device?

    Kf

  • @jeremydouglass - subset() seems to do the job... TBH i'm not entirely sure why that should work any different to the method I was using to write out a fixed number of bytes but it does :) Have a load other things to fix up, but at least that's one of the list and have realised that subset() will come in handy for something else as well.

    @kfrajer - I didn't try your suggestion as subset() worked first time. The device I'm sending to is some custom hardware with a mega 2560 inside,this sits between PC and a large number of slave units on a "network" Yes I'm checking and receiving ACK and other messages back from it. There is no documentation to speak of, though I do have the original C firmware. Cheers, mala

Sign In or Register to comment.