Question

I am trying to control some stepper motors using an app I am writing in Objective-C and my Arduino board. I use popen to send a byte (the letter a) to my Arduino and count the steps taken (Xc):

-(IBAction)PlusX:(id)sender{
    popen("echo a > /dev/tty.usbmodem621", "r");
    Xc = Xc +1;
    _lXc.stringValue = @(Xc);
    [NSThread sleepForTimeInterval:0.16f];
}

My Arduino reads this in the void loop and makes a step.

if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte == 'a'){
MotorX->step(1, FORWARD, SINGLE);
}
}

This al works pretty much as expected. Except after byte/step/action 144 the Objective-C app keeps counting the steps correctly, however they don't appear in Arduino's serial monitor and the motor stops making steps. Is there anybody who knows why this keeps happening?

Thanks

Was it helpful?

Solution

you are opening many file (in linux everything is a file) but never closing it. Then you are creating "garbage" open file, because you can't use that file anymore, but it is there. And any OS give you a limit of maximum file open (file descriptor limit); in your case about 144.

when you "kill" your application, that descriptor get released, so you can use them again.

Also, because you don't check the descriptor is valid, you didn't debugged that after the 144 open it was giving error.

Solution (that does NOT check for error) is

pclose(popen("echo a > /dev/tty.usbmodem621", 'r')); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top