Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Read / Write to Serial Port in Linux

Status
Not open for further replies.

twintriode

Technical User
Jan 6, 2005
21
AU
Hi All -

Have been trying to get this code running. It is the first example in on this page:
Code:
#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 */

/*
 * 'open_port()' - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error.
 */

int
open_port(void)
{
 int fd; /* File descriptor for the port */

 fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
 if (fd == -1)
{
/*
 * Could not open the port.
 */

perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
        cntl(fd, F_SETFL, 0);
        return (fd);
}


I keep getting the error:

Code:
# gcc -o 88 88.c
/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/../../../crt1.o: In function `_start':
init.c:(.text+0x18): undefined reference to `main'
/tmp/ccgPNr2v.o: In function `open_port':
88.c:(.text+0x48): undefined reference to `cntl'
collect2: ld returned 1 exit status

Can anybody tell me what I am doing wrong here??

Thanks
 
I believe you want :

fcntl(fd, F_SETFL, 0);

rather than

cntl(fd, F_SETFL, 0);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi - thanks for the reply. I tried what you said above and it got rid of half the error. However I am now still getting:

Code:
/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/../../../crt1.o: In function `_start':
init.c:(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

I dont understand as there is no main() defined in the script above so how am I getting an "undefined reference to `main'"?

Thanks
 
For programmes that are to be invoked directly, you must specify an entry point into the programme - which is the "main" method.

For example :

Code:
int main(int argc, char* argv[]) {
   return open_port();
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top