CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Please help with PIC & VB

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gIO
Guest







Please help with PIC & VB
PostPosted: Mon Jun 09, 2008 7:28 am     Reply with quote

I have a VB application that send strings via rs232:
Code:

Dim Control As Byte
   
  On Error GoTo err_handler
  If fstep = 0 Then Exit Sub
  Control = trans ' 1 when tranmission mode, else 0
 
  Dim fout As Long
  fout = sFMin * DDSMult
  MSComm1.Output = Str(Control) & Chr(13)     'control
  Sleep (1)
  MSComm1.Output = Str(fout) & Chr(13)        'Frequency start
  Sleep (1)
  MSComm1.Output = Str(lSteps) & Chr(13)      'number of samples
  Sleep (1)
 
  Dim passo1 As Long
  passo1 = fstep * DDSMult
  MSComm1.Output = Str(passo1) & Chr(13)      'step size
  Sleep (1)

 


My PIC application read rs232 via USB CDC driver:
Code:


char  Strtmp[15];
char *ptr;

      get_string_usb(Strtmp, 15);             // get string
      glcd_text35(10, 20, Strtmp, ON);     // diplay string on GLCD
      Control = atoi32(Strtmp);               // conversion to i32
      Disp_num(50,20,control, 0);           //  display formatted number

      get_string_usb(Strtmp, 15);
      glcd_text35(10, 30, Strtmp, ON);
      //TW = atoi32(Strtmp);
      TW = strtoul(strtmp,&ptr,10);
      Disp_num(50,30,TW, 0);

      get_string_usb(Strtmp, 15);
      glcd_text35(10, 40, Strtmp, ON);
      Samples = atoi32(Strtmp);
      Disp_num(50,40,Samples, 0);

      get_string_usb(Strtmp, 15);
      glcd_text35(10, 50, Strtmp, ON);
      DDSStep = atoi32(Strtmp);
      Disp_num(50,50,DDSStep, 0);



Data on GLCD (glcd_text35) seem to be ok but result of atoi32 is always = 0

I have tried also with result = strtoul(strtmp,&ptr,10);
In this case result is a wrong number


Can you help me ? Thank so much!
Giorgio
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Mon Jun 09, 2008 11:45 am     Reply with quote

Greetings gIO,

Please provide an example of the actual data, expected response, and results of your program. This will more than likely narrow where the problem lies.

Have you tried to examining the data received?

If memory serves me correctly, the atoi() function requires a pointer to a null terminated sequence of chars. Perhaps your array is not null terminated? The result of atoi() conversion is not defined for char sequences which cannot be converted.


Cheers,
JMA
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jun 09, 2008 4:47 pm     Reply with quote

What is your compiler version number?

What does the declaration of Control, TW, Samples and DDSStep look like?
Guest








PostPosted: Tue Jun 10, 2008 12:28 am     Reply with quote

I use CCS version 3.249.

Declaration are:
int32 Control;
int32 TW; // Tuning Word start for AD9851
int32 Samples; // number of samples
int32 DDSStep; // step size
char Strtmp[15];

I have also tried using hyperterminal for send data to pic.
In this case result of atoi32() is OK.

Giorgio
gIO
Guest







Any Help ?
PostPosted: Thu Jun 12, 2008 2:29 am     Reply with quote

I have also tried to add a \0 at the end of the strings but the result is the same, string is display correctely but result of atoi32() is always 0:

Code:


int32 Control;
int32 TW; // Tuning Word start for AD9851
int32 Samples; // number of samples
int32 DDSStep; // step size
char Strtmp[15];

         get_string_usb(Strtmp, 15);
         Strtmp[14] = '\0';
         glcd_text35(10, 30, Strtmp, ON);
         TW = atoi32(Strtmp);
         Disp_num(50,30,TW, 0);

         get_string_usb(Strtmp, 15);
         Strtmp[14] = '\0';
         glcd_text35(10, 40, Strtmp, ON);
         Samples = atoi32(Strtmp);
         Disp_num(50,40,Samples, 0);

         get_string_usb(Strtmp, 15);
         Strtmp[14] = '\0';
         glcd_text35(10, 50, Strtmp, ON);
         DDSStep = atoi32(Strtmp);
         Disp_num(50,50,DDSStep, 0);



Any help please ?

Giorgio
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jun 12, 2008 9:30 am     Reply with quote

glcd_text35() is an unknown function to me. The CCS supplied glcd driver has a function glcd_text57, I assume you are using a modified version?

Disp_num(0 is another unknown function to me.

Try to narrow down the problem by splitting the program:
1) Is the error in the received data?
or
2) Is the Disp_num() function not working as expected?

Case 1 can be tested by printing the hexadecimal values of the received data.
Case 2 can be tested by defining a hard coded value in your program and feeding this to the function like:
Code:
TW = 12345678;
Disp_num(50,30,TW, 0);



Can you post the code of Disp_num?
gIO
Guest







PostPosted: Fri Jun 13, 2008 2:13 am     Reply with quote

Tnx to all, I have found the problem.

Th VB istruction :

MSComm1.Output = Str(Control) & Chr(13)

store a 0x20 to begin of string and atoi32() convert to 0

I have modified my program adding a memmove() to bypass first byte.

Greeting, Giorgio

p.s.: glcd_text35() is a modified version for 3x5 pixel

Disp_num is this:
Code:

//------------------------------------------------------
void Disp_num (char col, char row, int32 num, int1 kilo)
//------------------------------------------------------
{
   //int8  ColEnd;
   int16 NumM;
   int16 NumK;
   int16 NumU;
   char toPrint[12];

   if (kilo)  num = num/1000;          // dispaly in khz

                                                        // num                      28.525.199
   NumM = num / 1000000;                // NumM=display       28
   NumK = (num % 1000000) / 1000;  // NumK=display       525
   NumU = num % 1000;                    // NumU=display       199

   if (kilo)
      {
      if (num > 999)
         sprintf(toPrint, " %3lu.%03lu", NumK, NumU);
      else
         sprintf(toPrint, "     %3lu", NumU);

      toPrint[8] = '\0';                              // Limit shown digits
      }
    else
      {
      if (num > 999999)
         sprintf(toPrint, "%3lu.%03lu.%03lu", NumM, NumK, NumU);
      else if (num > 999)
         sprintf(toPrint, "    %3lu.%03lu", NumK, NumU);
      else
         sprintf(toPrint, "        %3lu", NumU);

      toPrint[11] = '\0';                              // Limit shown digits to 11
      }

   glcd_text35(col, row, toPrint, ON);

}
Steve H
Guest







PostPosted: Fri Jun 13, 2008 9:28 am     Reply with quote

Oh yeah - should have seen that. Str() when it converts a number leaves a space for a minus sign, even if it does not use it.

You can also fix this on the VB side by using Trim(). Trim() removes white space from the beginning and end of strings.

I use Trim() all the time to solve this.

Building your own Format() is also an option, but I think Trim(Str(num)) is probably faster.

HTH - Steve H.
gIO
Guest







PostPosted: Mon Jun 16, 2008 4:21 am     Reply with quote

Tnx Steve H for info.

The VB application is not mine and I am not able to work with it.

Important is know and resolved the problem.

Bye
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group