c语言中把串口当成文件来操作时 fread函数不工作是咋回事?Windows下
c语言中把串口当成文件来操作时 fread函数不工作是怎么回事?Windows下
程序就卡在fread函数那里。。请各位大侠看看怎么回事啊
我的代码是
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
unsigned char AT[3];
unsigned char RBUF[20];
FILE *portfp;
AT[0] = 0x41; AT[1] = 0x54; AT[2] = 0x0D;
system("mode com1: baud=115200 parity=n data=8 stop=1 to=off xon=off");
portfp = fopen("com1","wb+");
if (NULL == portfp){
printf("open com port fail!\n");
exit(0);
}
memset(RBUF, 0, sizeof(RBUF));
printf("will write data to uart\n");
fwrite(AT, 1, 3, portfp);
fflush(portfp);//清除指针缓冲区使数据立即发出
printf("write over, had flush\n");
printf("press any key to continue...\n");
printf("input char:%c\n", getchar());
//Sleep(5000);
fread(RBUF, 1, 3, portfp);
printf("RBUF:%s\n", RBUF);
printf("%#4x,%#4x,%#4x",RBUF[0],RBUF[1],RBUF[2]);
printf("hello world");
return 0;
}
------解决方案--------------------
------解决方案--------------------
fflush(portfp);//清除指针缓冲区使数据立即发出
fseek(portfp,0, SEEK_SET);
试试
------解决方案--------------------
程序就卡在fread函数那里。。请各位大侠看看怎么回事啊
我的代码是
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
unsigned char AT[3];
unsigned char RBUF[20];
FILE *portfp;
AT[0] = 0x41; AT[1] = 0x54; AT[2] = 0x0D;
system("mode com1: baud=115200 parity=n data=8 stop=1 to=off xon=off");
portfp = fopen("com1","wb+");
if (NULL == portfp){
printf("open com port fail!\n");
exit(0);
}
memset(RBUF, 0, sizeof(RBUF));
printf("will write data to uart\n");
fwrite(AT, 1, 3, portfp);
fflush(portfp);//清除指针缓冲区使数据立即发出
printf("write over, had flush\n");
printf("press any key to continue...\n");
printf("input char:%c\n", getchar());
//Sleep(5000);
fread(RBUF, 1, 3, portfp);
printf("RBUF:%s\n", RBUF);
printf("%#4x,%#4x,%#4x",RBUF[0],RBUF[1],RBUF[2]);
printf("hello world");
return 0;
}
------解决方案--------------------
------解决方案--------------------
fflush(portfp);//清除指针缓冲区使数据立即发出
fseek(portfp,0, SEEK_SET);
试试
------解决方案--------------------