ORSSerialPort icon indicating copy to clipboard operation
ORSSerialPort copied to clipboard

Not working with Sparkfun Pro Micro

Open pierdr opened this issue 6 years ago • 11 comments

Hi, with a Sparkfun Pro Micro that ships with a Atmel MEGA 32u4 I'm not able to receive any incoming data.

The code (arduino) I'm testing is the following:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println(millis());
  delay(2000);
}

It works with an Arduino seamlessly. With the Pro Micro this line is never reached.

Actually there is a case in which the line is reached and it happens when a second serial application (cool term in my case) opens the serial communication with the pro micro; when open with an other program the data is received by both my application and cool term.

It seems to me that the connection is never really open (even if the open delegate method is triggered) and the data is not received. Any ideas on what the problem might be?

pierdr avatar Nov 02 '17 22:11 pierdr

UPDATE: I managed to run the Sparkfun Pro Micro (ATMEGA 32u4) but using the low level IOKit/ioctl API from the Apple documentation (here) It will be great to make this happen in ORSSerialPort!

_serialFileDescriptor = open( [address UTF8String], O_RDWR | O_NOCTTY | O_NONBLOCK ); // block non-root users from using this port ioctl(_serialFileDescriptor, TIOCEXCL);

// clear the O_NONBLOCK flag, so that read() will
//   block and wait for data.
fcntl(_serialFileDescriptor, F_SETFL, 0);

// grab the options for the serial port
tcgetattr(_serialFileDescriptor, &options);

// setting raw-mode allows the use of tcsetattr() and ioctl()
cfmakeraw(&options);

// specify any arbitrary baud rate
ioctl(_serialFileDescriptor, IOSSIOSPEED, &baudRate);

// start a thread to read the data coming in
[self
 performSelectorInBackground:
 @selector(incomingTextUpdateThread:)
 withObject:
 [NSThread currentThread]];

pierdr avatar Nov 07 '17 23:11 pierdr

I am having the exact same problem with the Adafruit Itsy bitsy.

It seems to be able to send data over the serial connection, but it never receives any responses.
The delegate never gets triggered. It works perfectly with an Arduino Uno board.

justinisdumb avatar Mar 06 '18 19:03 justinisdumb

I've just ordered an Itsy Bitsy so I can look into this. It probably won't be here until next week sometime, so if you come up with something in the meantime, please let me know.

armadsen avatar Mar 06 '18 19:03 armadsen

Thanks for such a quick response!
Im not sure where to dig into it to look for the issue, but let me know if there is anything you need from me. I appreciate your help, this framework is insanely helpful.

justinisdumb avatar Mar 08 '18 22:03 justinisdumb

Hey armadsen, I just wanted to check in and see if you got a chance to look at the Itsy Bitsy issue. Thanks

justinisdumb avatar Mar 27 '18 22:03 justinisdumb

Sorry, I did get one, just haven't had a chance to play with it yet due to being slammed with other stuff. I haven't forgotten!

armadsen avatar Apr 23 '18 16:04 armadsen

Hey armadsen, just checking in. Any way I can help in debugging? Id love to switch all my systems over to the Itsy Bitsy. Thanks!

justinisdumb avatar Jul 18 '18 19:07 justinisdumb

Not sure if you ever got a chance to look into this, but I have found that when I start the demo command line app, I can't get a connection or response from the Itsy Bitsy. If I then open up the "Serial Monitor" from the Arduino app, the connection comes to life and the demo app starts seeing the board. Not sure if that might help.

justinisdumb avatar Oct 02 '18 23:10 justinisdumb

@pierdr

UPDATE: I managed to run the Sparkfun Pro Micro (ATMEGA 32u4) but using the low level IOKit/ioctl API from the Apple documentation (here) It will be great to make this happen in ORSSerialPort!

_serialFileDescriptor = open( [address UTF8String], O_RDWR | O_NOCTTY | O_NONBLOCK ); // block non-root users from using this port ioctl(_serialFileDescriptor, TIOCEXCL);

// clear the O_NONBLOCK flag, so that read() will
//   block and wait for data.
fcntl(_serialFileDescriptor, F_SETFL, 0);

// grab the options for the serial port
tcgetattr(_serialFileDescriptor, &options);

// setting raw-mode allows the use of tcsetattr() and ioctl()
cfmakeraw(&options);

// specify any arbitrary baud rate
ioctl(_serialFileDescriptor, IOSSIOSPEED, &baudRate);

// start a thread to read the data coming in
[self
 performSelectorInBackground:
 @selector(incomingTextUpdateThread:)
 withObject:
 [NSThread currentThread]];

The only real differences I see are ioctl(_serialFileDescriptor, TIOCEXCL); and that you didn't set O_EXLOCK on the open() call. I changed ORSSerialPort.m to reflect that, but it did not help in my case.

@armadsen It sees the library lost its ability to communicate with Arduino boards as well in recent macOS versions. The fact that it works after the port is opened in another application makes me think there is something missing in the serial port or GCD initialization in ORSSerialPort. I looked into the source but did not find any clues.

Dirk- avatar Mar 04 '22 14:03 Dirk-

Thanks for the followup report, @Dirk-. I've left myself a note to try digging into this this weekend.

armadsen avatar Mar 04 '22 18:03 armadsen

I found a solution:

Somehow, RTS and DTR have to be set to TRUE for a successful connection with an Arduino on recent macOS versions.

In the openOrClosePort method of ORSSerialPortDemo, for example, you could do this:

- (IBAction)openOrClosePort:(id)sender
{
    self.serialPort.isOpen ? [self.serialPort close] : [self.serialPort open];
    // For Arduino:
    if (self.serialPort.isOpen) {
        self.serialPort.RTS = TRUE;
        self.serialPort.DTR = TRUE;
    }
}

It is not sufficient to set usesDTRDSRFlowControl or usesRTSCTSFlowControl to TRUE. No need to change these flags.

Dirk- avatar Mar 17 '22 18:03 Dirk-