byte/word to string conversion hangs in loop

Beta Testing discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

byte/word to string conversion hangs in loop

#1 Post by Rotary_Ed » 24 Jun 2011 20:21

With MKP 4.8 I have encountered a problem. In one loop I use, the conversion from byte/word to string causes the code to hang up.
Bytetostr - hangs
WordToStr - hangs
//non-MKP Conversion
Byte2StrN - hangs
Word2strN - only conversion that does not hang in this loop

I do not have a problem anywhere else in the code, but then this is the only code where I have a loop using the conversion twice in the same repeat loop.

I have submitted this problem with a file under ticket QXL-696258

For those interested, Here is the code; First is the Main program followed by a needed Unit of variables and record definitions

Code: Select all

program Test_Byte;
uses EIC_Variables;
Procedure Initialize;
   begin
      R_ConfigRPM.User_Max_Value := 20;
   end;
   
Procedure Config_Send_to_Pmag;
   begin
      nop;
   end;
   
Procedure Store_Pmag_Configuration ;
   begin
      nop;
   end;
Procedure Config_To_PMag; //This sends the selected Pmag configuration in EIC to Pmag over Rs232 link
                          //and sets Last_used_Flag in EEPROM
   Var
      Cmd_Value_Str:string[2];
      Hexstring1:string[2];
      Lengthx:byte;
      i:byte;  //index for FOR loop
      Value:byte;
      ValueI:integer;
      Send_times:byte;
      Save1_Channel:byte;
   begin
    send_Times := 0;
    i := 1;

    if save_Channel = 3 then
    begin

      Channel := 1;  //if dual then change to first channel 1 (right) and then Channel 2 (left) channel reset to 2 at bottom of For statement
      send_Times := 3;  //send it twice, 2nd time to channel 2 (left)

     end
    else
       Send_Times := 1; //send once to channel 1


     Repeat

  //*********** Max RPM Change  to EPI  **************
      MaxRPMdata1.raw := R_ConfigRPM.User_Max_Value ;  //From EEPROM or Changes made to Configuration record values
      Save_RPM_Max := R_ConfigRPM.User_Max_Value * 256; //* 256 to turn Raw rpm Value into display Value                                                                 //save for comparison of changed config on the fly                                                                 //From EEPROM or Changes made to Configuration record values
      Value := R_ConfigRPM.User_Max_Value ;
      //************** PROBLEM AREA *******************************************
      // bytetostr(Value, Hexstring1);     //All three of these converstions  to string
      //Wordtostr(Value, Hexstring1);    // cause program to hang here
      //*********** used different conversion library in attempt to get coversion to work - I think you will recognize it
      //Byte2strN(value,Hexstring1,2);
      word2strN(value,Hexstring1,2);     //This is the only conversion that does not hang
      Cmd_Value_Str := '/M' ;
      strcat( Cmd_Value_Str,HexString1);
      Txbuff1 := Cmd_Value_Str;
      Lengthx := Strlen(Txbuff1);
      Txbuff1[Lengthx] := $0D;
      Config_Send_to_Pmag;
      Delay_MS(50); // 20 May 2011 1 Apr was 100}

      channel := 2;    // Set to channel 2 Left for second transmission to left pmag if required

      Store_Pmag_Configuration; // 1 Dec 2010   //stores Sent Configuration "permanently' in EPI Flash Memory
      Config_Send_to_Pmag;
      if i = 2 then Delay_MS(250);  //delay between right and left Pmags only if dual
      inc(I);
      until ( I >= Send_Times);   //if single channel send_times = 1 , I =2 so exits,  If Send_Times =3 then loop repeats a second time

      nop;   //In Debug run to cursor here

  end;
begin
   Initialize;
   save_Channel := 3;
   Config_To_PMag;
end.
Needed unit

Code: Select all

Unit EIC_Variables;


  Type Static = Record
       Text1:string[4];   // 5 Nov  was string[4];
       Raw     :integer;  // 1 Apr
       Value    :dword;

     end;

     
     //Type for RPM and Hobbs configuration Record (1st line in EEPROM)
