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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-21-2008, 06:14 AM
|
#1
|
LQ Newbie
Registered: Feb 2008
Posts: 1
Rep:
|
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
|
|
|
02-23-2008, 12:51 PM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
02-27-2008, 03:18 PM
|
#3
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
All times are GMT -5. The time now is 10:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|