Thread: will this work? just a quick test needed

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    See, I'm not the only one who hates reading ugly code.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > FILE *fp = fopen("bufferdata.txt", "a+b");
    Did you check for errors in opening the file?
    Nope, didn't think so.

    > fputs(string, fp);
    Writing strings to a file opened in binary mode is suspicious.
    Are you entirely sure that your data is text, or is it binary.

    If the first byte of your 250 bytes from the serial port is \0, then yes, you'll end up with no data in your file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by Salem
    > FILE *fp = fopen("bufferdata.txt", "a+b");
    Did you check for errors in opening the file?
    Nope, didn't think so.

    > fputs(string, fp);
    Writing strings to a file opened in binary mode is suspicious.
    Are you entirely sure that your data is text, or is it binary.

    If the first byte of your 250 bytes from the serial port is \0, then yes, you'll end up with no data in your file.
    THE FILE IS CREATED, AS I SAID EARLIER.
    the "\n" is printed into it everytime the program runs.
    so it works. i have used this code before and it seems to work perfectly.

    i tried to put a hardcoded string input buffer and it is written perfectly so it works. i have tried using fwrite() but the program crashes half way through.

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by dwks
    Do you mean completely empty, as in zero bytes?
    the only thing written is the newline character. which means that the file opens and writes but not what i want it to...

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So it looks like plan B - there is a \0 at the start of your buffer.

    And try showing what you did using fwrite(), rather than dismissing it as "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    the program doesnt run when something else is accessing the same com port so the opening port section works.

    the closing port section also works.

    i tried writing a section of data i.e. "yo mama" to the port and the read section of the port displayed it on screen perfectly.

    so the writing section works and the reading section reads the written string on the port.

    BUT, at the moment, in the txt file, i╢m getting rubbish like:

    лллллллллллллллллллллллллллллллллллллллллллллллллл лллллллллллллллллллллллллллллллллллллллллллллллллл лллллллллллллллллллллллллллллллллллллллллллллллллл лллллллллллллллллллллллллллллллллллллллллллллллллл лллллллллллллллллллллллллллллллллллллллллллллллллл лллллллллллллллллллллллллллллллллллллллллллллллллл ллллллллллллллллллллллллллллллллллл╠ ─ ─ ─ ─ ─── ─ ── ─ ─── ── ──────── ──────── ── ─── ─ ─── ─── ─── ─ ── ─ ─ ─ ─ ─ ─ ── ── ─ ─── ─ ─ ── ─── ─ ─── ─ ─ ─ ── ── ─ ─ ── ─ ── ─── ─ ─ л Д  ─% ▒    ╪AB ллллюЪ @  PC ░
    C ┌ ПЩ plW─■Ъ ]^ВЮЪ d▌@ xCB ПЪ iКГw┌ ПЩТ\^ВхЪ jS─ЪЪЪЪ├╩ИwZИw @ Actx  < |    4 ╛   d ┼ [IY-П 2 $ ╗   ▄   ╛    л p  SsHd,     ┬  , Z Z  $ 4 C : \ W I N D O W S \ W i n S x s \ ╦ ┼  [IY-D 2 x ╗  \   ,  пч┐НLvе  S y s t e m D e f a u l t C o n t e x t M i c r o s o f t . W i n d o w s . G d i P l u s \ Л т  р б ─`НLvе  ┬ √ M i c r o s o f t . W i n d o w s . G d i P l u s , p r o c e s s o r A r c h i t e c t u r e = " x 8 6 " , p u b l i c K e y T o k e n = " 6 5 9 5 b 6 4 1 4 4 c c f 1 d f " , t y p e = " w i n 3 2 " , v e r s i o n = " 1 . 0 . 0 . 0 " C : \ W I N D O W S \ W i n S x S \ M a n i f e s t s \ x 8 6 _ M i c r o s o f t . W i n d o w s . G d i P l u s _ 6 5 9 5 b 6 4 1 4 4 c c f 1 d f _ 1 . 0 . 0 . 0 _ x - w w _ 8 d 3 5 3 f 1 3 . M a n i f e s t x 8 6 _ M i c r o s o f t . W i n d o w s . G d i P l u s _ 6 5 9 5 b 6 4 1 4 4 c c f 1 d f _ 1 . 0 . 0 . 0 _ x - w w _ 8 d 3 5 3 f 1 3 SsHd,     ,  °Йj,D  \   G d i P l u s . d l l 
    This happens when the mouse is moved.

    the funny thing is the ─ signs as the 3rd party software shows the mouse input to be this too. ALSO, the program waits at "com port configured" until there is some input from the mouse.

    my current code is:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    
    int main( int argc, char *argv [] )
      {
        char character;
        char *pcCommPort;
    
        if (argc > 1)
            {
            pcCommPort = argv[1];
            }
        else
            {
            pcCommPort = "COM1";
            }
    
        DCB dcb;
        HANDLE hSerial;
        BOOL fSuccess;
        //   char *pcCommPort = "COM2";  //Set at com 2 for moment
        char szBuff[251] = { 0 };
        DWORD dwBytesRead = 0;
        char string[BUFSIZ];
    
        hSerial = CreateFile(pcCommPort, GENERIC_READ |GENERIC_WRITE, 0, // must be opened with exclusive-access
                             NULL,                                       // no security attributes
                             OPEN_EXISTING,                              // must use OPEN_EXISTING
                             0,                                          // not overlapped I/O
                             NULL                                        // hTemplate must be NULL for comm devices
        );
    
        if (hSerial == INVALID_HANDLE_VALUE)
            {
            printf("CreateFile failed with error %d.\n", GetLastError());
            return (1);
            }
    
        // Build on the current configuration, and skip setting the size
        // of the input and output buffers with SetupComm.
    
        fSuccess = GetCommState(hSerial, &dcb);
    
        if (!fSuccess)
            {
            // Handle the error.
            printf("GetCommState failed with error %d.\n", GetLastError());
            return (2);
            }
    
        // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
    
        dcb.BaudRate = CBR_9600; // set the baud rate
        //	   dcb.BaudRate = CBR_57600;      // FOR TESTING
        dcb.ByteSize = 8;          // data size, xmit, and rcv
        dcb.Parity = NOPARITY;     // no parity bit
        dcb.StopBits = ONESTOPBIT; // one stop bit
    
        fSuccess = SetCommState(hSerial, &dcb);
    
        if (!fSuccess)
            {
            printf("could not implement port settings");
            return (3);
            }
    
        printf("Serial port %s successfully reconfigured.\n", pcCommPort);
    
        BOOL fWaitingOnRead = FALSE;
        OVERLAPPED osReader = { 0 };
    
        if (!fWaitingOnRead)
            {
            // Issue read operation.
            if (!ReadFile(hSerial, szBuff, 250, &dwBytesRead, &osReader))
                {
                if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
                    printf("Error in reading\n");
                else
                    fWaitingOnRead = TRUE;
                }
            else
                {
                ReadFile(hSerial, szBuff, 250, &dwBytesRead, &osReader);
                printf("bytes read %d", szBuff);
                }
    
            DWORD dwBytesWrite = 0;
            WriteFile(hSerial, "log rawwaasframe onnew", 24, &dwBytesWrite, NULL);
            printf("writing bytes\n\n");
    
            printf("bytes have been read!!\n\n");
            strncpy(string, szBuff, dwBytesRead);
            //		string[dwBytesRead] = '\0';
            CloseHandle(hSerial);
    
            FILE *fp = fopen("bufferdata.txt", "a+b");
    
            {
    
            //*****************Writes contents of decoded final data to the appropriate hour file********
    
            fprintf(fp, "\n"); // write to a new line	
    //        fputs(string, fp);
            fwrite(string, 250, 250, fp);
            printf("String Written in file\n\n");
    
            //******************Closes the file, flushes all buffers and waits for next cycle*************************
    
            fclose(fp);
            fflush(NULL);
    
            printf("All processes successful\n\n");
            printf("data obtained : %s", string);
            scanf("%c", &character);
            }
    
            return 0;
            }
      }
    thanks for your help so far
    Last edited by shoobsie; 07-11-2005 at 01:19 AM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Your indentation still sucks.
    Turn OFF TABS in your IDE, and indent using 4 spaces for code you intend to post to the board. Mixing spaces and TABS is a real killer if you want presentable code on a message board.

    You still haven't got the idea about this
    Code:
           strncpy(string, szBuff, dwBytesRead);
            //		string[dwBytesRead] = '\0';
            CloseHandle(hSerial);
    
            FILE *fp = fopen("bufferdata.txt", "a+b");
    
            {
    
            //*****************Writes contents of decoded final data to the appropriate hour file********
    
            fprintf(fp, "\n"); // write to a new line	
    //        fputs(string, fp);
            fwrite(string, 250, 250, fp);
    string serves no purpose except to mess up your data because you have no \0 anywhere in your buffer. You even comment out your attempt to place a \0 in the result.
    So as a result, you copy random garbage as well.

    You already have a buffer and a length, use them.
    Code:
    fwrite( szBuff, 1, dwBytesRead, fp );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Hey man tabs rule. Though I feel for your... problems.

    This whole thread had me rolling on the floor, seriously. Indentation can be a thrilling flame starter on forums, not pretty. Especially if it's on a Linux forum and the thread starter is a win32 programmer; like watching a train crash in slow motion.

    In my opinion indentation should be either 4 or 8 spaces. I think 2 spaces looks a little too 'condensed' - again this is my opinion. I noticed a couple of people use 2 spaces so please don't .......... to me about it.

    As if anyone cared, here's the coding style of this 17 year old:

    - indentation = 8 spaces
    - place the opening brace on a new line if at the start of a function / structure / class declaration, otherwise place it on the same line:

    Code:
    class Person 
    {
            public:
                    Person ();
                    ~Person ();
    };
    
    Person::Person ()
    {
            if (0) {
                    // ...
            } else {
                    // ...
            }
    }
    - meaningful names rock
    - identifiers such as 'a' and '_x34' rock less than Iron Maiden
    - when commenting, assume the reader can read c++, but can't read your mind.
    - spaces after identifiers etc: if (0) not if(0)

    Well that's about it. I may have over-replied here, but i'm bored sh**less.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 08:02 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  3. Replies: 4
    Last Post: 03-21-2006, 10:21 PM
  4. Taking a Programming Test and need help with something.
    By 1BadRbt in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2006, 06:19 AM
  5. "break" - quick response needed
    By luke in forum C Programming
    Replies: 4
    Last Post: 04-23-2002, 08:57 AM