Passing a byte array (or any array) and other questions

Hi,

I have a few questions on the programming language syntax. Normally I’m advanced in C# .Net, and while the language is similar, i’m unable to do something simple like pre-defining an array and passing it to a function. for example:

I have a function that will send a byte array through the serial port to my control application (written in C# .Net), the function is as followed:

void SerialSendData(byte data[])
{
  for ( int i = 0; i < sizeof(data); i++ )
    Serial.write(data[i]);
}

In C#, if I want to send some data through, I can do something like this:
SerialSendData( new byte {255,255,255,255,1} );
or…
byte data = new byte {255,255,255,255,1};
SerialSendData( data );

It doesn’t work here, obviously because it’s a wrong syntax to use. I tried several things but I’m just unable to make it work.

I also have another problem in regards to serial communications.
In my understanding Serial.flush() should clear the buffer. I’m having a problem with that.

So I’ve got a function that reads all the available bytes stored in the buffer and returns it as a byte array:

byte* SerialReadAll()
{
  int c = Serial.available();
  if (c==0) return 0;

  byte data[c];

  for (int i = 0; i < c; i++)
    data[i] = Serial.read();

  Serial.flush();

  return data;
}

In my loop procedure a loop without delaying, to get the fastest processing of data send by my control application on the other side of the COM. If I leave no delay, i’m getting chunks of data that are not cleared from the previous loop, or chipped of chunks of bytes; when I leave a delay of 10 milliseconds, the phenomenon seems only occurring now and then:

void loop()
{
  byte data[] = SerialReadAll();
  // Do stuff with it
}
void loop()
{
  byte data[] = SerialReadAll();
  // Do stuff with it
  delay(10);
}

Anyone can explain this? is it the way i send bytes through from my controller app?
(code on controller app in C#)

public void Send(SerialPort port, Datapacket datapacket)
{
    byte[] data = datapacket.Data;
    try
    {
        port.Write(data, 0, data.Length);  // data, offset, count
    }
    catch(Exception ex)
    {
        console.Print("(Exception) While trying to send data on port " + port.PortName + ": " + ex.Message);
        console.Print("*** DUMP ***");
        console.print( data.GetDump() );
    }
}


private void DoSomething()
{
    byte throttle = 127;

    // Datapacket is a class that allows easy reading and writing into a byte array
    Datapacket dp = new Datapacket();
    

    dp.Write( (int)-1 );
    dp.Write( (byte) ArduinoCommand.Throttle );   // ArduinoCommand.Throttle is int 20 (converted to a byte)
    dp.Write( throttle );

    SerialPort_Send( port, dp ); // "port" is defined, initialized and connected somewhere else
    
}

One final question on data conversion.
I’ve noticed when i send a 255 (byte) through the com port, my control app receives a 3F for some reason (a 63 decimal). the baud rates are both set to 14400 so that not the problem. If I send something lower than 255, I receive it as sent. I though it might be a faulty board or something, but my second arduino board has it too.
** Edit: Actually scratch this problem… The problem seems fixed after changing both bauds to 9600.

Anyway, hoping for some answers on my problems. Thanks.

i'm unable to do something simple like pre-defining an array and passing it to a function. for example:

You need to revise your post. You say that you can't do some things. You don't say whether you can't do them because the compiler doesn't allow them, or whether the syntax is valid, but the execution of the code does not have the results you expect. In the latter case, you need to define what the expected results are and what the actual results are.

In your first example, the argument is actually a pointer. The sizeof() macro determines the size of the pointer, which is, obviously, not the same as the size of the array pointed to be the pointer.

There is no way to determine the size of the array pointed to be a pointer. That is why things like strings, which are char arrays, must be NULL terminated. It is the position of the NULL terminator that functions like strlen() locate, to determine the size of the array. It is also why passing a non-NULL-terminated array of chars to a function that expects a string is not a good idea, regardless of the number of people that claim it works.

You can't safely return the address of a local variable. As soon as the function returns, the variable goes out of scope and the contents are liable to be overwritten by any other operation that causes the stack to extend over it. It's better to pass in the address of a buffer which the received data is to be placed in.

  byte data[c];
  return data;

So I've got a function that reads all the available bytes stored in the buffer and returns it as a byte array:

Aside from the issue that PeterH pointed out, there is a flaw in your logic.

When the while loop ends, you have read all the serial data in the buffer. If you are using 0023 or earlier, where flush() throws away random amounts of unread data, there is no sense calling it when you have just finished making sure that the buffer contains no unread data.

If you are using 1.0 or later, where flush() blocks until the outgoing serial buffer is empty, I'm sure you can see how that is a useless activity to perform.

In either case, the flush() call, as in so much code where it is used, is useless. Get rid of it.