Hello
I have little problem with a code.
I want to send information about a size on a file and uses “size_t filesize”.
I will send this information thru a network called ”controller area Network” and uses the code
stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
in order to do this.
The problem is that ”size” in the code only may last 8 byte. Is there somewhat better way to type the code below?
void send_messege(void)
{
int hnd,stat;
unsigned char data[8];
unsigned int size, i=0;
unsigned char* buf = 0;
FILE* fp;
//***************************************************
//********* Read in the entire file to one buffer **********
//***************************************************
size_t filesize = 0;
// Open the sound file
fp = fopen("test.mp3","rb");
// Find the size and allocate the buffer
fseek(fp,0,SEEK_END);
filesize = ftell(fp);
// Go to beginning of the file
fseek(fp,0,SEEK_SET);
// Allocate buffer memory
buf = malloc(filesize);
// Read in to buffer
fread(buf,1,filesize,fp);
// close the file
fclose(fp);
// Open up the bus on controller are network
hnd = InitCtrl(second);
/****************************************************
*****************************************************
********* Send away INFO about the file's size **********
*****************************************************
****************************************************/
size=filesize;
// send message
stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
if (stat < 0)
{
printf("Failed, status == %d\n", stat);
}
stat=canWriteSync(hnd, 500);
/****************************************************
*****************************************************
******* Divide up the file about 8 byte and send it *******
*****************************************************
****************************************************/
while (i < filesize)
{
data[0]=buf[i];
data[1]=buf[i++];
data[2]=buf[i++];
data[3]=buf[i++];
data[4]=buf[i++];
data[5]=buf[i++];
data[6]=buf[i++];
data[7]=buf[i++];
//send data
stat = canWrite(hnd, 1234, data, 8, canMSG_EXT);
if (stat < 0)
{
printf("Failed, status == %d\n", stat);
}
stat=canWriteSync(hnd, 500);
i++;
}
//close bus
canBusOff(hnd);
canClose(hnd);
}