c - Serial Port Reading VxWorks String conversion gibberish -


i reading in data serial port ueipac 600-1g linux box vxworks installed. supposed read in string data. when send in string, testing used "this test" log file reads "this test¤ -p". altered code read hex values , got " 74 68 69 73 20 69 73 20 61 20 74 65 73 74 0a" when plugged hex converter reads "this test".

i have done multiple test different input strings , gibberish changes each time. wondering how filter out gibberish think has how c converting hex data string, 0a character.

my code follows

void readandlog_serial(){  // read error throwing int int n=0; int bytes_counter=0; int bytes=256; /*num of bytes read @ time*/ int i=0; /* for loop*/ int bytes_written=0;  // hold file descriptor int fd=0; /* dev name iosdevshow output */  char dev_name[] = "/tyco/0";  /* buffer receive read */  char re[bytes];  /* length of string read */  int re_len = bytes;  /* open device reading. */  fd = open( "/tyco/0", o_rdwr, 0); outputfile=fopen ("logfile.txt","a"); /* open output file*/ //check open error if(fd<0)     fprintf("outputfile","%s","\nerror opening file\n"); //while(n = read( fd, re, re_len)>0)     //bytes_counter=bytes_counter+n;  n = read( fd, re, re_len); /* read */ if(n<0)     fprintf("outputfile","%s","\nerror reading file\n"); close( fd ); /* close */  (i=0; i<n;++i){     bytes_written=bytes_written+( fprintf(outputfile," %02x", re[i]) ); } //fprintf(outputfile,"%c",'\n'); // pull string data //bytes_written=fprintf(outputfile,"%s",re);   *****************************  fclose(outputfile); printf("readandlog executed number of bytes written: %d\n",bytes_written); } 

the loop commented out string reading builds, , fprintf line astriks next commented out hex data read builds.

hex output correct, string + 0a newline.

you using uninitialised variable re, printing hex correct, beacause print hex value n char, fprintf(outputfile,"%s",re); print null terminated string (your string + oa + garbage + \0).

change char re[bytes]; char re[bytes] = {}; or use function bzero/memset set buffer 0.


Comments