Thread: what is wrong with the code ?

  1. #1
    Registered User
    Join Date
    Aug 2008

    what is wrong with the code ?

    Hi I have written the followinng serial port program. It compiles without errors. During execution the program does not recieve all the characters that I send from another computer thru minicom. Most of the characters are lost only a few are recieved. The baud rates on both sides are same. Is there something missing in the code ? Please help.

    #include <termios.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/signal.h>
    #include <sys/types.h>
    #include <sys/select.h>
    #include <unistd.h>
    #include <string.h>
    #define DEVICE "/dev/ttyS0"
    int main(void)
    	int fd;
    	struct termios oldtio, newtio;
    	//open the serial communication device.
    	fd = open(DEVICE, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
    	if(fd < 0) {
    		printf("Failed to open device.\n");
    		return -1;
    	memset(&newtio, 0, sizeof(newtio));
    	/* Store the old configuration. This will be restored when we close the library. */
    	tcgetattr(fd, &oldtio);
    	newtio.c_cflag = B115200 /* Baudrate specified by the user */ \
    			 | CS8		/* character size mask */ \
    			 | CLOCAL	/* Ignore modem control lines */ \
    			 | CREAD	/* Enable reciever */ \
    			 | CSTOPB;
    	printf("ISIG : %x\n", ISIG);
    	printf("ICANON : %x\n", ICANON);
    	newtio.c_iflag = IGNPAR | ICRNL ;
    	newtio.c_oflag = 0;
    	newtio.c_lflag = ISIG;
    	printf("newtio.c_lflag: %x\n", newtio.c_lflag);
    	/* Set the new configuration */
    	tcflush(fd, TCIFLUSH);
    	if(tcsetattr(fd, TCSANOW, &(newtio)) < 0 ) {
    		printf("Failed to set new configuration. Possibly an invalid configuration.\n");
    		return -2;
    		char a=0;
    		char b[11];
    		while(1) {
    			fd_set fds;
    			FD_SET(fd, &fds);
    			select(fd+1, &fds, NULL, NULL, NULL);
    			printf("Select returned.\n");
    			if(read( fd, &(b[0]), sizeof(char) )< 0) {
    //				perror("reading error.");
    //			printf("%c",b[0]);
    	return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I didn't see any mention of flow control.

    Why not try reading up to 10 chars at once, then using the result of read() to tell you how many characters you got?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2008
    I am not using any flow control but I am sending only one character every one second not more than that.

  4. #4
    Registered User
    Join Date
    Aug 2008

    any other process listening serial port

    Make sure no other program listens that port.
    That is possible you may have forgot minicom open in one of your consoles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM