I have a programming which draws a graph for .txt data and other things.
Im trying to make this code take single character inputs from the serial port and store them into a txt file as an integer along with the date and time. I think im ok with the date and time. but im not sure how to change what ive done to save data to a txt file. At the mo it just keeps taking inputs, displaying them and sends out _kbhit's.
Previously i was just manually inputing the data as you can see from the commented code at the top.
I have been tying to overcome this problem for days now so any help welcome. Here is my code, thanks.
Code:int GetReadings(int nReadings, int *pReadings) { /*int iReadings; printf("Enter readings, one at a time.\n"); for(iReadings = 0; iReadings < nReadings; iReadings++){ printf("Enter readins #%i: ", iReadings+1); scanf("%i", pReadings++); } return nReadings; */ unsigned char c; DCB dcb; HANDLE hCom; char *pcCommPort; int portno; int nErrNo; DWORD nWritten, nRead; COMMTIMEOUTS CommTimeOuts; // Which port is to be used 1 or 2? printf("Which port (1/2)?: "); scanf("%d",&portno); if(portno == 1)pcCommPort = "COM1"; else pcCommPort = "COM2"; // "Open" the port and get a handle to it hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // exclusive-access NULL, // no security attributes OPEN_EXISTING, // comm devices must use OPEN_EXISTING 0, // not overlapped I/O NULL // must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { printf ("CreateFile failed with error %d.\n", GetLastError()); return 0; } // Get the device control block for the port, check it's OK... if (!GetCommState(hCom, &dcb)) { printf ("GetCommState failed with error %d.\n", GetLastError()); return 0; } // ...and adjust the values to baud=9600 bps, 8 data bits, no parity, and 1 stop bit... dcb.BaudRate = CBR_9600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit // ...and write the device control block back. if (!SetCommState(hCom, &dcb)) { printf ("SetCommState failed with error %d.\n", GetLastError()); return 0; } // Set up the comm timeouts if(!GetCommTimeouts(hCom, &CommTimeOuts)) { printf ("GetCommTimeouts failed with error %d.\n", GetLastError()); return 0; } CommTimeOuts.ReadIntervalTimeout = MAXDWORD; // This combination of values CommTimeOuts.ReadTotalTimeoutConstant = 0; // ..causes ReadFile to return CommTimeOuts.ReadTotalTimeoutConstant = 0; // ..immediately even if no chars rec'd if (!SetCommTimeouts(hCom, &CommTimeOuts)) { printf ("SetCommTimeouts failed with error %d.\n", GetLastError()); return 0; } printf ("Serial port %s successfully reconfigured.\n", pcCommPort); printf("Starting TTY emulator.\n"); // Loop around reading the keyboard and comm port while(c != 'X'){ if(_kbhit()){ c = _getch(); if(WriteFile(hCom, &c, (DWORD)1, &nWritten, NULL) != 0) if((nErrNo = GetLastError()) != ERROR_SUCCESS) printf("\nWriteFile error: %d", GetLastError()); } if(ReadFile(hCom,&c,(DWORD)1,&nRead,NULL) == 0) printf("ReadFile error\n"); if(nRead > 0) putchar(c); } }



LinkBack URL
About LinkBacks



