Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    problem reading from serial port

    Hi, I hope somebody can help me with this. I'm sending a character array over the serial port from one PC to another using c. 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.

    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");
    		error= write(fWSfd, message,17);
    		error= tcsetattr(fWSfd, TCSAFLUSH, &newtio) <0;
    This data is then read by a program on the other PC.

    int y=0;
    char data[50];
    	        error= read( fWSfd, data, 50*sizeof(char) );
    		while (error<1)
    		           error= read( fWSfd, data, 50*sizeof(char) );
    		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 )
    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.

    Any ideas on this would be appreciated.


  2. #2
    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.

  3. #3
    Have you tried debugging at all to see if the write/read is done in one step or many? Does the first write all 17 in one sweep or does it take multiple runs? Same for the read?

    How about changing the data being sent to see if it has an effect on how it read/writes?

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts