串列埠通訊——寫串列埠(C語言)
阿新 • • 發佈:2019-02-01
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; }
上述程式因為將資料寫入串列埠是資料型別沒有考慮周全,一直存在問題,所以要保證接收端與傳送端資料型別的一致性。