簡體   English   中英

使用VxWorks C將二進制數據發送到串行端口

[英]Sending Binary Data to serial port using VxWorks C

您好,我正在嘗試對UEIPAC 600-1G多維數據集進行編程,以從溫度傳感器接收數據。 為了激活傳感器,我必須向其發送二進制數據。 我正在嘗試將字符集設置為十六進制值,但是出了點問題。 通過將數據轉儲到文本文件中,我進行了測試,並得到空格和一些亂碼。 如何通過串行端口發送二進制數據?

struct timespec tim, tim2;
unsigned char access_code[] ="       ";
int bytes=256; /*num of bytes to read at a time*/
char re[bytes]; 
int re_len = 0;
int n_access = 0;   /* store number of bytes written from access code */
int n_read =0;
int i=0;
int baud=0;
int bytes_written=0;
/* hold the file descriptor */ 
int fd = 0;  
access_code[0]=0x01; /*fill access code with correct hex data */
access_code[1]=0x00;
access_code[2]=0x00;
access_code[3]=0x00;
access_code[4]=0x08;
access_code[5]=0x02;
access_code[6]=0x03;
re_len = bytes;
memset(re, 0, sizeof(char)*bytes);
//nanopause initialization data
tim.tv_sec = 0;
tim.tv_nsec = 95000000;
/* open the device for writing and reading later. */ 
fd = open( "/tyCo/0", O_RDWR, 0);
//fd = open("TEST.txt", O_RDWR, 0);
//baud = ioctl(fd, FIOBAUDRATE, 19200); /*note test length maybe 576000 b/c terminal*/

for (i=0;i<7;i++){
    n_access = n_access+write( fd, &access_code[i], 1); /* write */ 
}
logfile=fopen ("LOGFILE.txt","a"); /* open log file*/
outputfile=fopen ("SENSORDATA.txt","a"); /* open output file*/
if(fd<0)  /* check for open error */
    fprintf(logfile,"%s","\nerror opening file\n");
fprintf(logfile,"%s" "%d" "%c","number of access code bytes written: ",n_access,'\n'); /* display bytes written*/
//step 1 call completed now pause with nano pause
if(nanosleep(&tim,&tim2)<0){
    fprintf(logfile,"%s","Nano sleep system call failed \n");
}
//step 2 complete now read with read code
n_read = read( fd, re, re_len); /* read */
if(n_read<0)
    fprintf(logfile,"%s","error reading serial data from sensor\n");
bytes_written=fprintf(outputfile,"%s",re);
fprintf(logfile,"%s" "%d" "%c","call and response executed number of bytes written: ",bytes_written,'\n');

//baud = ioctl(fd, FIOBAUDRATE, 57600);
close( fd ); /* close */ 
fclose(logfile);
fclose(outputfile);

它將在/ tyCo / 0中寫出“ 001 \\ 0 \\ 0 \\ 0 \\ b 002 003”,這是預期的結果。 為什么需要一次寫入一個字節?

寫入之前,請確保TEST.txt文件中的7個字節之后沒有任何內容,因為您僅寫入7個字節。 您也可以刪除TEST.txt文件,觸摸該文件以使其存在,然后它僅包含您寫入的7個字節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM