HELP with storing serial port data into txt file

This is a discussion on HELP with storing serial port data into txt file within the C Programming forums, part of the General Programming Boards category; I have a programming which draws a graph for .txt data and other things. Im trying to make this code ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    20

    HELP with storing serial port data into txt file

    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);
    			
        }
    	
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    how these 2 functions
    Code:
           if(_kbhit()){
                c = _getch();
    are concerned with the reading from the com-port?
    you should use the correct API functions to read using hCom handle
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    how these 2 functions
    Code:
           if(_kbhit()){
                c = _getch();
    are concerned with the reading from the com-port?
    you should use the correct API functions to read using hCom handle
    They are not related to reading the comport, but from what I understand of the code, it's a very simple terminal program that reads the console and echoes it to the serial port, reads the comport and echoes to the console.

    To trivially write to a file, just open a file using fopen() and then use fputc() next to your putchar(). If you want both input and output to appear in the same file, then use a fputc() next to the WriteFile(hCom, ....) too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 06-16-2008, 02:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21