Type
       ConfigRpm = Record    //For Tach configuration
       Config_Name:String[5];//name of configuration file
       Param_Name   :char;//Name of paramter of which value is to be changed
       Tach_PPR:byte;//pulses per rev
       User_Max_Value:word;    //adjustable by user within Max_Value_LimitUsed for passing parmaters to adjustment of configuration values
       User_Min_Value:word;  //adjustable by user within min_value_Limit
       Max_Value_Limit:word;   //Not adjustable by user pull from EEPROM when in Adjust Mode
       Min_Value_Limit:word;   //Not adjustable by user Store to EEPROM when leaving Adjust Mode
       Rpm_Caution_Lower1:word;    //These are set by user to provide warning when operating in a restricted rpm band
       Rpm_Caution_Higher1:word;
       Rpm_Caution_Lower2:word;
       rpm_caution_Higher2:word;
       Hobbs_Limit:word; //sets the rpm above which the Hobbs keep time
       Hobbs_Start_Time:dword; // 29 May sets initial value of hobbs meter if different than zero
       Caution_LED:byte;//Flag which turns on/off Yellow Caution LED - can not turn of RED Alarm LED
       Fast_Rate   :byte; //Maximum step size of value  changed to word to accomodate 256 byte step
       Slow_Rate   :byte;  //min stemp sixe of value
       Pause       :byte;  //number of counts before pushed button clicks into fast mode

    end;
     
  const size1:byte = 24; // 19 May 2011 15 Mar 2011  20 Feb 2011 was 26 size for Rx and Hexstring
        ConvFactor = 1.401; // 19 May 2011
  Var //Variable for Emag RxBuf handling
      Txbuff1              :string[size1];Volatile;//array[size1] of char;

      
     //***********RECORD TYPE ASSIGNMENT ***************************************
     
      //Set up proper record types for keeping data - Static or Stream

      PropData1            :Static;//PropData;
      PropData2            :Static;

      conshiftData1   :Static;//:SmallRecData;
      conshiftData2   :Static;//:SmallRecData;
      
      eicad           :Static;//SmallRecData;
      
      RevData1        :Static;//:SmallRecData;
      RevData2        :static;

      Leddata1        :Static;//:smallRecData;
      LedData2        :static;
      
      pprdata1        :Static;
      pprdata2        :static;
      
      maxrpmdata1     :Static;
      Maxrpmdata2     :static;
      
      maxadvdata1     :Static;
      Maxadvdata2     :static;
      
      CpuTempData1    :Static;
      CpuTempData2    :Static;
      
      CycleData1      :Static;
      CycleData2      :Static;

     

      runtimehour1    :Static;//SmallRecData; //Text field is for hours
      runtimehour2    :Static;


//********** FOR VALUE CHANGE AND CONFIGURATION **************************
    Type
       Config = Record
       Config_Name:String[5];//name of configuration file
       Param_Name   :char;//Name of paramter of which value is to be changed
       Raw          :word;
       User_Max_Value:word;    //adjustable by user within Max_Value_LimitUsed for passing parmaters to adjustment of configuration values
       User_Min_Value:word;  //adjustable by user within min_value_Limit
       Max_Value_Limit:word;   //Not adjustable by user pull from EEPROM when in Adjust Mode
       Min_Value_Limit:word;   //Not adjustable by user Store to EEPROM when leaving Adjust Mod
       Fast_Rate   :byte {word}; //Maximum step size of value
       Slow_Rate   :byte;  //min stemp sixe of value
       Pause       :byte;  //number of counts before pushed button clicks into fast mode
       Last_used_Flag:byte;
    end;

    Type                      //Record for Intergal Type Configuration Data
      ConfigI = record
      Config_Name:String[5];
      Param_Name     :char;
      //Current_ValueI :integer; //Current value
      User_max_ValueI:integer;
      User_Min_ValueI:integer;
      Max_Value_LimitI :integer; //Max Value
      Min_Value_LimitI :integer;
      Fast_Rate   :byte; //Maximum step size of value
      Slow_Rate   :byte;  //min stemp sixe of value
      Pause       :byte;  //number of counts before pushed button clicks into fast mode
     // Last_used_Flag:byte;
   end;

 var  R_ConfigRPM:config;  //records for configurations Stored in EEPROM

      Save_RPM_Max:word;
      Save_Channel:byte;
      channel:byte;


implementation

end.
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#2 Post by Rotary_Ed » 24 Jun 2011 21:07

Forgot to load the file with the problem - here is attached file compressed.
MKP 4.8 Test.zip
(49.7 KiB) Downloaded 215 times
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: byte/word to string conversion hangs in loop

#3 Post by janni » 25 Jun 2011 02:31

Hi Ed,

Good move with adding the full project :) . This makes things much easier.

So, the problem is in length of Hexstring1 and the way conversion procedures work. As Hexstring1 can hold only 2 chars, you cannot use mE's conversion procedures - ByteToStr requires string[3], and WordToStr, string[5] as a minimum parameter. Even procedures from my replacement lib, that are supposed to use only necessary string space, will write past the end of the parameter string if the resulting string is longer than the declared one (for values greater than 99 even Word2StrN will need string[3]).

As it happens, Hexstring1 is followed in RAM by i - the loop counter in your repeat..until loop. When conversion procedure is called, the counter may be overwritten with 0 - string terminator belonging to Hexstring1.. and the loop goes forever.

You may get rid of the problem by simply declaring Hexstring1 as string[3].

And I'll have to correct my procedure Byte2str (used by Byte2strN) - apparently, trying to shorten the code, I've forgotten that it's supposed to use only the necessary string space :( . Other procedures, like Word2str, behave properly and may be used with strings as short as the 'number' parameter dictates. I'll update my libs as soon as possible.

One final warning - in the following part of code

Code: Select all

      Txbuff1 := Cmd_Value_Str;
      Lengthx := Strlen(Txbuff1);
      Txbuff1[Lengthx] := $0D;
      Config_Send_to_Pmag;
