|
问题:使用串口工具写的数据正常写入txt文件,而C编写的串口有时可以有时不行,付代码,求大神指点。
代码:
//============================
#include <SD.h>
File myFile;
char text[200];
int i= 0;
void setup()
{
Serial.begin(9600);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop(){
while(!(Serial.available()> 0)){
;
}
Serial.readBytes(text,200);
Serial.print("Initializing SD card...");
myFile = SD.open("test.txt",FILE_WRITE);
if(myFile){
Serial.println(text);
myFile.print(text);
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
memset(text,0,200);
delay(1000);
}
//======================================
C程序
sprintf(outBuffer,"hello 中国!");
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
bWriteStat = WriteFile(hCom,outBuffer,len,&len,NULL);
|
|