Go Down

Topic: Arduino logs data to a hard drive (Read 955 times) previous topic - next topic

samolet4e

Aug 12, 2012, 11:45 am Last Edit: Aug 12, 2012, 11:54 am by samolet4e Reason: 1
Hi! I have attached a simple program and a source code for data logging onto the hard drive. Should one doesn't have a micro SD shield, this might be an alternative option currently. The program could be started either through start.bat file or typing in ReadCOM.exe com4 (or com400 whatsoever). The baud rate equals 9600 bps.
Well, this is a quite simple sketch, just to try out. I have compiled the program by means of MSVS 2008 as a console application with precompiled headers.
Feel free to develop the source further and enjoy!
Regards...

// ReadCOM.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[]) {
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
COMMTIMEOUTS CommTimeouts;
DWORD dwBytesTransferred;
//BYTE Byte[1];
char Byte[1];
Byte[0] = '0';
//int TimePakIn = 0;
FILE *filehandler;

    LPCWSTR pcCommPort = argv[1];
    hCom = CreateFile(pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    if (hCom == INVALID_HANDLE_VALUE) {
       printf("Cannot open serial port first!\n"); getc(stdin);
       return 1;
    }//if

    //Build on the current configuration, and skip setting the size of the input and output buffers with SetupComm.
    fSuccess = GetCommState(hCom, &dcb);
    if (!fSuccess) {
       printf("Cannot open serial port second!\n"); getc(stdin);
       return 1;
    }//if

    //Fill in DCB: 9600 bps, 8 data bits, no parity, and 1 stop bit.
    dcb.BaudRate = CBR_9600;  //CBR_115200;//set the baud rate
    dcb.ByteSize = 8;         //data size, xmit, and rcv
    dcb.Parity   = NOPARITY;  //no parity bit
    dcb.StopBits = ONESTOPBIT;//one stop bit

    fSuccess = SetCommState(hCom, &dcb);

    if (!fSuccess) {
       printf("Cannot open serial port third!\n"); getc(stdin);
       return 1;
    }//if

    CommTimeouts.ReadIntervalTimeout         = 100;
    CommTimeouts.ReadTotalTimeoutMultiplier  = 0;
    CommTimeouts.ReadTotalTimeoutConstant    = 4000;
    CommTimeouts.WriteTotalTimeoutConstant   = 0;
    CommTimeouts.WriteTotalTimeoutMultiplier = 0;

    fSuccess = SetCommTimeouts(hCom, &CommTimeouts);

    //Start sampling
    WriteFile(hCom, "s", 1, &dwBytesTransferred, 0);

    if((filehandler = fopen("dataout.txt", "w+")) == NULL) printf( "The file 'dataout.txt' was not opened\n" );
    else printf("The file 'dataout.txt' was opened\n");

    for (;;) {
        //TimePakIn = GetTickCount();
        ReadFile(hCom, Byte, 1, &dwBytesTransferred, 0);
        printf("%c", Byte[0]);
        fprintf(filehandler, "%c", Byte[0]);
    }

    //Unreachable code unless for loop is finite
    fclose(filehandler);
    //Stop sampling
    WriteFile(hCom, "e", 1, &dwBytesTransferred, 0);

    return 0;

}

PaulS

That for loop near the bottom appears to be non-standard syntax. Is that something that Microsoft supports?

samolet4e

That was just not very funny. Just replace the smiley with another semicolon and right parenthesis like this: for(;;) {}
In order to get data logged properly, you must provide output by means of Serial.write(str).

PaulS

Quote
Just replace the smiley with another semicolon and right parenthesis like this: for(;;) {}

As opposed to you posting code properly? I don't think so.

Go Up