LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Linux Forums > Linux - Hardware
User Name
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?


LinkBack Search this Thread
Old 02-21-2008, 05:14 AM   #1
LQ Newbie
Registered: Feb 2008
Posts: 1

Rep: Reputation: 0
Facing problems in reading from and writing to serial port.

Hi all,
I am trying to communicate with a PIC micro controller through serial port using c program for controlling dc motors. Initially, to get familiarize my self with the hardware programming i am sending a character to pic and receiving a string of 3 characters. I programmed the PIC for that, i tested it with hyperterminal. It was working. When i am accessing it through my program it was not working. I am using following code.
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 - ");
printf("Port opened successfully...\n");
printf("File descriptor is %d\n",fd);
fcntl(fd, F_SETFL, FNDELAY);

/*Get the current options for the port...*/
tcgetattr(fd, &options);
/*Set the baud rates to 19200...*/
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
/*Enable the receiver and set local mode...*/
options.c_cflag |= (CLOCAL | CREAD);
/*Set the new options for the port...*/

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

tcsetattr(fd, TCSANOW, &options);
int numberOfBytes;
int lengthOfMessage=strlen(message);
printf("number of bytes sent = %d\n", numberOfBytes);
int numberBytesRcd;
int angle,i;
char buff[5];
printf("number of bytes received = %d\n",numberBytesRcd);
always read is returning -1.

I am stuck here. I am not getting where is the problem actually.
Any body plz help me.

Thanks alot.
Old 02-23-2008, 11:51 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,390
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
What is the value of errno? You use perror to print error related to opening the port; what about on reading the port? Is the remote device ready to send? Is the state of all of the modem control lines appropriate for the local UART to receive?

Have you read the Serial Programming HOWTO?

--- rod.
Old 02-27-2008, 02:18 PM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,390
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Some suggestions
Get the current attributes, as you do now, and save them in a struct termios; Use a fully re-initialized different struct termios to set the attributes of the serial port. This make sure you don't get unwanted effects from the original settings. Also, it allows you to restopre the origianl settings when you're done. Here's a fragment from some code I either wrote or stole; I forget which.
    tcgetattr(fd,&oldtio); /* save current port settings */
    /* set new port settings for canonical input processing 
    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;
    newtio.c_lflag = ECHO;
    tcflush(fd, TCIFLUSH);

    /* do serial port IO here.....   */

    /* restore old port settings 
Use the port in non-blocking mode, so you can perform other things while waiting for input.
    /* open the device to be non-blocking (read will return immediately) 
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fd <0) {
        perror("/dev/ttyS0"); exit(-1); 
You never did describe what you mean by 'not working'; this is vague and came mean many different things.

(It is helpful to post your source code and other formatted text in [code] tags)

--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading from serial port IC009562 Linux - Software 2 11-08-2007 11:25 PM
writing to serial port sea waves Programming 6 05-08-2007 11:59 AM
help reading data from a serial port skydemon Linux - General 1 08-10-2006 08:48 AM
reading and writing to a serial device (modem) Xanadu Linux - Laptop and Netbook 0 01-04-2005 10:05 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration