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.