Serial Port problems

This is a discussion on Serial Port problems within the Linux Programming forums, part of the Platform Specific Boards category; Hello, i wrote a small program which sends one byte data over serial port 0 and receives one as answer. ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Serial Port problems

    Hello,
    i wrote a small program which sends one byte data over serial port 0 and receives one as answer.
    As the conencted device reported serial overflow we connected another pc to the serial port. The Problem is:
    The other PC gets the correct byte, but after one or two seconds it receives multiple other bytes.
    The code i used for serial communication is (mostly taken out of the serial programming guide):
    Code:
    int CheckAmp(int b)
    {
    	struct termios newtio, oldtio;
    	char *port="/dev/ttyS0";
    	int portfd;
    	sigset_t block_mask;
    	struct sigaction saio;
    
    	portfd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
    	if (portfd <0)
    	{
    		close(portfd);
    		return 0;
    	}
              
            fcntl(portfd, F_SETOWN, getpid());
            fcntl(portfd, F_SETFL, FASYNC);
    	
    	sigemptyset (&block_mask);
    
    	saio.sa_handler = signal_handler_IO;
    	saio.sa_mask = block_mask;
            saio.sa_flags = 0;
            saio.sa_restorer = NULL;
            sigaction(SIGIO,&saio,NULL);
            
    	
    	newtio.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
    	newtio.c_iflag = IGNPAR;
    	newtio.c_oflag = 0;
    	newtio.c_lflag = 0;
    	newtio.c_cc[VMIN]= 0;
    	newtio.c_cc[VTIME]= 5;
    	tcflush(portfd, TCIFLUSH);
    	tcsetattr(portfd,TCSANOW,&newtio);
    	write(portfd, &b, 1);
    	char rd;
    	int STOP=FALSE;
    	char buff[255];
    	int res;
    	wait_flag=TRUE;
    	while (STOP==FALSE) 
    	{
    		if (wait_flag==FALSE)
    		{
    			printf("Reading...\n");
    			res = read(portfd,buf,1);
    			buf[res]=0;
    			printf("Got %i \n", buf[0]);
    			STOP=TRUE;
    		}
    		
    	}
    	rd = buf[0];
    	checkamp = rd;
    	printf("CheckAmp: 0x%hx \n", rd);
    	tcflush(portfd, TCIFLUSH);
    	close(portfd);
    	return 0;
    }
    void signal_handler_IO (int status)
    {
    	printf("received SIGIO signal.\n");
    	wait_flag = FALSE;
    }
    Thanks for your help

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Well actually it showed that the PC System i used had an broken COM Port.

    On a new machine the code just runs fine. An admin oculd delete this, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplex communication thro serial port
    By Priyachu in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2009, 05:24 AM
  2. Duplex communication thro serial port
    By Priyachu in forum Linux Programming
    Replies: 1
    Last Post: 05-30-2009, 05:03 AM
  3. Serial Port Issues (again!)
    By HalNineThousand in forum Linux Programming
    Replies: 6
    Last Post: 04-09-2008, 09:26 PM
  4. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 07:07 PM
  5. serial port problems
    By megadith in forum C++ Programming
    Replies: 0
    Last Post: 07-08-2002, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21