03:30 pm on Dec 20, 2011 | read the article | tags: hobby
yesterday, i’ve posted my wrapper library for the windows COM port with the promise of posting the actual C program that uses it. to compile it you need mingw (see here my tutorial on how to get it up and running). the program is simple and writes in the text.txt file whatever it receives on the serial interface, but no more than MAXLINES lines (in this case, 10 lines). you need to save the previous code as “serial.h” in the same directory as the code below. then, compile it and that’s it! =)
ask whatever questions here, and i will answer.
#include <stdio.h> #include <windows.h> #include "serial.h" // the number of lines read from the serial interface. // good for data acquisition for a specified amount of time. #define MAXLINES 10 int main (int argc, int ** argv) { HANDLE comPort; FILE * f; char buffer[32]; unsigned short int readbits = 0; unsigned long int lines = 0; unsigned short int c; // my COM port is COM15. be sure to change it accordingly if ((comPort = openSerialConsole("\\\\.\\COM15")) == (void *) NULL) { printf ("Error: COM Port!\n"); return 1; } // open the file "text.txt". change it for a different file f = fopen ("test.txt", "w+"); // read online MAXLINES lines while (lines < MAXLINES) { // readbits is the number of bytes read from the COM port while ((readbits = readFromSerialConsole(comPort, buffer, 32)) != 0) { // a good thing to check that the readbits+1 character marks the end // of the string. saves you from "Segmentation fault!" errors buffer[readbits] = 0; // check buffer for ENDL character for (c = 0; c<readbits; c++) if (buffer[c] == '\n') { lines++; if (lines == MAXLINES) buffer[c] = 0; } // output to the standard IO, and printf("%s", buffer); // to the file fprintf(f, "%s", buffer); } } // close the file handle fclose (f); // close the COM port closeSerialConsole(comPort); return 0; }
aceast sait folosește cookie-uri pentru a îmbunătăți experiența ta, ca vizitator. în același scop, acest sait utilizează modulul Facebook pentru integrarea cu rețeaua lor socială. poți accesa aici politica mea de confidențialitate.