2016-08-10 2 views
0

안녕하세요 저는 UEIPAC 600-1G 큐브를 온도 센서로부터 데이터를 수신하도록 프로그래밍하려고합니다. 센서를 활성화하기 위해서는 이진 데이터를 보내야합니다. 난 16 진수 값으로 설정된 문자를 보내려고하지만 뭔가 잘못되었습니다. 나는 데이터를 텍스트 파일에 덤핑하여 테스트를했고, 공백 문자를 몇 개의 giberish 문자가 따라왔다. 직렬 포트를 통해 이진 데이터를 보내려면 어떻게합니까?VxWorks C를 사용하여 이진 데이터를 직렬 포트로 보내십시오.

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); 

답변

0

이 예상되는 것입니다/TYCO/0으로 "001 \ 0 \ 0 \ 0 002 B \ 003"을 기록합니다. 왜 한 번에 1 바이트 씩 쓸 필요가 있습니까?

7 바이트 만 쓰고 있기 때문에 TEST.txt 파일에 쓰기 전에 7 바이트 뒤에 아무것도 없는지 확인하십시오. TEST.txt 파일을 제거하고 파일을 터치하여 파일이 존재하면 7 바이트 만 쓸 수 있습니다.

관련 문제