Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: hardcorehead87 on Sep 09, 2014, 10:15 pm

Title: Sending byte arrays
Post by: hardcorehead87 on Sep 09, 2014, 10:15 pm
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 ;-)
Title: Re: Sending byte arrays
Post by: CrossRoads on Sep 09, 2014, 10:29 pm
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();
Title: Re: Sending byte arrays
Post by: CrossRoads on Sep 09, 2014, 10:31 pm
This is better

if (Serial.available()>=5) // 6 bytes available?
Title: Re: Sending byte arrays
Post by: AWOL on Sep 09, 2014, 10:53 pm
Even better would be >= 6.
Title: Re: Sending byte arrays
Post by: hardcorehead87 on Sep 09, 2014, 11:16 pm
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..
Title: Re: Sending byte arrays
Post by: CrossRoads on Sep 10, 2014, 05:21 am
Add a 7th sync byte, such as 0xAA or some other combination you are not likely to receive. Or a pair, like 0xAA 0x55
Title: Re: Sending byte arrays
Post by: hardcorehead87 on Sep 10, 2014, 08:39 am
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?
Title: Re: Sending byte arrays
Post by: AWOL on Sep 10, 2014, 08:53 am
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.
Title: Re: Sending byte arrays
Post by: Robin2 on Sep 10, 2014, 10:50 am
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
Title: Re: Sending byte arrays
Post by: CrossRoads on Sep 11, 2014, 05:58 am
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();
   }
}

Title: Re: Sending byte arrays
Post by: AWOL on Sep 11, 2014, 09:12 am
Code: [Select]
  while (Serial.available() <5){
  // hang out for 6 bytes
  }
// got 6, now read them

{ polite cough }
Title: Re: Sending byte arrays
Post by: CrossRoads on Sep 12, 2014, 02:51 am
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!
Title: Re: Sending byte arrays
Post by: Robin2 on Sep 12, 2014, 10:13 am

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


Firefox ???

...R
Title: Re: Sending byte arrays
Post by: PaulS on Sep 12, 2014, 05:05 pm
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.