Thread: Unexpected "tab" showing up in a string array

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    1

    Unexpected "tab" showing up in a string array

    I am dealing with a unique device that has a specific protocol to communicate with it via RS485. I must convert a decimal number (temperature) into a character array and concatenate this to another character array.

    I've build a decimal to hex converter and this appears correct. I have a second function which contains a "dummy string" of "00000000" to comply with the device protocol and I substitute the temperature string into this string using a for loop. (outcome like "000002D4")

    If I call each of these functions from the main program, all is well. If I call these functions from within a function source code file, I get the tab substituted in front of my generated string. (like "0000 2D4").

    Code:
    /* Included Files
    ==============================================================================*/
    #include <string.h>
    #include <stdio.h>
    //#include<conio.h>
    #include "mcc_generated_files/delay.h"
    #include "CompoWay-F.h"
    
    /* Command Block Generator
     ===============================================================================
     This function takes the unit number and generates the header (node number,
     sub-address and SID) for the command line of the CompoWay/F protocol.
     =============================================================================*/
    char *command_blk_generator(char *cmd_blk, char *node, char *cmd_line)
    {
        char sub_add_SID[] = "000";     //always 00 0
        const char STX = 0x2;           //start transmission character
        const char ETX = 0x3;           //end transmission character
        char bcc;                       //block check character
        int str_len;
        int i;
        
        strcpy(cmd_blk, node);          //Set node
        strcat(cmd_blk,sub_add_SID);    //append sub address & SID
        strcat(cmd_blk,cmd_line);       //append command line
        strncat(cmd_blk,&ETX,1);        //append ETX
        bcc = BCC_generator(cmd_blk);   //generate the block check character
        strncat(cmd_blk,&bcc,1);        //append the BCC
        
        //shift cmd_blk right one character, add STX at beginning of array
        str_len = strlen(cmd_blk);
        for (i = str_len; i > 0; --i)
        {
            cmd_blk[i]=cmd_blk[i-1];
        }
        cmd_blk[0]=STX;
        
        return(cmd_blk);
    }
    
    
    /* BCC Generator
     ===============================================================================
     This function determines the XOR of each character in the command string 
     (including the ETX-end of transmission character 0x3) to generate a BCC
     (Block Check Character) necessary to append to the end of a command block when
     using Omron's CompoWay/F Communication Protocol
     =============================================================================*/
    char BCC_generator(char *str)
    {
        char bcc;
        int str_len;
        int i;
    
        str_len = strlen(str);
    
        bcc = (str[0])^(str[1]);    
        for (i = 2; i < str_len; ++i)
        {
            bcc = bcc^(str[i]);
        }
        return(bcc);
    }
    
    /* Decimal_to_Hex
     ===============================================================================
     Converts a long into a hex for use in the command generation using 
     Omron's CompoWay/F Communication Protocol
     =============================================================================*/
    char *dec2hex(int decnum, char *hexnum)
    {
        int rem;
        int i = 0;
        
        //determine coefficients by dividing by 16
        while(decnum!=0)
        {
            rem = decnum%16;
            if(rem<10)
                rem = rem+48;
            else
                rem = rem+55;
            hexnum[i] = rem;
            i++;
            decnum = decnum/16;
        }
    
        return (hexnum);
    }
    
    /* hex2cmd
     ===============================================================================
     formats a hex string into the 8 byte hex string necessary for Omron's
     CompoWay/F Communication Protocol
     =============================================================================*/
    char *hex2cmd(char *hexnum, char *hexstr)
    {
        int str_len;
        int i = 0;
        char tempstr[] = "00000000";
                
        str_len = strlen(hexnum);
    
        for (i = 0; i < str_len; ++i)
        {
            tempstr[7-i] = hexnum[i];
        }    
        
        strcpy(hexstr, tempstr);
    
        return (hexnum);
    }
    
    /* compo_write_variable
     ===============================================================================
     formats a hex string into the 8 byte hex string necessary for Omron's
     CompoWay/F Communication Protocol
     =============================================================================*/
    char *compo_write_variable(long var, char *type, char *write_str)
    {
        char hexnum[50];
        char hexstr[9];
        
        dec2hex((var*10),hexnum);       //convert temperature to hex
        hex2cmd(hexnum,hexstr);         //convert hex value to hex string
        strcpy(write_str,type);           //generate the SP command
        strcat(write_str,hexstr);         //append the hex string (0xTemp)
        
        return(write_str);
    }
    
    /* end of file*/

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    You need a +1 here, otherwise you won't move the '\0' terminator.
    Code:
        str_len = strlen(cmd_blk) + 1; /// added +1
        for (i = str_len; i > 0; --i)
        {
            cmd_blk[i]=cmd_blk[i-1];
        }
        cmd_blk[0]=STX;
    Here you've said +55 where you meant +65. You shouldn't use 48 and 65, though; just use '0' and 'A' (they are the same thing!).
    Code:
            if(rem<10)
                rem = rem+48;   /// should be + '0'
            else
                rem = rem+55;   /// should be + 'A'
    You can create a zero-padded 8-hexdigit-wide value like this:
    Code:
    void dec2hex(int num, char *hexnum)
    {
        sprintf(hexnum, "%08X", num);
    }
    I removed the return value since it doesn't make much sense to me.
    And the input is actually a binary integer, or course, not decimal (unless you are using a very unusual computer).

    And here's an idea for command_blk_generator. Note that it passes cmd_blk + 1 to BCC_generator since you seem to not want to include STX in that calculation, although you do include ETX.
    Code:
    void command_blk_generator(char *cmd_blk, const char *node, const char *cmd_line)
    {
        const char *sub_add_SID = "000";  //always 000
        const char *STX = "\x2";          //start transmission character
        const char *ETX = "\x3";          //end transmission character
        char bcc[2] = {0};                //block check character
     
        strcpy(cmd_blk, STX);
        strcat(cmd_blk, node);             //Set node
        strcat(cmd_blk, sub_add_SID);      //append sub address & SID
        strcat(cmd_blk, cmd_line);         //append command line
        strncat(cmd_blk, ETX);             //append ETX
        bcc[0] = BCC_generator(cmd_blk+1); //generate the block check character
        strcat(cmd_blk, bcc);              //append the BCC
    }
    And an idea for compo_write_variable:
    Code:
    void compo_write_variable(long var, const char *type, char *write_str)
    {
        assert(var < 10000000);
        char hexstr[9];
        sprintf(hexstr, "%08X", var * 10);
        strcpy(write_str, type);           //generate the SP command
        strcat(write_str, hexstr);         //append the hex string (0xTemp)
    }
    And BCC_generator:
    Code:
    char BCC_generator(const char *str)
    {
        char bcc = 0;    
        int len = strlen(str);
        for (int i = 0; i < len; ++i)
            bcc ^= str[i]);
        return bcc;
    }
    Last edited by john.c; 03-25-2022 at 02:44 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 12-03-2016, 10:52 PM
  2. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  3. Replies: 46
    Last Post: 08-24-2007, 04:52 PM

Tags for this Thread