• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Serial port access from C / Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Serial port access from C / Objective C


  • Subject: Re: Serial port access from C / Objective C
  • From: Tom Hohensee <email@hidden>
  • Date: Tue, 16 Jun 2009 06:20:51 -0500

If you want to access the serial port via Objective-C classes try AMSerialPort. It is a collection of classes to access serial ports. I have used it extensively for my serial programs and it works very well.

Tom

On Jun 15, 2009, at 5:10 PM, Vansickle, Greg wrote:



I'm trying to communicate with a Photo Research Spectral photometer via USB. The device responds to simple ascii commands via a terminal program.

I have no problem accessing this via the screen command from Terminal. I need to get to the same capability from a Cocoa project.

From terminal I'd enter:
screen /dev/cu.usbmodem1a21

Once the screen utility starts, I simply type PHOTO (no carriage return) and the device would respond "REMOTE MODE" (no carriage return) and the spectro photometer displays "REMOTE" on it's panel.


Here's the C code I'm trying to make work. It's Apples SerialPortSample, stripped to it's minimum (so I understand what's important and what's fluff ...)

#include <fcntl.h>
#include <termios.h>
#include <string.h>

int main(void)
{
   int    fileDescriptor;
   const char *bsdPath = "/dev/cu.usbmodem1a21";
   const char *cmdString = "PHOTO";
   int    handshake, numBytes;
   int result;
   char buffer[256];
   char *bufPtr;

fileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY | O_NONBLOCK); // returns 3
if (fileDescriptor == -1) { return -1;}


result = ioctl(fileDescriptor, TIOCEXCL);
if (result == -1) { return -1; } // returns 0


result = ioctl(fileDescriptor, TIOCMGET, &handshake); // returns 0, handshake is 0x26
numBytes = write(fileDescriptor, cmdString, strlen(cmdString)); // returns 5


   bufPtr = buffer;
   numBytes = read(fileDescriptor, bufPtr, 15); // returns -1

   close(fileDescriptor);

   return 0;
}

If I remove the read and write, the USB device opens and closes properly, i.e. I can access the device successfully using the screen command again.

With the write / read in place, although the write appears to work (numbytes returns 5), the photometer does not respond (I get no "REMOTE" indication on the instrument panel. The subsequent read fails (returns -1).

I've never programmed at the device level like this, so please confirm my assumption - I do not need to set the baud rate, parity and all that as the device appears to default to a sane state. (Screen utility doesn't set it, I assume the default values are set in the device it self.)

FWIW, the complete SerialPortSample code, complete with setting up the terminal, baud, parity, ... and changing the command strings, behaved the same way. I stripped all this out in case the problem was setting this stuff up.

Any pointers appreciated.

Greg
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Serial port access from C / Objective C (From: "Vansickle, Greg" <email@hidden>)

  • Prev by Date: Re: Float Seconds?
  • Next by Date: Passing events from original receiver / redirecting flow of events
  • Previous by thread: Re: Serial port access from C / Objective C
  • Next by thread: CFAttributedStringGetMutableString returning null value
  • Index(es):
    • Date
    • Thread