# [SOLVED]commande free in mplab

Status
Not open for further replies.

#### dido1987

##### Member level 2
When I wants to send data by usb towards the pic 18f4550 a pointer " Buffer " is going to point towards an address memory and to store it then if I want to send data of length different I have to delete the previous data stored in memory I have to try the following commands under Mplab:
Buffer [0] = '\0 ';
also i try this
For (j=0; j < strlen ( Buffer); j ++)
{ The Var [j] =Buffer [j];
Buffer [ j ] = '\0 ';
}
But that did not work, then I made the command
( Buffer) free;
by noting that I added libraries
*include stdio.h >
*include string.h >
*include stdlib.h >
Then the following error show
Copyright ( c ) 2007 Microchip Technology Inc.
Error - could not find definition of symbol ' free ' in line ' C:\Users\Administrateur\Desktop\hid\HID \user.o '.
Errors: 1 "
If somebody can help me and thank you in advance.

#### btbass

Why do you need to delete the contents of the buffer?
You could just over write them with the new data and send that.

free uses memory that is allocated on the heap. You would have to read your compiler docs to see what memory management routines it supports.

#### betwixt

##### Super Moderator
Staff member
As you are using 'strlen' I assume you are sending text strings from the buffer. In that case, the string functions use NUL (0x00) as the string terminator as is normal in the C language. All you have to do is put the NUL into the first string location so the terminator is right at the start. You can do this with either "Buffer[0] = 0;" or "*Buffer = 0;". The buffer will not be empty but it will have a contents length of zero.

Brian.

#### dido1987

##### Member level 2
it's just that am using a vb.net application to send a text with different length using USB and pic 18F4550 so when i send for exemple helloworld my LCD shows helloworld but when i send a smaller text like day it shows dayloworld you see what's the problem and am using strlen and i try with Buffer[0]=NULL; and Buffer[0]='\0'; but it dosen't work

#### btbass

Your function that writes to the lcd should detect the string null terminator.
Do you clear the display before writing to it?

#### dido1987

##### Member level 2
when i try to do it with my application every thing stop's and i have to shut down the application and the simulation in the ISIS

#### betwixt

##### Super Moderator
Staff member
I think the problem is that you are simply sending "Buffer" to the LCD again without realizing there is a second buffer in the LCD module. What you need to do is clear the LCD (or at least the part you want to overwrite) before writing your buffer to it again.

Another trick that might help is that when you write data into your buffer add a NUL to the end of it each time. This will ensure a terminating character is 'pushed' along the buffer by new data being written into it. For example:
(this is pseudo code, convert it to C if necessary)
Code:
while(j < MaxBufferSize)
{
Buffer[j++] = MyCharacter;   //write character into the buffer and advance the pointer to the next location
Buffer[j] = 0;  //write a terminator in the next free location, it will be overwritten by the next character arriving.
}
"J" holds the current write pointer and the next location to it will always a NUL in it.

Brian.

#### dido1987

##### Member level 2
this is my program:
for the declaration
"#include <p18cxxx.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <usart.h>

#include "C:\Users\Administrateur\Desktop\hid\HID\io_cfg.h" // I/O pin mapping

#define ON 1
#define OFF 0
/** V A R I A B L E S ********************************************************/
#pragma udata
char Buffer[64];
char var[] ;"

and the process:
"
void ProcessIO(void)
{
int i;
int j;
int nb;
int lnvar ;

u8 counter=0 ;
u8 LCD_buffer[16];
pr:
init_lcd();
LCD_BACKLIGHT=ON;
while(1)
{ i=0;
j=0;
if (HIDRxReport(Buffer,PacketSize ) > 0) // USB receive buffer has data
{
lcd_gotoxy(0,1);
fprintf(_H_USER," ");

strcpy(var,Buffer);
Buffer[0]=NULL;
switch (var[1])
{
case 'i':
PORTB=0x00;

break;
case 'a':
PORTB=0x80;

break;
case 'r':
PORTB=0x40;

break;
case 'l':
PORTB=0x20;
break;

}// fin switch
bn :
for(i=0 ; i<=lnvar ; i++)
{ lcd_gotoxy(0,0);
fprintf(_H_USER,"%d %s",lnvar,var);
lcd_gotoxy(16-i,1);
fprintf(_H_USER,"%s",var);
delay_ms(30);

if(i==lnvar)
{
i==0;
lcd_gotoxy(0,1);
fprintf(_H_USER," ");
// goto pr;
if (HIDRxReport(Buffer,PacketSize ) > 0)
goto pr;
else
goto bn;}

}
}

// goto boucle; }
}//end while

}//end ProcessIO
"
when i call this fonction if (HIDRxReport(Buffer,PacketSize ) > 0) this is the code in hid.c void
HIDTxReport(char *buffer, byte len)
{
byte i;

/*
* Value of len should be equal to or smaller than HID_INT_IN_EP_SIZE.
* This check forces the value of len to meet the precondition.
*/
if(len > HID_INT_IN_EP_SIZE)
len = HID_INT_IN_EP_SIZE;

/*
* Copy data from user's buffer to dual-ram buffer
*/
for (i = 0; i < len; i++)
hid_report_in = buffer;

HID_BD_IN.Cnt = len;

}//end HIDTxReport

---------- Post added at 10:19 ---------- Previous post was at 10:01 ----------

i find a solution
in my vb.net application i have to put BufferOut(index) = 0
like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Select Case ComboBox1.Text
Case "bloquant"

BufferOut(1) = Asc("b")
BufferOut(2) = Asc("l")
BufferOut(3) = Asc("o")
BufferOut(4) = Asc("q")
BufferOut(5) = Asc("u")
BufferOut(6) = Asc("a")
BufferOut(7) = Asc("n")
BufferOut(8) = Asc("t")
BufferOut(9) = Asc("*")
BufferOut(10) = Asc("t")
BufferOut(11) = Asc("e")
BufferOut(12) = Asc("s")
BufferOut(13) = Asc("t")
BufferOut(14) = 0
Call WriteSomeData()
and it works
thinks any way

Status
Not open for further replies.