Thread: S/w part Number Transmission

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    S/w part Number Transmission

    I have a requirement of software part number to be transmitted over CAN. 8 digits for part number and 3 characters suffix. The 8 digits in BCD and the 3 characters in ASCII. Right justified with any unused with 0 and if suffix contains one character there shall be two spaces between version number and suffix.
    Ex:
    1234567<space><space>A -> (hex) 01 23 45 67 20 20 41
    and here is my code
    Code:
    #include <stdio.h>
    typedef unsigned char uint8_t;
    typedef unsigned long uint64_t;
    
    /* 
     The partnumber should be transmitted as BCD 
     The version suffix should be transmitted as Ascii
    */
    struct SwPartNumber_tag
    {
     uint8_t partNumber[10];
     uint8_t versionSuffix[3];
    }SwPartNumber={"1234567","A"};
    
    uint8_t CanDataPartNumber[10];
    int main(void)
    {
      uint8_t l_data_one_u8=0;
      uint8_t l_data_two_u8=0;
      uint8_t l_final_data_u8=0;
      uint64_t l_partnumber_u64=0;
      uint8_t l_stringlength_orig_u8=0;
      uint8_t l_stringlength_u8=0;
      uint8_t l_index_u8=0;
      uint8_t l_onemore_index_u8=0;
      uint8_t l_count_u8=0;
      uint8_t stringNew[10];
      uint8_t versionNew[10];
      uint8_t l_versionLen_u8=0;
      uint8_t finalarray_u8A[20];
      uint8_t finalindex_u8=0;
      int i=0;
      uint8_t CanFinalData[20];
     
     l_index_u8=0;
     l_stringlength_u8 = strlen(SwPartNumber.partNumber);
     l_stringlength_orig_u8 = strlen(SwPartNumber.partNumber);
     l_count_u8 = 9 - l_stringlength_u8;
    
     if(l_stringlength_u8 - 1 < 8)
      {
        while(--l_count_u8)
        {
          stringNew[l_index_u8] = '0';
          l_index_u8++;
        }
      }
    stringNew[l_index_u8] = '\0';
    strcat(stringNew,SwPartNumber.partNumber);
    printf("PartNumber=%s\n", stringNew);
    
    l_stringlength_orig_u8 =8;
    /* Processing the data */
    for(l_index_u8=0; l_index_u8 < 8; l_index_u8 += 2)
    {
      l_data_one_u8 =  stringNew[l_stringlength_orig_u8-1] - '0'; /* Will extract the data */
      l_data_two_u8 =  stringNew[l_stringlength_orig_u8-2] - '0'; /* Will extract the data */
      l_final_data_u8 =  (l_data_one_u8 | (l_data_two_u8 << 4));
      l_stringlength_orig_u8 -= 2;
      //printf("%d\t%d\t%d\n",l_data_two_u8,l_data_one_u8,l_final_data_u8);
      CanFinalData[finalindex_u8] = l_final_data_u8;
      printf("%x\n",CanFinalData[finalindex_u8] );
      finalindex_u8++;
    }
    
    l_index_u8=0;
    l_versionLen_u8 = strlen(SwPartNumber.versionSuffix);
    l_count_u8 = 4 - l_versionLen_u8;
    
    if(l_versionLen_u8 <= 3)
    {
        while(--l_count_u8)
        {
          versionNew[l_index_u8] = ' ';
          l_index_u8++;
        }
    }
    versionNew[l_index_u8] = '\0';
    strcat(versionNew,SwPartNumber.versionSuffix);
    //printf("version2=%s\n", versionNew);
    finalindex_u8--;
    for (i = 0; i < 3; ++i)
    {
      /* code */
      CanFinalData[finalindex_u8] =versionNew[i];
      printf("%x\n",CanFinalData[finalindex_u8] );
    }
    Did I write the code very long? Is there any better method? Please suggest.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well a few functions to break up the overly long main would be nice.
    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
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Ok, Thank you. I modified the code as follows
    Code:
    #include <stdio.h>
    typedef unsigned char uint8_t;
    typedef unsigned long uint64_t;
    
    /* 
     The partnumber should be transmitted as BCD 
     The version suffix should be transmitted as Ascii
    */
    struct SwPartNumber_tag
    {
     uint8_t partNumber[10];
     uint8_t versionSuffix[4];
    }SwPartNumber={"1234567","A"};
    void SwPartNumberRightAdjustFunction(uint8_t *);
    void ConvertToBCDFunction(uint8_t *s);
    void VersionFillSpace(uint8_t *s);
    uint8_t finalResult[20];
    uint8_t g_finalcount_u8=0;
    int main(void)
    {
      uint8_t stringRightJustified[10]; 
      uint8_t i=0;
      SwPartNumberRightAdjustFunction(SwPartNumber.partNumber);
      printf("%s\n",SwPartNumber.partNumber);
      ConvertToBCDFunction(SwPartNumber.partNumber);
      VersionFillSpace(SwPartNumber.versionSuffix);
      for (i = 0; i < g_finalcount_u8; ++i)
      {
        printf("%x\n", finalResult[i]);
      }
    return 0;
    }
    
    void SwPartNumberRightAdjustFunction(uint8_t *s)
    {
    
     uint8_t l_index_u8=0;
     uint8_t l_stringlength_u8=0;
     uint8_t l_count_u8=0;
     uint8_t stringNew[10];
    
    
     l_index_u8=0;
     l_stringlength_u8 = strlen(s);
     l_count_u8 = 9 - l_stringlength_u8;
    
     if(l_stringlength_u8 - 1 < 8)
      {
        while(--l_count_u8)
        {
          stringNew[l_index_u8] = '0';
          l_index_u8++;
        }
      }
    stringNew[l_index_u8] = '\0';
    strcat(stringNew,s);
    strcpy(s,stringNew);
    //printf("PartNumber=%s\n", s);
    }
    void ConvertToBCDFunction(uint8_t *s)
    {
    uint8_t l_index_u8=0;
    uint8_t l_data_one_u8=0;
    uint8_t l_data_two_u8=0;
    uint8_t l_final_data_u8=0;
    uint8_t l_stringlength_u8=strlen(s);
    
    for(l_index_u8=0; l_index_u8 < 8; l_index_u8 += 2)
     {
      l_data_one_u8 =  s[l_stringlength_u8-1] - '0'; /* Will extract the data */
      l_data_two_u8 =  s[l_stringlength_u8-2] - '0'; /* Will extract the data */
      l_final_data_u8 =  (l_data_one_u8 | (l_data_two_u8 << 4));
      l_stringlength_u8 -= 2;
     printf("%d\t%d\t%d\n",l_data_two_u8,l_data_one_u8,l_final_data_u8);  
      finalResult[g_finalcount_u8++]=l_final_data_u8;
     }
      
    }
    
    void VersionFillSpace(uint8_t *s)
    {
    uint8_t l_versionLen_u8=0;
    uint8_t l_count_u8 =0;
    uint8_t versionFill[10];
    uint8_t l_index_u8=0;
    uint8_t i=0;
    //printf("s=%s\n",s );
    l_versionLen_u8 = strlen(s);
    l_count_u8 = 4 - l_versionLen_u8;
    
    if(l_versionLen_u8 <= 3)
    {
        while(--l_count_u8)
        {
          versionFill[l_index_u8] = ' ';
          l_index_u8++;
        }
    }
    versionFill[l_index_u8]='\0';
    strcat(versionFill,s);
    //versionFill[3]='\0';
    //printf("Version= %s\n",versionFill);
    strcpy(s,versionFill);
    for (i = 0; i < 3; ++i)
    {
     finalResult[g_finalcount_u8++] = s[i];
    }
    printf("Version=%s\n",s);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some more points.
    1. Try and do this without using any global variables. Pass parameters to output result buffers for example.
    2. Apply a consistent indentation style.
    3. The l_ prefix and _u8 suffix on all the variables adds nothing, except to make the thing unreadable.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I wanted to have part number as global variable as in future it may change for every new version and change frequently, i want to provide at a place where it can be changed easily.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting part of a number
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2009, 12:44 PM
  2. removing a part from a number
    By samsung in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 05:41 PM
  3. Replies: 2
    Last Post: 11-18-2006, 03:31 AM
  4. How to get the decimal part of a number...
    By Caldus in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2006, 06:41 PM
  5. finding the decimal part of a number
    By Geo-Fry in forum C++ Programming
    Replies: 13
    Last Post: 07-31-2003, 12:43 PM

Tags for this Thread