2016-08-08 47 views
-1

シリアルポートからVxWorksがインストールされたueipac 600-1G Linuxボックスにデータを読み込んでいます。私は文字列データを読み込むはずです。しかし、文字列で送信すると、テスト用に "これはテストです"というログファイルに "これはテスト-pです"と表示されます。私はコードを変更して16進数値を読み込み、 "74 68 69 73 20 69 73 20 61 20 74 65 73 74 0a"を得ました。シリアルポートの読み込みVxWorks文字列変換の不便

私はさまざまな入力文字列を使って複数のテストを行い、その都度不気味な変更が行われました。私はこの不気味さをフィルタリングする方法を考えています。私は、Cが16進データを0a文字で文字列に変換する方法と関係があると思います。

私のコードの文字列の読み取りを構築するためにforループ

void readandlog_serial(){ 

// read error throwing int 
int n=0; 
int bytes_counter=0; 
int bytes=256; /*num of bytes to read at a time*/ 
int i=0; /* for the for loop*/ 
int bytes_written=0; 
// hold the file descriptor 
int fd=0; 
/* dev name from iosDevShow output */ 
char dev_name[] = "/tyCo/0"; 
/* buffer to receive read */ 
char re[bytes]; 
/* length of string to read */ 
int re_len = bytes; 
/* open the device for reading. */ 
fd = open("/tyCo/0", O_RDWR, 0); 
outputfile=fopen ("LOGFILE.txt","a"); /* open output file*/ 
//check for 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 */ 
for (i=0; i<n;++i){ 
    bytes_written=bytes_written+(fprintf(outputfile," %02x", re[i])); 
} 
//fprintf(outputfile,"%c",'\n'); 
// pull only string data 
//bytes_written=fprintf(outputfile,"%s",re); ***************************** 

fclose(outputfile); 
printf("readandlog executed number of bytes written: %d\n",bytes_written); 
} 

次のようであるがコメントアウトされており、その横にastriksとfprintfのラインが進データの読み出しは、ビルドにコメントアウトされています。

+0

'fprintf(" outputfile "、" ... ")'は何をしますか? *変数* 'outputfile'ではなく、*文字列*へのポインタを渡すと、*未定義の振る舞い*にしかならないでしょう。このコード行について尋ねると –

+0

です// bytes_written = fprintf(出力ファイル、 "%s"、re);私はreが文字列であると思う文字列へのポインタだとは思わない。だから私はそれが出力ファイルに文字列を印刷することを期待します。あなたは、 '\ 0'ヌルターミネーターが置かれるように、私は文字列としてそれを行うべきであることをインスタンス化するときに言っていますか? –

+1

彼は最初の議論が間違っていると言っています。 'fprintf(" outputfile "を' fprintf(outputfile')に変更してください。問題がある行は2行あります。 – user3386109

答えて

0

16進数の出力が正しく、文字列+ 0改行。

あなたは未初期化変数を使用して、あなたがN charのHEX値を印刷beacause再印刷HEXは、正しかったが、fprintfの(OUTPUTFILE、 "%sの" 再);はヌル終了文字列(あなたの文字列+ OA +ガーベージ+ \ 0)を出力します。

変更char re [bytes];char re [bytes] = {};または関数bzero/memset set bufferを0にしてください。

関連する問題