+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    11

    problem reading from serial port

     
    Hi, I'm not sure if this should go in the Linux/Unix forum, so forgive me if it's in the wrong place, but here goes anyway.

    I'm sending a character array over the serial port from one PC to another using c on linux. The following code is used to transmit the data. It's in an infinite loop so it will keep writing to the port every 2 seconds.

    Code:
    char message[]={0x1,'C','L','A','1','0','0','2','6',0x2,'\r','\n','3','0','a','b','\0'};
    
    int p=0;
    
    	printf("transmitting data\n");
    	
    	while(p==0)
    	{
    		error= write(fWSfd, message,17);
    		error= tcsetattr(fWSfd, TCSAFLUSH, &newtio) <0;
    		sleep(2);
    	}
    This data is then read by a program on the other PC.

    Code:
    int y=0;
    char data[50];
    
    while(y==0)
    		{
    	        error= read( fWSfd, data, 50*sizeof(char) );
    		while (error<1)
    		          {
    		           error= read( fWSfd, data, 50*sizeof(char) );
    		           data[error]=0;
    		          }
    		
    		printf("Read the following data: %s\n", data);
    		error = tcsetattr(fWSfd,TCSAFLUSH,&newtio)<0 ;     
    		}
    It loops continuosly, the inner loop being used to wait for input to arrive at the port.

    My problem is with the output, which turns out like this:


    Read the following data: #CLA10026# (# = 0x1 and 0x2 characters )
    30
    Read the following data: ab


    It seems to only read part of the message and then stop, and then loop around again and grab the rest of the data. I can't figure out why it's doing this. I have it set to raw data, so the carraige returns and line feeds shouldn't affect it. If you're wondering, the reason they're in there in the first place is because those exact characters must be transmitted.

    I can post all the serial port settings too if it would be helpful, I just thought maybe I overlooked something obvious.

    I hope somebody can help me with this. Any ideas would be appreciated.

    Thanks

  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    Never mind, I got it working. I think it was reading before it received all the characters. A small delay before it reads sorts it out.

    Thanks anyway.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. serial port
    By Mahlon Gillard in forum Technical Support
    Replies: 1
    Last Post: January 20th, 2010, 06:25 AM
  2. Serial port error
    By videobruce in forum Technical Support
    Replies: 0
    Last Post: August 21st, 2005, 10:58 AM
  3. Printer port to serial port ???
    By pickel in forum Applications and Operating Systems
    Replies: 5
    Last Post: June 9th, 2005, 09:43 PM
  4. Whats the serial 1 port for?
    By mjolnir1134 in forum General Tech Discussion
    Replies: 2
    Last Post: February 24th, 2005, 06:44 PM
  5. Serial port
    By seanmark2 in forum General Tech Discussion
    Replies: 9
    Last Post: June 24th, 2003, 10:56 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews