read from serial port

I am attempting to read ascii formatted data from an external device via
the serial port using the following code:

int
read_port(int fd)
{

   int numRead=0;   	
   char buffer[255];
   char letter;
   int i;
         
   memset(buffer, 0, sizeof(buffer));
   numRead = read(fd, buffer, sizeof(buffer));

   printf("fd..%i numRead..%i\n",fd,numRead);
   
   /* 
    * print the data read from dev/ser1 to screen
    */
   for (i=0; i<=254; i++)
   {
     letter = buffer[i];
     printf(" %c",letter);
   }
   printf("\n");
     
   return(0); 
 }    

I am expecting data of the form
IMU xxxx,xxxx,xxxx,xxxx,xxxx,xxxx
GPS xxx.xxx,xx.xxx,xxxx.xxx
MUX xxxx,xxxx,xxxx,xxxx,xxxx

Where the x’s represent various numerical values, however all i get out
is:
fd…3 numRead…255
à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à Ã
etc.

I can obtain this data correctly using Hyper Terminal in windows, so i believe the device is correctly configured. Does anyone know why this might be happening?

How did you setup/open your com port?
Are you using the right baudrate, parity etc.?

here is the code i used to open the port:

int
open_port(void)
{
  int fd; /* File descriptor for the port */

  fd = open("/dev/ser1", O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)
  {
   /*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ser1 - ");
  }
  else
  	printf ("port opened\n");
  		
  return (fd);

As you can see, i have not set baud, parity etc, which is prob causing my problems. What is the best way to set these, i am new to C and cannot find out how to do it.

For example in sh:
stty +raw +ignbrk +ignpar baud=115200 +ihflow +ohflow -isflow -osflow </dev/ser1

Look at tcgetattr/tcsetattr functions in library reference (and overal tc*() set of functions).

Thanks for the help.

Does anyone know of a book which covers this sort of thing? I have looked at many c texts but cannot find one which does.

easysw.com/~mike/serial/

Hi,
I tried to write a test-application to gain access on device-files.
It should be used to check whether driver works correctly for the device.
Yet, I just try to read out some sample-devices.
For example I tried to read out /dev/ser1 or also /dev/kbd.
I have an ps2ser keyboard and a ps2ser mouse plugged in, but not loaded any drivers or stacks (USB…) additionally.
I can open the files but after read() errno tells me “Ressource temporarily unavailable”!!!
Are these actually the corrct device-files, for the serial mouse/ keyboard???
Or do I have to load any drivers first??? Which and How???
And then there was something about baud-rate and parity. What settings do I have to use???

Here is my source-code:


/* Dies gibt eine Testapplikation, die auf Gerätedateien zugreifen kann */

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


int main( int argc, char *argv[] )
{
	int fd; 			/* File descriptor for the port */

#if 1
	int numRead=0;		/* #gelesener Bytes */
	char buffer[255]; 	 /* Buffer */
	char letter;		    /* Buchstabe */
	int i;				/* Index */
	int errvalue;		   /* ErrorCode */
#endif

	/* open port */
	fd = open("/dev/kbd", O_RDONLY | O_NOCTTY | O_NDELAY);
	if (fd == -1)
	{
		/* Could not open the port. */
		perror("open_port: Unable to open /dev/kbd - ");
	}
	else
	{
		/* Statusausgabe */
		printf("/dev/kbd opened!!\n");
			
#if 1
		/* weiterverarbeiten */
                /* reserviere Pufferspeicher */
		memset(buffer, 0, sizeof(buffer));		
                /* lese Datei, schreibe gelesene Daten in den Puffer und gib aus wieviele Bytes gelesen wurden */
		numRead = read(fd, buffer, sizeof(buffer));		
		
    	errvalue = errno;
	    printf( "The error generated was %d\n", errvalue );
	    printf( "That means: %s\n", strerror( errvalue ) );

                /* gib aus, wieviel gelesen wurde */
		printf("fd..%i numRead..%i\n",fd,numRead);		

		/* print the data read from dev/kbd to screen */
		for (i=0; i<=254; i++)
		{
			letter = buffer[i];
			printf("%c",letter);
		}
		printf("\n");
#endif
	
		/* und wieder schließen */
		close(fd);
	
		/* Statusausgabe */
		printf("/dev/kbd closed!!!\n");
	}
	
	return 0;
}