Hello,
I need to realize file transfer between two processors via USB. At the
SLAVE terminal, I used the g_serial module to simulate a virtual USB serial
port, and the parameters was chosen as transmission via BULK, while in the
HOST terminal, i loaded an USBSERIAL.ko module, and it can be identified
normally. Now the terminals can communicate with each other. However, I
think there are still some problems:
1. In my code, is it adopts BULK or serial port to transfer? If the BULK is
not used to transfer, how to write the code?
2. I found that it wrong when sending data, for example, I would like to
send a file with 1M bytes, but only 10K bytes or less data can be received
at the other side.
Thank you!
Zhou Sheng
send.c
#include "unistd.h"
#include "fcntl.h"
#include "stdio.h"
#include "sys/types.h"
#include "errno.h"
#include "string.h"
#include <sys/stat.h> /**/
#include <termios.h>
//#include <linux/tty_driver.h>
#define BUFFER_SIZE 64
#define FALSE 0
#define TRUE 1
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main(int argc,char **argv)
{
int from_fd,to_fd,i,j,total_bytes=0;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
char *dev ="/dev/ttygserial";
struct termios newtio;
to_fd = OpenDev(dev);
if (to_fd>0)
{
printf("to_fd open succeed\n");
}
else
{
printf("Can't Open Serial Port!\n");
return 0;
}
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
newtio.c_oflag &= ~OPOST; /*raw output*/
tcflush(to_fd,TCIFLUSH);//clear input buffer
//newtio.c_cc[VTIME] = 100; /* inter-character timer unused */
//newtio.c_cc[VMIN] = 0; /* blocking read until 0 character arrives
*/
tcsetattr(to_fd,TCSANOW,&newtio);
if((from_fd=open("/home/123.jpg",O_RDONLY))==-1)
{
printf("Open Error: %s\n",strerror(errno));
return 0;
}
i=0;
total_bytes = 0;
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
total_bytes += bytes_read;
++i;
//printf("in the %d read,we get %d bytes\n",i,bytes_read);
if((bytes_read==-1)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
ptr=buffer;
j=0;
wait_until_sent(to_fd,1);
while(bytes_write=write(to_fd,ptr,bytes_read))
{
++j;
//printf("in this %d read cycle we write %d bytes in %d
write\n",i,bytes_write,j);
if((bytes_write==-1)&&(errno!=EINTR))
break;
else if(bytes_write==bytes_read)
break;
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1)
break;
}
//printf("input a key to continue...\n");
//getchar();
}
printf("we send %d bytes all\n",total_bytes);
close(from_fd);
close(to_fd);
exit(0);
}
receive.c
#include "unistd.h"
#include "fcntl.h"
#include "stdio.h"
#include "sys/types.h"
#include "errno.h"
#include "string.h"
#include <sys/stat.h>
#include <termios.h>
#define BUFFER_SIZE 64
#define FALSE 0
#define TRUE 1
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDONLY); // | O_NOCTTY| O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main()
{
int from_fd,to_fd,i,j,total_bytes;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE*64];
char *ptr;
char *dev ="/dev/tts/USB0";
struct termios newtio;
from_fd = OpenDev(dev);
if (from_fd>0)
{
printf("form_fd open succeed\n");
}
else
{
printf("Can't Open Serial Port!\n");
return 0;
}
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
newtio.c_oflag &= ~OPOST; /*raw output*/
tcflush(from_fd,TCIFLUSH);//clear input buffer
newtio.c_cc[VTIME] = 100; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */
tcsetattr(from_fd,TCSANOW,&newtio);
if((to_fd=open("/home/456.jpg",O_WRONLY|O_CREAT,10600))==-1)
{
printf("Open %s Error:\n",strerror(errno));
return 0;
}
total_bytes = 0;
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
total_bytes += bytes_read;
/*
if((bytes_read==-1)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
//printf("in %d read, we get %d bytes form /dev/tty\n",i,bytes_read);
//bytes_write=write(to_fd,buffer,bytes_read);
}
*/
}
printf("recieve over! we recieved %d bytes total\n",total_bytes);
close(from_fd);
close(to_fd);
return 1;
}
_______________________________________________
uClinux-dev mailing list
[email protected]
http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
This message was resent by [email protected]
To unsubscribe see:
http://mailman.uclinux.org/mailman/options/uclinux-dev