Thread: unusual behaviour of my function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    68

    unusual behaviour of my function

    Hello everyone,
    I have a function that creates a custom protocol for transmission of data to a serial port.
    The function is below:
    Code:
    void calc()
    {
    /*cmd array and num integer are global*/
        unsigned char checksum = 0;
        cmd[0] = 0xAA;   //starting byte
        cmd[1] = 0x10;
        cmd[2] = 4;
        cmd[3] = 'G';
        cmd[4] = 'P';
        cmd[5] = ' ';
        cmd[6] = num & 0xFF;        //lsb
        cmd[7] = (num >> 8) & 0xFF; //msb
    
        //printf("lsb: %d",cmd[6]);
        //printf("\nmsb: %d",cmd[7]);
    
        checksum = 0;
        checksum ^= cmd[1];
        checksum ^= cmd[2];
        checksum ^= cmd[3];
        checksum ^= cmd[4];
        checksum ^= cmd[5];
        checksum ^= cmd[6];
        checksum ^= cmd[7];
    
        cmd[8] = checksum;
       //printf("\nchecksum:%d",cmd[8]);
        cmd[9] = 0xCC;     //closing byte
        cmd[10] = 0;
        cmd[11] = 0;
        //cmd[12] = 0;
    }
    Now in my main.c i am sending the cmd array:
    Code:
    calc(num);
            if(!WriteFile(hSerial,cmd,strlen(cmd),&dwBytesRead, NULL))
            {
                //if any error occurs in writing to serial port.
                printf("\n\nError occured while writting file. \n");
                exit(1);
            }
            else
            {
                printf("\nData sent.");
            }
    Just to confirm that all the data was indeed sent to the serial port ,i looped the port back to my computer to re-read the data.
    Code:
    if(!ReadFile(hSerial, rzBuff, 10, &dwBytesRead, NULL))
            {
                system("cls");
                printf("Error occured while reading string data.\n");
                system("pause");
                exit(1);
            }
            else
            {
                printf("\n\nRead data: \n");
                printf("0:%x\n1:%x\n2:%d\n3:%c\n4:%c\n5:%c\n6:%d\n7:%d\n8:%d\n9:%x\n10:%d",rzBuff[0],rzBuff[1],rzBuff[2],rzBuff[3],rzBuff[4],rzBuff[5],rzBuff[6],rzBuff[7],rzBuff[8],rzBuff[9],rzBuff[10]);
            }
    memset(rzBuff,0,strlen(rzBuff));
    memset(cmd,0,strlen(cmd));
    All is working fine except for one strange thing.
    if num is less than 256 and greater than 799 the closing bit i.e cmd[9] always shows 0!!!
    I am not able to understand what i am doing wrong,no matter what , cmd[9] should always be 0xcc.

    I know this is more of a windows programming related discussion,but i am having trouble in the basic functionality and asignment of variables in a function.

    Please help.
    Last edited by ak47; 12-19-2013 at 11:24 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You're using strlen() on a buffer which contains binary data (eg checksum results).

    > if(!ReadFile(hSerial, rzBuff, 10, &dwBytesRead, NULL))
    Also check dwBytesRead is the number you expect.

    Is it really 10 bytes?
    If so, what are these here for?
    cmd[10] = 0;
    cmd[11] = 0;
    //cmd[12] = 0;
    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. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Can you paste the declaration of cmd?

    I'm guessing that cmd is an array of unsigned char (or char). Looking at your printf() format string you have n8:%d. That expects an integer which is very unlikely to be the same size as char or unsigned char. So, it's probably printing cmd[8] and cmd[9] (at least, probably more) "combined".

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    Quote Originally Posted by Salem View Post
    You're using strlen() on a buffer which contains binary data (eg checksum results).

    > if(!ReadFile(hSerial, rzBuff, 10, &dwBytesRead, NULL))
    Also check dwBytesRead is the number you expect.

    Is it really 10 bytes?
    If so, what are these here for?
    cmd[10] = 0;
    cmd[11] = 0;
    //cmd[12] = 0;
    Actually my array is 10 bytes.
    Code:
    unsigned char cmd[10] = "\0";
    unsigned char rzBuff[10] = "\0";
    Originally cmd[] was 12 bytes long,i needed to confirm that data after cmd[9] was being looped back correctly or not. I have changed it to 10 bytes now and removed cmd[10] and cmd[11]

    Secondly,about the strlen() in Readfile(),im confused i have used strlen() in Writefile().Yoy mean i should remove strlen() and write the size of the array? 10 in this case?

    Thirdly,
    I checked with dwBytesRead variable
    Here are the results i got
    0 to 255 : 7 bytes
    256 above: 10 bytes

    Edit: I can post the full program if you wish
    Last edited by ak47; 12-20-2013 at 01:17 AM. Reason: last line

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    Quote Originally Posted by Hodor View Post
    Can you paste the declaration of cmd?

    I'm guessing that cmd is an array of unsigned char (or char). Looking at your printf() format string you have n8:%d. That expects an integer which is very unlikely to be the same size as char or unsigned char. So, it's probably printing cmd[8] and cmd[9] (at least, probably more) "combined".
    Code:
    unsigned char cmd[10] = "\0"; 
    unsigned char rzBuff[10] = "\0";
    i thought that ,that %d would effect the next byte too,but it did not.I confirmed it by changing it to %c(1 byte) and checking,still cmd[9] value mysteriously disappears

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    Hey guys,i solved the issue,it was in my Writefile statement.
    Code:
    if(!WriteFile(hSerial,cmd,strlen(cmd),&dwBytesRead, NULL))
    I removed the strlen() rightly pointed out by Salem and replaced it with the actual size,i.e 10 and wola..it worked

    Thank you for all your help
    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behaviour in questions function
    By Gil Carvalho in forum C Programming
    Replies: 4
    Last Post: 06-13-2012, 08:23 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Unusual 'malloc()' behaviour...
    By sanjav in forum C Programming
    Replies: 4
    Last Post: 03-28-2009, 05:26 PM
  4. unusual behaviour by GETS function
    By capvirgo in forum C Programming
    Replies: 9
    Last Post: 02-07-2008, 05:05 AM
  5. Looking for an unusual c++ book
    By algruber in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2002, 12:21 PM