ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I am reading outputs from many sensor through an ADC with the aid of UART but not succesful for more than three weeks. Following is my code cause I think it will be faster to get my problem this way
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/select.h>
#include <stdlib.h> //
#include <sys/types.h> //
#define BAUDRATE B9600 //Baud rate set to 9600 bit/sec
#define SERIALDEVICE "/dev/ttyS0" //Serial port to be used for communication
int main(int argc, char *argv[]){
int sfd, ret, i;
struct termios oldtio, newtio;
char buf[256];
char buf1[256];
fd_set readfds; //file descripto set
int maxfd; //maximum file descriptor used
long disable;
//open serial port to be non-blocking
sfd = open(SERIALDEVICE, O_RDWR );//| O_NOCTTY | O_NONBLOCK);//change later pg(241)
if( sfd < 0 ){
perror(SERIALDEVICE);
exit(1);
}
//current maximum file descriptor
maxfd = sfd + 1;
//struct newtio for new serial port setting
bzero(&newtio,sizeof(newtio));
//struct oldtio for holding the original port setting
bzero(&oldtio,sizeof(oldtio));
//save current options for the port
tcgetattr(sfd,&oldtio);
/*---set the baudrate---*/
//set the input speed
cfsetispeed(&newtio, BAUDRATE);
//set the output speed
cfsetospeed(&newtio, BAUDRATE);
/*---new port setting---*/
//enable receiver and set local mode
newtio.c_cflag |= (CLOCAL | CREAD);
/*Setting Raw Terminal I/O*/
//select 8 data bits, no parity and 1 stop bit
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE; //Mask the character size bits
newtio.c_cflag |= CS8; //select 8 data bits
//Clear ISTRIP to get all eight bits also clear clear parity checking
//clear IEXTEN to turn off extended character processing
//no flow contro clear IXON
//clear BRKINT
newtio.c_iflag &= ~(INLCR | ICRNL | ISTRIP | INPCK | IXON | BRKINT);
//clear OPOST to turn off output processing
newtio.c_oflag &= ~OPOST;
//clear ECHO and set MIN and TIME.
//clear ISIG
newtio.c_lflag &=~(ICANON | ECHO | IEXTEN | ISIG);
for (i = 0; i < NCCS; i++) //NCCS
newtio.c_cc[i] = (cc_t)disable; //what is this?
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush(sfd, TCIFLUSH);
ret = tcsetattr(sfd, TCSANOW, &newtio);
if (ret < 0){
perror("Failure to set new port options\n");
exit(1);
}
/*---loop for reading from the port---*/
FD_ZERO(&readfds);
while(1){
FD_SET(sfd, &readfds);
FD_SET(0, &readfds);
ret = select(maxfd, &readfds, NULL, NULL, NULL);
if (ret < 0){
perror("select failure\n");
exit(1);
}
//check for calling file descriptor
if ( FD_ISSET(sfd, &readfds)){
//handle input from the serial port
bzero(buf1,sizeof(buf1));
ret = read(sfd, buf1, sizeof(buf1));
if(ret < 0){
perror("failure to read serial port\n");
exit(1);
}
printf("Contents from serial input : %s\n", buf1);
}else{
//read input from keyboard
bzero(buf,sizeof(buf));
fgets(buf, sizeof(buf), stdin);
if (strncmp("quit", buf, 4)==0 ){
//prepare to close serial port
printf("now closing serial port\n");
//restore old port setting before closing port
tcsetattr(sfd, TCSANOW, &oldtio);
close(sfd);
return 0;
}else{//may be try writting the serial port
}
}
}
}
I'm not sure that I can help with your problem, but you are more likely to get a useful response if you do two things:
Post your code with the CODE tag around it. Use the go advanced button, select your code with your mouse, and click on the # icon above the editing area (in advanced mode).
Rather than tell us that it didn't work, be more detailed. Where in your code are things getting stuck? Would it be useful to put more detailed error messages in your code, tell us what messages you're getting from that code, and post the changed program which emits those additional error messages?
Last edited by wjevans_7d1@yahoo.co; 07-02-2007 at 09:20 PM.
I am sorry that My circuit keeps on continuously sending and there was something wrong with the contents I was printing. It seems to be working now for one of the sensors and I hope the same will apply to the others. Once more thanks a lot for the very fast responses I have received.
The other issue was with the signal levels. Thanks to Wim Sturkenboom located in South Africa.
It usually will work with TTL levels if you don't forget to invert the signal(s); 0V TTL equals 12V RS232 and 5V equals -12V RS232.
It however is better to use RS232 levels.
And as requested, place your code between code tags; this will preserve indentations and makes it easier to read. The opening tag is [ c o d e ] and the closing tag is [ / c o d e ], both without the spaces.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.