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*/