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);
}>>stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
what is the 1234 and 8 for? sizeof(size_t), which is an unsigned int, if 4 on most 32-bit computers, not 8.
>Is there somewhat better way to type the code below?
Are you simply asking for advice on working code, or are you having problems with it? I can see areas where mixing and matching types (both ranges and signedness) may get you in trouble.
If all you want is ways to improve the code then to simplify matters, I would get rid of data and just use a slice of buf:
for ( i = 0; i < filesize; i += 8 ) {
stat = canWrite ( hnd, 1234, buf + i, 8, canMSG_EXT );
if ( stat < 0 )
printf ( "Failed, status == %d\n", stat );
stat = canWriteSync ( hnd, 500 );
} And you can refactor that while writing piece because everything is the same except for the pointer to your eight bytes:
int networkWrite ( unsigned char *start )
{
if ( canWrite ( hnd, 1234, start, 8, canMSG_EXT ) < 0 )
printf ( "Failed, status == %d\n", stat );
return canWriteSync ( hnd, 500 );
} Then the two blocks of code become:
/* Send away INFO about the file's size */
size = filesize;
stat = networkWrite ( size );
/* Divide up the file about 8 byte and send it */
for ( i = 0; i < filesize; i += 8 )
stat = networkWrite ( buf + i ); However, since size is an unsigned int, you have an issue if int isn't an 8-byte quantity. If that's the case, you need to pad it. The same goes for if the file size isn't evenly divisible by eight. You can bring data back into the picture for this:
/* Send away INFO about the file's size */
memset ( data, 0, 8 );
memcpy ( data, size, sizeof size );
stat = networkWrite ( data );
/* Divide up the file about 8 byte and send it */
for ( i = 0; i < filesize; i += 8 ) {
size_t remaining = filesize - i;
size_t n = 8;
if ( remaining < 8 )
n = remaining;
memset ( data, 0, 8 );
memcpy ( data, buf + i, n );
stat = networkWrite ( data );
} Beyond that you'll have to specify what you need help with.
Hello again.
Thanks a lot for these codes. They have helped a lot.
I have typed 2 codes, one in order to send a file via controller area Network and a code in order to receive the file.
However, there are 2 problems with the code in order to receive the file.
1: Stack around the variable buf was corrupted
2: I believe that the file that to be sent is ok, but when I receive the file and then saved it on the hard disk. It has same size that the original file. But it is corrupted.
Some proposals on how to solve this?
The code to send the file
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include <canlib.h>
//global value
int bitrate = BAUD_125K;
int first = 0; // channel 1
int second = 1; // channel 2
void Check(char* id, canStatus stat)
{
char buf[50];
if (stat != canOK)
{
buf[0] = '\0';
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
int InitCtrl(int ctrl)
{
int stat;
int hnd;
//open the channel
printf("canOpenChannel, channel %d... ", ctrl);
hnd = canOpenChannel(ctrl, canOPEN_REQUIRE_EXTENDED);
if (hnd < 0)
{
Check("canOpenChannel", (canStatus)hnd);
exit(1);
}
printf("OK.\n");
printf("Setting the bus speed...");
stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
if (stat < 0)
{
printf("canSetBusParams failed, stat=%d\n", stat);
}
printf("OK.\n");
//Go online on the bus
printf("Go bus-on...");
stat = canBusOn(hnd);
if (stat < 0)
{
printf("canBusOn failed, stat=%d\n", stat);
}
printf("OK.\n");
return hnd;
}
void send_messege(void)
{
int hnd,stat;
unsigned char data[8];
signed int *size, i,k=0;
unsigned char* buf = 0;
FILE* fp;
/*********Read the file to buffer**********/
size_t filesize = 0;
// Open the file
fp = fopen("mat.rar","rb");
// get the size of the file and allocate input buffer space
fseek(fp,0,SEEK_END);
filesize = ftell(fp);
// go back to beginning of file
fseek(fp,0,SEEK_SET);
// allocate input buffer
buf = malloc(filesize);
// read into buffer
fread(buf,filesize,1,fp);
//close the file
fclose(fp);
//Öppna upp busen
hnd = InitCtrl(second);
/* Send away INFO about the file's size */
size=filesize;
//Send messege
stat = canWrite(hnd, 1234, &size, 4, 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
1234 = ID, buf +n=data that is send, n=how many byte to send(max=8),
*/
for ( i = 0; i <= filesize; i += 8 )
{
size_t remaining = filesize - i;
size_t n = 8;
if ( remaining < 8 )
{
n = remaining;
k = n+1;
stat = canWrite ( hnd, 1234, buf +k, n, canMSG_EXT );
}
else
stat = canWrite ( hnd, 1234, buf + i, n, canMSG_EXT );
}
//Close the bus
canBusOff(hnd);
canClose(hnd);
}
void main()
{
canStatus stat;
canInitializeLibrary();
send_messege();
}
And the code to receive
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include <canlib.h>
//global value
int bitrate = BAUD_125K;
int first = 0; // channel 1
int second = 1; // channel 2
void Check(char* id, canStatus stat)
{
char buf[50];
if (stat != canOK)
{
buf[0] = '\0';
canGetErrorText(stat, buf, sizeof(buf));
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
int InitCtrl(int ctrl)
{
int stat;
int hnd;
//open the channel
printf("canOpenChannel, channel %d... ", ctrl);
hnd = canOpenChannel(ctrl, canOPEN_REQUIRE_EXTENDED);
if (hnd < 0)
{
Check("canOpenChannel", (canStatus)hnd);
exit(1);
}
printf("OK.\n");
printf("Setting the bus speed...");
stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
if (stat < 0)
{
printf("canSetBusParams failed, stat=%d\n", stat);
}
printf("OK.\n");
//Go online on the bus
printf("Go bus-on...");
stat = canBusOn(hnd);
if (stat < 0)
{
printf("canBusOn failed, stat=%d\n", stat);
}
printf("OK.\n");
return hnd;
}
void read_messege(void)
{
long id;
unsigned int dlc,flags;
unsigned long timestamp,timeout;
int hnd;
int stat,i=0;
char *buf ;
signed int *size;
size_t filesize;
FILE* fp;
hnd = InitCtrl(first);
/* Read INFO about the file's size
hnd=handshake, &size=data that i resived, &dlc=how many byte that is resived
*/
stat=canReadWait(hnd, &id, &size, &dlc, &flags, ×tamp, &timeout);
stat=canReadSync(hnd, 500);
filesize = size;
/*Store data to the buffer*/
for ( i = 0; i < filesize; i += 8 )
{
size_t remaining = filesize - i;
size_t n = 8;
if ( remaining < 8 )
{
n = remaining+i;
stat=canReadWait(hnd, &id, &buf + n, &dlc, &flags, ×tamp, &timeout);
stat=canReadSync(hnd, 500);
}
else
{
stat=canReadWait(hnd, &id, &buf + i, &dlc, &flags, ×tamp, &timeout);
stat=canReadSync(hnd, 500);
}
}
//Close the bus
canBusOff(hnd);
canClose(hnd);
/*write data to file*/
fp = fopen("matt.rar","wb");
fwrite(&buf,filesize,1,fp);
fclose(fp);
}
void main()
{
canStatus stat;
canInitializeLibrary();
read_messege();
return 0;
}>> Stack around the variable buf was corrupted
That means the program has trashed the stack somewhere. That's a tough one to find. Check for buffer overruns, writing beyond the end of a buffer or array, using uninitialized pointers, and indexing into non-existant array elements. If you still can't find it then start commenting out blocks of code until the problem disappears. When that happens you have probably found the deamon.