go_away

Author Topic: Serial port interface in C  (Read 568 times)

0 Members and 1 Guest are viewing this topic.

Offline macbookTopic starter

  • Jr. Member
  • **
  • Posts: 45
  • Helpful? 0
Serial port interface in C
« on: September 20, 2011, 04:04:34 AM »
Hi,

I am using a FriendlyARM Mini2440 board for my project and I need to serially interface a GPS receiver with it. I am writing a C code for the same. I had a doubt regarding some of the UNIX system calls.
I am using the following functions - open() (http://linux.about.com/od/commands/l/blcmdl2_open.htm), read(), write() etc. I have a doubt in the open() call.

Code: [Select]
int fd;
fd = open("ttySAC2", O_RDWR | O_NOCTTY | O_NDELAY); //O_RDWR = Read Write

It returns 3 as the value of fd. I have not been able to find out the meaning of this value. I know if its -1, it's an error but apart from that, what does any other value signify?

Thanks.

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Serial port interface in C
« Reply #1 on: September 20, 2011, 05:22:34 AM »
Code: [Select]
int fd;
fd = open("ttySAC2", O_RDWR | O_NOCTTY | O_NDELAY); //O_RDWR = Read Write

It returns 3 as the value of fd. I have not been able to find out the meaning of this value. I know if its -1, it's an error but apart from that, what does any other value signify?

The man page for open(2) is pretty clear. If it doesn't return -1, the value it does return is the file descriptor that should be used for subsequent calls to read() write() ioctl() and close().

Man pages in general have a section called "Return Value" that tell you what to expect back from a function. The page you link to also has this section. Sometimes you have to look for it, or use the search function of your browser to search the page. But it should always tell you what the return value is, or point you to another source of information.

Joe


Joe
« Last Edit: September 20, 2011, 05:25:31 AM by joe61 »

 


Get Your Ad Here