C语言程序,将一个文件中保存的控制LED的十六进制数读取,并写入串口由433M发送至连接有另一433M的MSP430单片机
C语言程序:
#include <stdio.h>
#include <windows.h>
FILE * fileFP;
int main(void)
{
FILE *fp;
int temp;
int count = 0;
if((fp=fopen("com3", "w")) == NULL) puts("can't open the com/n");
if((fileFP=fopen("readdata.txt", "r")) == NULL) puts("can't open the file/n");
while(1)
{
fscanf(fileFP, "%x", &temp);
if(temp != ' ')
{
printf("%x", temp); //在cmd上打印一下读取的信息
//printf("%d", temp);
fprintf(fp, "%c", temp);
// fprintf(fp, "%x", temp);这种方式由于单片机程序接收中断的data是uchar类型,所以会被当成字符处理,最终转成二进制
count++;
}
else
Sleep(100);
if(count >= 1) //一次只写入一个信息(即十六进制数)
{
break;
}
}
fclose(fp); //关闭文件及串口
fclose(fileFP);
return 0;
}
上述程序因为将数据写入串口是数据类型没有考虑周全,一直存在问题,所以要保证接收端与发送端数据类型的一致性。