Hi,
I am trying to pass float data from 1.0 to 200.0 in increment of 1.0
(for testing, actual data will be real float numbers)
through usb gadget serial drive of beagle board to windows xp host
speed is 115200 and 8N1 is the protocol
I find that data 1.0 to 137.0 is received on the Windows perfectly but
after that data suddenly becomes wrong and extra character is added in
the receiving side and the actual data again starts coming but now the
order of characters received is lost and wrong float data is
interpreted.....
I will explain how :
for 137 on receiver side I get : (00 00 09 43) as float in hex in
reverse order
for 138 i suddenly get : (00 00 0D 0A) in place of (00 00 0A 43)
and then 139 I get : (43 00 00 0B) --- in place of (00 00 0B 43)
and then 140 I get : (43 00 00 0C) ... in place of (00 00 0C 43) ...
so the track is lost due to corrupt data being added during 138 i.e.
0D
Please help if some one has received this problem earlier
This is the code :
///////////////////////////////////////////// main program
begins /////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
//clrscr();
int N = 4000000;
char *ptr;
float *x;
int i,bytes_written;
float j=1.0;
double cpu_time_used;
clock_t start,end;
size_t len = 0;
x = (float*) malloc(sizeof(float)*N);
*x = 1.0;
for (i=0;i<=(N/4 - 1);i++)
{
*(x+i)=j;
j = j+1.0;
}
len = (N/5000);
ptr=(char*)x;
x = (float*)ptr;
serial_port_open();
// Assign a handler to close the serial port on Ctrl+C.
signal (SIGINT, (void*)sigint_handler);
start = clock();
//for(i=0; i<=2; i++)
//{
bytes_written = write(serial_port, ptr, len);
if (bytes_written < len)
printf("Write failed %d\n", i);
//ptr = ptr + 400;
//usleep(10000);
//}
end = clock();
cpu_time_used = ((double) (end - start));
printf("cpu time used = %lf",cpu_time_used);
printf("successful transfer operation");
return 0;
free (x);
}
//////////////////////////////////////////////// main program
ends /////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Resets the terminal and closes the serial port.
void serial_port_close()
{
tcsetattr(serial_port,TCSANOW,&options_original);
close(serial_port);
}
// Opens a USB virtual serial port at ttyUSB0.
//
// returns - the port's file descriptor or -1 on error.
int serial_port_open(void)
{
struct termios options;
serial_port = open(PORT_NAME, O_RDWR | O_NONBLOCK);
if (serial_port != -1)
{
printf("Serial Port open\n");
tcgetattr(serial_port,&options_original);
bzero(&options, sizeof(options));
tcgetattr(serial_port, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag |= ICANON;
tcsetattr(serial_port, TCSANOW, &options);
}
else
printf("Unable to open /dev/ttyGS0\n");
return (serial_port);
}
// Attempts to read up to 10 bytes from the serial port.
// If data was read, calls a routine to examine the received data
// and take action.
// *read_buffer - the buffer that will contain the data read.
// returns - 0 if data was received, -1 if no data received.
int serial_port_read(char *read_buffer)
{
if ( read(serial_port, read_buffer, MAX_COMMAND_LENGTH) > 0)
{
return 0;
}
else
return -1;
}
// Writes data to the port.
// Parameter: write_buffer - the data to write to the port.
// *write_buffer - the buffer that contains the data to write.
void serial_port_write(char *write_buffer)
{
int bytes_written;
size_t len = 0;
len = strlen(write_buffer);
bytes_written = write(serial_port, write_buffer, len);
if (bytes_written < len)
printf("Write failed \n");
}
// Executes when the user presses Ctrl+C.
// Closes the port, resets the terminal, and exits the program.
void sigint_handler(int sig)
{
serial_port_close();
exit (sig);
}