Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Objective-C
./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

Objective-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 /* POSIX compliant source */
#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 sendcmd1[256]="\0";
char buffer[512];

void main()
{


	openport();
	sleep(1);
//	readport();
	sendport();




}

void sendport(void)
{
        printf("enter write\n");
        int n;
     //   sem_wait(&len);
                file = fopen("sample.txt", "r");

                //get file size

                fseek(file, 0, SEEK_END);
                fileLen = ftell(file);
                fseek(file, 0, SEEK_SET);

                tmpbuffer = (char *)malloc(fileLen + 1);

                //read file contents
                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); /* save current serial port settings */
        // tcgetattr(fd,&newtp); /* save current serial port settings */
         bzero(&newtp, sizeof(newtp));
        // bzero(&oldtp, sizeof(oldtp));
                                                                                
         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;     /* Ctrl-c */
         newtp.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
         newtp.c_cc[VERASE]   = 0;     /* del */
         newtp.c_cc[VKILL]    = 0;     /* @ */
         //newtp.c_cc[VEOF]     = 4;     /* Ctrl-d */
         newtp.c_cc[VEOF]     = 0;     /* Ctrl-d */
         newtp.c_cc[VTIME]    = 0;     /* inter-character timer unused */
         newtp.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
         newtp.c_cc[VSWTC]    = 0;     /* '\0' */
         newtp.c_cc[VSTART]   = 0;     /* Ctrl-q */
         newtp.c_cc[VSTOP]    = 0;     /* Ctrl-s */
         newtp.c_cc[VSUSP]    = 0;     /* Ctrl-z */
         newtp.c_cc[VEOL]     = 0;     /* '\0' */
         newtp.c_cc[VREPRINT] = 0;     /* Ctrl-r */
         newtp.c_cc[VDISCARD] = 0;     /* Ctrl-u */
         newtp.c_cc[VWERASE]  = 0;     /* Ctrl-w */
         newtp.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
         newtp.c_cc[VEOL2]    = 0;     /* '\0' */
                                                                                
                                                                                
   //     tcflush(fd, TCIFLUSH);
//	tcsetattr(fd,TCSANOW,&newtp);
}


RECEIVE.c

Objective-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"/*UART NAME IN PROCESSOR*/
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#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);
//	fcntl(fd,F_SETFL,0);
  if (n == -1) switch(errno) {
         case EAGAIN: /* sleep() */ 
            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); /* save current serial port settings */
    //     tcgetattr(fd,&newtp); /* save current serial port settings */
         bzero(&newtp, sizeof(newtp));
  //       bzero(&oldtp, sizeof(oldtp));
                                                                                
         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;     /* Ctrl-c */
         newtp.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
         newtp.c_cc[VERASE]   = 0;     /* del */
         newtp.c_cc[VKILL]    = 0;     /* @ */
        // newtp.c_cc[VEOF]     = 4;     /* Ctrl-d */
         newtp.c_cc[VTIME]    = 0;     /* inter-character timer unused */
         newtp.c_cc[VMIN]     = 0;     /* blocking read until 1 character arrives */
         newtp.c_cc[VSWTC]    = 0;     /* '\0' */
         newtp.c_cc[VSTART]   = 0;     /* Ctrl-q */
         newtp.c_cc[VSTOP]    = 0;     /* Ctrl-s */
         newtp.c_cc[VSUSP]    = 0;     /* Ctrl-z */
         newtp.c_cc[VEOL]     = 0;     /* '\0' */
         newtp.c_cc[VREPRINT] = 0;     /* Ctrl-r */
         newtp.c_cc[VDISCARD] = 0;     /* Ctrl-u */
         newtp.c_cc[VWERASE]  = 0;     /* Ctrl-w */
         newtp.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
         newtp.c_cc[VEOL2]    = 0;     /* '\0' */
   	                                                                                                                                             
//	  tcflush(fd, TCIFLUSH);
//	 tcsetattr(fd,TCSANOW,&newtp);

}

int  main()
{

	openport();
	readport();
        return 0;
}



Thanks
shan
Posted
Updated 29-Jan-14 23:21pm
v7
Comments
Richard MacCutchan 30-Jan-14 7:13am    
You are printing each character preceded by a digit and followed by a newline.
shan bala 30-Jan-14 7:30am    
hi richard ,

The digit is readsize of FD and the character is, i am receiving only one character from FD at a time, its printing in a newline because i have put \n in my printf inside the readport function
[no name] 30-Jan-14 14:19pm    
In sendport, why do you write with a length of fileLen + 1? That extra byte will be garbage (whatever was on the heap).

1 solution

I'm guessing your serial port's FIFO is limited to 16 bytes.

1. Run receive before running send (perhaps you are - I can't tell).
2. Modify receive to read more than one character (suggest 16 characters) at a time.
3. Modify receive to linger after the last character for some small amount of time.
4. Define an end-of-transmission mechanism. This could be a special byte value or one of the modem status pins (like DSR/DTR) so that receiver knows when to exit.
 
Share this answer
 
Comments
Shravan Meghavath 6-Oct-20 9:21am    
is their any hardware connection required by this program to execute like moxa card or directly it will execute.I tried to excute this program by enabling tty0 but i am not receiving any data please help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900