0

I have written an G code interpreter/control app for a cnc machine in objective C. Everything runs fine for the first 20 - 30 secs but after that the whole thing stops for an other 20 sec and resumes super slow. I made a video so you can see for your self: video (about a minute). As far as I can tell it doesn't skip steps or something like that it just goes really slow.

In my X-code console I can see that code is interpreted at the normal speed (using a NSLog every time a byte is written).

I use the IOkit from the Arduino Cocoa reference to communicate. I have tried a lot of different Baudrates and sometimes that will prolong the time it keeps working correctly but eventually it always slows down.

I some thing after this line needs to clean up the serial buffer or something

// send a byte to the serial port
void writeByte(char val) {
        write(serialFileDescriptor, [[NSString stringWithFormat:@"%c" , val]  cStringUsingEncoding:NSUTF8StringEncoding], 1);

}

Update: I am developing the app on my 17"MBP running OS X 10.9, I tried this on a other 13"MBP running 10.9.1 same thing happens but when I use yet an other 13"MBP running 10.6.8 it works fine!

Any ideas on what is happening here?

Mubanga
  • 43
  • 7
  • 1
    You need to narrow the problem down. You have two distinct systems interacting. Is the slow down occurring because of a problem in the Arduino hardware, the Mac app, or some combination of bytes? Are packets being delivered to the Arduino at the rate you expect? – Andrew Madsen Feb 05 '14 at 18:50
  • "Combination of bytes" should be "combination of both" in that last comment. – Andrew Madsen Feb 05 '14 at 19:40
  • @AndrewMadsen I did some testing and I don't think it is the Arduino, I connected a LED made it light up `if (Serial.available() > 0)` and go off when else. It begins being light up continuously, after that it only lights up every second or so. Am I correct to assume it has something to do with my Mac app? – Mubanga Feb 06 '14 at 13:36

2 Answers2

1

Probably you are writing faster than baudrate, but you will slow down only when output buffere will be full, because you need to wait to write. This can be solved or worked around in many ways

Lesto
  • 2,260
  • 2
  • 19
  • 26
  • I tried slowing down my write rate by putting `[NSThread sleepForTimeInterval:t1];` after the writeByte statement. I tried a lot of different `t1`'s (even a full second) but eventually they all ended up slowing down. Am I correct to assume that the problem isn't caused by writing faster than the baudrate? Or is this not the correct way to check? – Mubanga Feb 06 '14 at 14:03
  • This is not correct way. Baudrate/10 are your byte/s. Then do some math :) – Lesto Feb 06 '14 at 14:06
  • Thanks, but I don't think that is the problem. I want to sent less than 500 bytes/s and I have tried a different baudrates all higher than 5000 up to 1000000 (I believe Arduino's maximum baudrate). – Mubanga Feb 06 '14 at 14:22
1

As it turned out there was a rogue Serial.Write() some were in the Arduino code (I placed there to find an other bug, and just forgot about it) The statement slowly filled the serial buffer.

Mubanga
  • 43
  • 7