LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
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?

Notices


Reply
  Search this Thread
Old 02-21-2008, 05:14 AM   #1
vineel
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 - ");
}
else{
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);
numberOfBytes=write(fd,message,lengthOfMessage);
printf("number of bytes sent = %d\n", numberOfBytes);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int numberBytesRcd;
int angle,i;
char buff[5];
numberBytesRcd=read(fd,buff,5);
printf("number of bytes received = %d\n",numberBytesRcd);
printf("%s\n",buff);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
bye
 
Old 02-23-2008, 11:51 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
Code:
    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;
    newtio.c_cc[VMIN]=1;
    newtio.c_cc[VTIME]=0;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

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

    /* restore old port settings 
    */
    tcsetattr(fd,TCSANOW,&oldtio);
Use the port in non-blocking mode, so you can perform other things while waiting for input.
Code:
    /* 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.
 
  


Reply


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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 08:06 PM.

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