you prepare the Txbuff1 sring without adding terminating character. If you're going to call in the Config_Send_to_Pmag procedure some library functions using this string, you may be in for a nasty surprise.

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#4 Post by Rotary_Ed » 25 Jun 2011 04:09

Thanks as always, Janni, for you knowledge, understanding, and insight.

I realized as I later played with the code in the debugger than my Cmd_Value_Str was too short and made it string[3], but I missed the Hexsring1 length needing to also be 3.

I figured it must be my programming (it usually is :( ). But, I could not figure out why the Word2strN would work while the Byte2strN would not. But stepping on the index "i" with a zero would certainly do it. I need to pay more attention to address locations of variables.

Thanks for the comment on the termination of the TxBuff1 string. I do insert an Carriage Return ($0D) at the end of the string as the string is then sent over a UART link to a piece of equipment by Config_To_Pmag that requires the CR as the termination character. I am not certain what would happen if I stuck a "Zero" in there to terminate the string and then added the CR.

In any case, while Word2strN(Value,HExstring1,2) worked - I always dislike workarounds without understanding why they work or what the root problems is. Sigh, in this case just a too short string variable was the cause of my problem

As always, appreciate your time and help.
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#5 Post by Rotary_Ed » 25 Jun 2011 04:13

Yep, Janni, I made the Hexstring1 a string of 3 and the problem was solved and the code runs fine with byte to string conversions.

Making corrections is not the hard part - is knowing where to make the corrections :D
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: byte/word to string conversion hangs in loop

#6 Post by janni » 25 Jun 2011 14:32

Rotary_Ed wrote:I figured it must be my programming (it usually is :( ). But, I could not figure out why the Word2strN would work while the Byte2strN would not..
Well, here the fault was mine. I assumed that both Byte2strN and Word2strN should use only necessary string space (also internally), but failed to ascertain it for Byte2strN. New version of Conversions library replacement is ready - whole package may be downloaded from here.
Thanks for the comment on the termination of the TxBuff1 string. I do insert an Carriage Return ($0D) at the end of the string as the string is then sent over a UART link to a piece of equipment by Config_To_Pmag that requires the CR as the termination character. I am not certain what would happen if I stuck a "Zero" in there to terminate the string and then added the CR.
Naturally, if added, the termination char should follow CR, not preceed it. And it's only important if you use the string with a library procedure as a parameter string. Such procedure would expect the termination char and could get lost without it. As long as you process the string by yourself, knowing what's in it, or just use single chars, there'll be no problem.

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#7 Post by Rotary_Ed » 25 Jun 2011 14:49

Duh! :oops: of course, add the termination at the end of the string which would naturally include the $0D carrage return. That should not cause any problem as the receiving unit will simply stop processing the serial string when it reaches the $0D.

Downloaded your Library with the Byte2N "adjustment". Thanks again, Janni, your sharing of your Library has made my programming my OLED display soooo much easier.
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: byte/word to string conversion hangs in loop

#8 Post by janni » 25 Jun 2011 16:36

Rotary_Ed wrote: That should not cause any problem as the receiving unit will simply stop processing the serial string when it reaches the $0D.
Actually, the termination char is not there to be sent anywhere. It only says - stop processing the string, it has already ended :wink: .

Just out of curiosity, what type of OLED display you use?

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#9 Post by Rotary_Ed » 25 Jun 2011 17:32

Ah, all the nuiances? I seem to miss :?

I have been using an 1.5" diagonal, full color, OLED with designation

OSD 2828OGDEDF01 for a number of years. My customers really like them because of the high contrast (brilliant colors) and no viewing angle limitation like an LCD - I use it in my 2 1/4" Aircraft Instrument case gadgets - I like it because it has some graphic primitives (rectangle in particular) that permits me to scale to all size Font from one font set - really saves on memory. I purchased 250 at a price of $9.00 back a few years ago.

Here is a link to some documentation on the Display

http://www.texim-europe.com/promotion/1 ... dedf01.pdf
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

joseLB
Posts: 444
Joined: 02 Apr 2006 05:56
Location: Riode Janeiro, Brasil

Re: byte/word to string conversion hangs in loop

#10 Post by joseLB » 02 Jul 2011 03:09

[quote="Rotary_Ed"]... I am not certain what would happen if I stuck a "Zero" in there to terminate the string and then added the CR. ...[quote]Hi Rotary_Ed
One extra care... if by any reason you transmit zero (binary %00000000) thru serial, normaly the PC serial driver will interpret it as XON-XOFF and so, you will loose chars transmited in betwen.
Jose

Rotary_Ed
Posts: 756
Joined: 26 Dec 2004 23:10
Location: Matthews, NC, USA
Contact:

Re: byte/word to string conversion hangs in loop

#11 Post by Rotary_Ed » 02 Jul 2011 12:26

Thanks, Jose

I was totally unaware of that information. I do use my PC to emulate communication with the system my project is connected to - and a binary zero would have been the source of endless frustration trying to figure out what was wrong :? .

Appreciate your sharing this information :D
Rotary_Ed
Matthews, NC USA
Rv-6A N494BW Rotary Powered

Post Reply

Return to “mikroPascal PRO for PIC Beta Testing”