我正在尝试使用以下代码读取COM端口,但即使我在rs232端口输入输入,端口上的可用数据也始终为零。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *Wfile;
long size;
char readport[100];
if((Wfile = fopen("com1:", "r")) == NULL) // open comport
{
printf("port not opened\r\n");
}
else
{
fseek(Wfile,0L,SEEK_END);
size= ftell(Wfile);
fseek(Wfile,0L,SEEK_SET);
printf("%ld",size);
fread(readport,size,1, Wfile);//reading CommunicationString from ComPort
printf("\n%s",readport);
fclose(Wfile);}
return 0;
}
}