Go Down

Topic: Sending byte arrays (Read 11607 times) previous topic - next topic

hardcorehead87

Hi everybody,

I'm trying to send byte arrays from a C# appication to the arduino.
Using very big delays I can get it to work but the point is to constantly send data over
If I remove the delays I'm getting an acceptable result.

I'm sending the data in chuncks of 6 bytes but this will need to change in other situations.

This is the c# code I use to send the data to the arduino;
Code: [Select]
static void Main(string[] args)
{
    var port = new SerialPort("COM4", 115200);
    port.DataReceived += PortOnDataReceived;
    port.Open();

    while (true)
    {
        port.Write(new byte[] { 0, 0, 0, 0, 0, 126 }, 0, 6);
        port.Write(new byte[] { 4, 0, 0, 0, 0, 190 }, 0, 6);
        port.Write(new byte[] { 8, 0, 0, 0, 0, 222 }, 0, 6);
        port.Write(new byte[] { 12, 0, 0, 0, 0, 238 }, 0, 6);
        port.Write(new byte[] { 16, 0, 0, 0, 0, 246 }, 0, 6);

        Thread.Sleep(1000);

        port.Write(new byte[] { 0, 0, 255, 255, 255, 127 }, 0, 6);
        port.Write(new byte[] { 4, 0, 255, 255, 255, 191 }, 0, 6);
        port.Write(new byte[] { 8, 0, 255, 255, 255, 223 }, 0, 6);
        port.Write(new byte[] { 12, 0, 255, 255, 255, 239 }, 0, 6);
        port.Write(new byte[] { 16, 0, 255, 255, 255, 247 }, 0, 6);

        Thread.Sleep(1000);
    }
}
private static void PortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    var port = sender as SerialPort;
    if (port == null) return;

    var readData = port.ReadLine();
    Console.WriteLine("Received: " + readData);
}


This is the code on the arduino side that just echo's the first byte.
Code: [Select]

byte rawData[6];
void setup()
{
Serial.begin(115200);
}

void loop()
{
while (Serial.available())
{
Serial.readBytes(rawData, 6);
Serial.println(rawData[0], DEC);
}
}


At the moment I'm still trying to get everything to work.
The arduino should use the data elsewhere instead of echoing it.
Eventually the arduino will push the bytes out of it's SPI port.
Or the bytes will be stored in array on the arduino and a timer will push it to the SPI.
Using the delays of 1 second between burts I receive the correct first bytes.
When using anything less then 1 second I don't get the same results.

I've been trying to get things to play nice with each other without succes.
Hopefully somebody will be able to point me in the right direction ;-)

CrossRoads

You're checking that at least 1 is received, then attempting to read 6.

while (Serial.available()>=5) // 6 bytes available?
   {
rawData[0] = Serial.read(); // read & save 6 bytes
rawData[1] = Serial.read();
rawData[2] = Serial.read();
rawData[3] = Serial.read();
rawData[4] = Serial.read();
rawData[5] = Serial.read();
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

This is better

if (Serial.available()>=5) // 6 bytes available?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AWOL

Even better would be >= 6.

hardcorehead87

That makes sence.
I thought readBytes would wait for the data to reach 6 bytes unless it reaches the timeout.
I'm still having problems but this would explain why with the long delay it was working.

I think that when I open the serial connection in C# there are some bytes sent over the line.
This would completely mess up my order as I expect chucks of 6 bytes.
With the big delay these initial few bytes are read and then the timeout hits.
The next read I do is into my array again but starting at index 0.

So what I'm getting at is how would I sync up the sending and receiving of these chucks?
If I send 1 2 3 4 5 6 then I don't want to receive 5 6 1 2 3 4..

CrossRoads

Add a 7th sync byte, such as 0xAA or some other combination you are not likely to receive. Or a pair, like 0xAA 0x55
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

hardcorehead87

When I check if the 7th (and 8th) byte are the correct sync bytes I know that the arduino is in sync or not.
But  if that's not the case, how would I get them to sync up? I have tried using Serial.find() without luck.
Maybe I was doing something wrong but what would be the adviced way to do this?

AWOL

Quote
But  if that's not the case, how would I get them to sync up

Keep reading (and dumping the result) until they are in sync.

Robin2

If you want to send a lot of data in fixed length chunks I suggest you get the Arduino to "request" the chunks - perhaps by sending an 'M' (for more :) ). That way synchonization is ensured.

Sending small chunks of data will not permit a large overall throughput. You could probably send chunks of 36 bytes as quickly as chunks of 6 bytes.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

CrossRoads

Here's one way:
Code: [Select]

// assume one sync byte
if (Serial.available()>0){  //
  if (Serial.read() == syncbyte){ // got a sync byte?
  while (Serial.available() <5){
  // hang out for 6 bytes
  }
// got 6, now read them
   array[0] = Serial.read();
   array[1] = Serial.read();
   array[2] = Serial.read();
   array[3] = Serial.read();
   array[4] = Serial.read();
   array[5] = Serial.read();
   }
}

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

AWOL

Code: [Select]
  while (Serial.available() <5){
  // hang out for 6 bytes
  }
// got 6, now read them

{ polite cough }

CrossRoads

What, I'm making it up on fly, trying  to post  before I get bumped into the "Warming:someone else posted" window, which locks up IE8 on me, and then I have to close down IE altogether and start all over, and if lucky I'll have remembered to copy so I can paste & post quick. If you see an error, just say so! I'm open for corrections. Modify locks me up too. Major PITA!
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Robin2


which locks up IE8 on me, and then I have to close down IE altogether and start all over,


Firefox ???

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
If you see an error, just say so!

So.

The comment does not agree with the first line of code in AWOL's snippet. The following code expects the comment to be right.

Go Up