Communication using RS232 port

This is a discussion on Communication using RS232 port within the C Programming forums, part of the General Programming Boards category; Alternatively, send the size of the file in a "header packet" somehow. -- Mats...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Alternatively, send the size of the file in a "header packet" somehow.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Quote Originally Posted by matsp View Post
    Code:
    delay(10);
    Would you think that waiting 0.01 seconds every character you output could have some effect on the transfer time? At 38400, this is equivalent of waiting for 384 bit times - so 38 characters, making it 39 two out of five waits.

    Also calling fputc on every character will be noticably less efficient than buffering up some amount (such as 256 to 1024 bytes) and storing that using fwrite().

    --
    Mats
    yes, sir i was just wondering why its taking so much time and realized that delay is the reason..also sir, I have one question,

    If transfer can be done so easily using these commands then why use bioscom or createfile etc. commands? I mean what is the main difference?(just curious)..

    Thanks
    Edesign

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by edesign View Post
    yes, sir i was just wondering why its taking so much time and realized that delay is the reason..also sir, I have one question,

    If transfer can be done so easily using these commands then why use bioscom or createfile etc. commands? I mean what is the main difference?(just curious)..

    Thanks
    Edesign
    Perhaps you'd want to use it within a 32-bit application (e.g. receive a LARGE file and store it in memory before writing anything to disk - just as an example), use higher than 38400 bps with some reliability, and not consume 100% of the CPU whilst doing it?

    Also, your serial port may not be where you expect it to be - e.g. it may be a USB port in between, and it MAY be that the DOS emulation inside Windows handles that too, but it may also NOT do that. Even in the case of DOS emulation actually handling the IO, it will most likely take MUCH longer to perform this emulation process with a USB device, and the overhead is noticeable.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Ok..thanks....
    I got it...Now I will try using fiber optic kit that I have at my college...I hope that also work..

    Thanks..all

  5. #20
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Hello all,

    I made a complete program & now suddenly I am facing a strange problem...for text files it works perfectly but for other type files it transfers 1KB & then stops..what can be the reason..

    Code:
    #include <stdio.h>
    #include <dos.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<limits.h>
    #define PORT1 0x3f8
    #define PATH_MAX 512
    long int length;
    char ch,cha;
    char a,c;
    int choice,i;
    void main()
    {
       void transmitfile(FILE *,long int);
       void receivefile(FILE *);
       long int sizeoffile(FILE *);
    
       char filename[PATH_MAX],filename2[PATH_MAX],buf;
       FILE *in,*out;
       clrscr();
       outportb(PORT1+3,0x80);
       outportb(PORT1,0x03);
       outportb(PORT1+3,0x03);
       printf("\nthis computer is going to act as a:");
       printf("\n 1.sender \n2.receiver\n\nEnter the choice:");
       scanf("%d",&choice);
       char x;
       switch(choice)
       {
    	   case 1:
    		  x=getchar();
    		  printf("enter the filename to be created:");
    		  fgets(filename,PATH_MAX,stdin);
    		  in=fopen(filename,"rb");
    		  length=sizeoffile(in);
    		  if(length!=NULL)
    			printf("\n\nsending.....\n\n");
    		  else
    			printf("\n\nFile does not exist\n\n");
    		  transmitfile(in,length);
    		  fclose(in);
    		  printf("Reading from the port...");
    		  printf("writing to file %s",filename2);
    		  break;
    	   case 2:
    		 x=getchar();
    		 printf("enter the filename to be created:");
    		 fgets(filename2,PATH_MAX,stdin);
    		 out=fopen(filename2,"wb");
    		 printf("Reading from the port...");
    		 printf("writing to file %s",filename2);
    		 receivefile(out);
    		 break;
    	   default:
    		 exit(1);
    }
    getch();
    }
    long sizeoffile(FILE *fp){
       long curpos, length;
       curpos = ftell(fp);
       fseek(fp, 0L, SEEK_END);
       length = ftell(fp);
       fseek(fp, curpos, SEEK_SET);
       return(length);
    }
    void transmitfile(FILE *in,long int len)
       {
          long int i=0;
          do{
          {
           ch=fgetc(in);
           do{
    			a=inportb(0x3fd)&0x40;
    		 }while(a!=0x40);
           outportb(PORT1,ch);
    	 //printf("%c",ch);
    	 i++;
      }while(i<len);
      }
      void receivefile(FILE *out)
      {
    
      do
      {
    	do
    	  {
    	   c=inportb(PORT1+5)&0x01;
    	  }while(c!=0x01);
        cha=inportb(PORT1);
        //printf("%c",buf);
        fputc(cha,out);
        //delay(10);
      if(kbhit())
       {
         ch=getch();
       }
      }while(ch!=27);
      }
    I can't find out what can go wrong...I changed everything I can think of causing the problem...

    Thanks
    Edesign

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see any reason why your code should stop after 1KB - are you sure it's exactly 1KB, or some other amount, and perhaps it makes sense to add a log-file that prints each byte sent as a hex number (using " %02x" format), for the transmit and receive sides, and see where it stops.

    Code:
    		  fgets(filename,PATH_MAX,stdin);
    		  in=fopen(filename,"rb");
    I'm surprised that works at all - fgets() is supposed to leave a newline in the string, and fopen() should complain about that.

    Code:
    		  length=sizeoffile(in);
    		  if(length!=NULL)
    Ehm, NULL is meant to be used with pointers. The variable "length" is a pointer.

    Code:
    		  x=getchar();
    Perhaps this belongs just after scanf(), rather than in each of the two switch statements?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Hello Sir,

    yes..I should move it before switch..also..I will change the fgets to readline that you suggested me ...then I will check again...If I keep the printf statements uncommented it prints same garbage on both screens...I'll check once again...

    Thanks,
    Edesign

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by edesign View Post
    Hello Sir,

    yes..I should move it before switch..also..I will change the fgets to readline that you suggested me ...then I will check again...If I keep the printf statements uncommented it prints same garbage on both screens...I'll check once again...

    Thanks,
    Edesign
    Yes, you don't want to use "%c" for the printf - and it's probably better to print to a file - but more importantly, use %02x rather than %c.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    also note that main returns int - read FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #25
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Sir there is also one more question - general

    When i pass the file path as command line arguments and if the path is having space " ", then it is not able to open the file...that is c:\documents & settings\hi.txt..can't be opened..how to overcome this?

    Edesign

  11. #26
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    use quotes around the path
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #27
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You should pass the path in ""

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And one drawback of using an old DOS-based compiler is that it doesn't "understand" long filenames. Long filenames is anything that:
    Contains a mix of lower and upper case
    Contains one or more spaces.
    Has more than 8 characters before the dot or more than 3 after, or more than one dot in the name.
    [There are PROBABLY other things that classify as "long name" too, but those are the most common "long name" rules].

    You then need to specify the filename using the "old style names". If you do dir /X it will show the names as 8.3 compliant names. Names will look like this XXXXXX~1.YYY. So your file to copy would be perhaps "c:\Docume~1\hi.txt"

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. codes for com port Rs232 in serial communication
    By justinwandji in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2009, 01:33 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Communication over parallel port
    By Istari in forum C Programming
    Replies: 5
    Last Post: 08-25-2004, 03:22 AM
  5. Need help or info about serial port communication
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2002, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21