hi all,
I am new to C and linux
I wrote a C serial port program in linux to read the .txt file from ttyusb and to save this to a another .txt file. My program doesn't work as i expected
There is a text file named "sample.txt" which contains the following text
sample.txt
hi
welcome
to
oviya technologies
bangalore
I want to send this file through ttyUSB0, to another PC, there i am having a receiver program to receive this data and write it to a file called "zname.txt". In the receiver side i cant able to receive the data fully
For your reference i have attached the
send.c and receive.c code and also the
output i am getting, can any one tell me what is wrong in this?
OUTPUT
./send
root@oviserver-desktop:/home/oviserver/shanmugam# ./send
Oviya 3
enter write
Start send
FIle sent successfully 44 bytes
./receive
oviserver@oviserver-System-Product-Name:/home/sathish$ ./receive
oviya :3
1 h
1 i
1
1 w
1 e
1 l
1 c
1 o
1 m
1 e
1
1 t
1 o
1
SEND.C
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1
FILE *file;
int fileLen;
char *tmpbuffer;
void openport(void);
void readport(void);
void sendport(void);
int fd=0;
struct termios oldtp, newtp;
char buffer[512];
void main()
{
openport();
sleep(1);
sendport();
}
void sendport(void)
{
printf("enter write\n");
int n;
file = fopen("sample.txt", "r");
fseek(file, 0, SEEK_END);
fileLen = ftell(file);
fseek(file, 0, SEEK_SET);
tmpbuffer = (char *)malloc(fileLen + 1);
printf("Start send\n");
fread(tmpbuffer, fileLen, 1, file);
fclose(file);
n = write(fd, tmpbuffer, fileLen + 1);
if (n < 0)
{
fputs("write() of bytes failed!\n", stderr);
}
else
{
printf("Image sent successfully %d\n",n);
}
close(fd);
}
void openport(void)
{
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY |O_NDELAY );
printf("Oviya %d\n",fd);
if (fd <0)
{
perror(MODEMDEVICE); }
fcntl(fd,F_SETFL,0);
tcgetattr(fd,&oldtp);
bzero(&newtp, sizeof(newtp));
newtp.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtp.c_iflag = IGNPAR | ICRNL;
newtp.c_oflag = 0;
newtp.c_lflag = ICANON;
newtp.c_cc[VINTR] = 0;
newtp.c_cc[VQUIT] = 0;
newtp.c_cc[VERASE] = 0;
newtp.c_cc[VKILL] = 0;
newtp.c_cc[VEOF] = 0;
newtp.c_cc[VTIME] = 0;
newtp.c_cc[VMIN] = 1;
newtp.c_cc[VSWTC] = 0;
newtp.c_cc[VSTART] = 0;
newtp.c_cc[VSTOP] = 0;
newtp.c_cc[VSUSP] = 0;
newtp.c_cc[VEOL] = 0;
newtp.c_cc[VREPRINT] = 0;
newtp.c_cc[VDISCARD] = 0;
newtp.c_cc[VWERASE] = 0;
newtp.c_cc[VLNEXT] = 0;
newtp.c_cc[VEOL2] = 0;
}
RECEIVE.c
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1
void openport(void);
void sendport(void);
void readport(void);
int fd=0, n;
static int cnt, size, s_cnt;
unsigned char *var;
struct termios oldtp, newtp;
char sendcmd1[10]="\0";
FILE *file;
void readport(void)
{
unsigned char buff;
file = fopen( "zname.txt", "w+" );
while (1) {
n = read(fd, &buff, 1);
if (n == -1) switch(errno) {
case EAGAIN:
continue;
default: goto quit;
}
if (n ==0) break;
fputc(buff, file);
printf("%d %c\n", n,buff);
}
quit:
fclose (file);
}
void openport(void)
{
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY |O_NDELAY );
printf("oviya :%d\n",fd);
if (fd <0)
{
perror(MODEMDEVICE);
}
fcntl(fd,F_SETFL,0);
tcgetattr(fd,&oldtp);
bzero(&newtp, sizeof(newtp));
newtp.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtp.c_iflag = IGNPAR | ICRNL;
newtp.c_oflag = 0;
newtp.c_lflag = ICANON;
newtp.c_cc[VINTR] = 0;
newtp.c_cc[VQUIT] = 0;
newtp.c_cc[VERASE] = 0;
newtp.c_cc[VKILL] = 0;
newtp.c_cc[VTIME] = 0;
newtp.c_cc[VMIN] = 0;
newtp.c_cc[VSWTC] = 0;
newtp.c_cc[VSTART] = 0;
newtp.c_cc[VSTOP] = 0;
newtp.c_cc[VSUSP] = 0;
newtp.c_cc[VEOL] = 0;
newtp.c_cc[VREPRINT] = 0;
newtp.c_cc[VDISCARD] = 0;
newtp.c_cc[VWERASE] = 0;
newtp.c_cc[VLNEXT] = 0;
newtp.c_cc[VEOL2] = 0;
}
int main()
{
openport();
readport();
return 0;
}
Thanks
shan