Go Down

Topic: Serial Read in Objective C under OSX? (Read 8262 times) previous topic - next topic


Greetings everyone!
I'm quite new to the Arduino community, and have only last night gotten one of the sample programs up and running.

I'm in the "Feasibility and Research" part of my project planning, so I have some general questions that google just isn't answering for me. I'm hoping someone around here can.

A tiny bit of background: I'm a recent CS graduate, with little electronic experience, and no real OSX programming experience. As a personal project, I want to read simple analog signals using the Arduino, and pass those signals to my computer as integer values. I would then write a program in Objective C, and be able to read those integer values in and display them.

But before I dive in, what I can not find an answer to, is if it's possible to read in a "serial port" in Objective C/OSX from the Arduino board, while they are connected via USB. The Arduino IDE appears to do just this sort of thing, however since I can find no examples or talk of it on the internet, I'm a bit nervous that what I want to do is impossible, or near enough to it that no one bothers.

Can anyone answer this for me?
Examples/ links to how-tos would be awesome, but unnecessary at this point. Knowing it's possible is what I really care about :-)

Thanks everyone!


Are you interested in ObjC or Cocoa?

The best bet might be using (or integrating) the serproxy utility, and connect to the network socket that gets created.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Here is some C code that works under OS X.  It should be compilable
within a Cocoa project.
Code: [Select]
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

/* change this to your USB port Device Name*/
#define PORT "/dev/tty.usbserial-A6007nT7"  

void USBSerialGetline(char *buffer, int bufsize);
int  USBSerialInit();

** this is just a sample c Main program.
** It reads lines and prints them
int main(int argc, char *argv[])
  int fd;
  char buffer[256];

  fd = USBSerialInit();
  /* loop reading lines from the USB Serial Port */  
  while (1)
     getline(fd,buffer,sizeof buffer);
     ** here's where to process the line that was read
     ** As an example, this code is just printing it out

int USBSerialInit()
  int fd;
  int i, j;
  struct termios options;
  /* open the USB Serial Port */
  fd = open(PORT, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
     perror("open_port: Unable to open serial port - ");
     return -1;
  else fcntl(fd, F_SETFL, 0);
  /* set the port to 9600 Baud, 8 data bits, etc. */
  tcgetattr(fd, &options);
  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  options.c_cflag |= CS8;    /* Select 8 data bits */
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  tcsetattr(fd, TCSANOW, &options);
  return fd;

** this reads an entire line of text, up to a Newline
** and discards any Carriage Returns
** The resulting line has the Newline stripped and
** is null-terminated
void USBSerialGetLine(int fd,char *buffer,int bufsize)
  char *bufptr;
  int nbytes;
  char inchar;

  bufptr = buffer;
  while ((nbytes = read(fd, &inchar, 1)) > 0)
     if (inchar == '\r') continue;
     if (inchar == '\n') break;
     *bufptr = inchar;
  *bufptr = '\0';

Go Up