Arduino Mega serial not sending equally
Moderators: adafruit_support_bill, adafruit

Arduino Mega serial not sending equally

by drummerboyx on Sat Dec 01, 2012 11:13 pm

I wrote this very simple code.

Code: Select all | TOGGLE FULL SIZE
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
 
        if (Serial.available() > 0) { 
  Serial.println("<DMX>255,23,1,4,6</DMX>");
  delay(2000);     
  Serial.println("<DMX>9,44,88,238,90</DMX>");
  delay(2000);       
        }
}



I also wrote a mac application in Objective-c to read incoming serial data from the Arduino through USB. Problem is, it seems like the Arduino is sending the line of serial (<DMX>255,23,1,4,6</DMX>) in random chunks. For example, it's sending, "<DM" , "X>255," , ",23,1,4," , "6</DM" , "X>". This is obviously happening really fast, but I need it to send all at the same time for my mac app to read it properly. It's receiving the chunks separately and I can't put them together easily.

Any ideas on how to fix this? Is it even possible? If not, any work arounds on either side (Arduino, mac)?

Thanks,
Elijah

P.S. - if anyone knows Obj-c here is the didReceiveData method on the mac side. I was trying to see if I could handle the chunks, but I'm having some trouble. That's why I thought I'd ask here to see if I can get the root of the problem on the Arduino side.

Code: Select all | TOGGLE FULL SIZE
- (void) didReceiveData:(NSString *)data {
   
    // status=0 means we are still looking for start tag
    if(status == 0) {
        // add new data to last examined chunk (if any)
        [tmpBuffer appendString:data];
       
        // try to locate the open tag inside the tmpBuffer
        NSRange range = [tmpBuffer rangeOfString:@"<DMX>" options:NSCaseInsensitiveSearch];
       
        // if found, store the portion after the start tag into buffer
        if(range.location != NSNotFound) {
            range.length = [tmpBuffer length] - range.location + 5; // 5 is length of start tag...
            [buffer setString:[tmpBuffer substringWithRange:range]];
            status = 1; // set status to 1 so we know recording started
        } else {
            // store last examined chunk
            [tmpBuffer setString:data];
        }
    } else {
        [buffer appendString:data];
        NSRange range = [buffer rangeOfString:@"</DMX>" options:NSCaseInsensitiveSearch];
        if(range.location != NSNotFound) {
            range.length = [buffer length] - range.location;
                       
            NSRunAlertPanel(@"", [NSString stringWithString: buffer], @"", @"", @"");
           
            [buffer deleteCharactersInRange:range];
            status = 0;
        }
    }
}
drummerboyx
 
Posts: 80
Joined: Wed Feb 10, 2010 7:36 am

Re: Arduino Mega serial not sending equally

by adafruit_support_bill on Sun Dec 02, 2012 8:57 am

Serial data is sent serially. That is: it goes out one byte at a time. The 'random chunks' you see on the other side depend on how fast the receiving side can pull the bytes out of the input buffer.

It is up to the receiving side to figure out when all the bytes have been received. This is pretty simple if your messages are of a fixed length. Other simple techniques include using a termination character such as and ASCII "ETX" (0x04). More sophisticated protocols involve parsing the input data string and recognizing when the message is complete. In the example you post, it would be when you receive the "</DMX>".

A very crude, but simple and (sometimes) effective hack is to have the receiving side wait some time after receiving the first character to make sure the rest of the message has been received and buffered.
User avatar
adafruit_support_bill
 
Posts: 30822
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino Mega serial not sending equally

by inevu on Sun Dec 02, 2012 12:35 pm

I don't know if I'm right or wrong, but I think it has something to do with the Apple computer. Something similar is happening to me but with an Arduino UNO: viewtopic.php?f=25&t=34827. If I use just one of the connections at a time, the data is received perfectly. When I use both connections at the same time (ie. send data through one serial connection and try to receive it through the other) then the data I am receiving is all messed up. I believe it's receiving all the data but in random chunks. I have used different baud rates and nothing fixes the problem. I am using a Macbook Pro Retina with Mountain Lion. I have tried this issue on two other Apple computers with the exact same results.
inevu
 
Posts: 39
Joined: Mon Feb 06, 2012 5:22 pm

Re: Arduino Mega serial not sending equally

by adafruit_support_bill on Sun Dec 02, 2012 1:55 pm

@inevu - It has nothing to do with the type of computer. As described in my previous post, the bytes are sent out one at a time and received one at a time. If the receiving end is pulling them out of the receive buffer before they all arrive, you will get 'random chunks'.
User avatar
adafruit_support_bill
 
Posts: 30822
Joined: Sat Feb 07, 2009 10:11 am