communication using serial port in c

ppvs
ppvs used Ask the Experts™
on
i want to comunicate to external electronic circuit through the serial port of PC. how can i do it in C.
is there any specific instruction in C for serial interface as it is there for parallel port like outportb or inportb?
will it be sufficient to connect the RX,TX & gnd pins of the RS 232 connector to the circuit?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Serial programming is a complex topic in any language on any platform.  Specifics of exactly how to do it will depend on what computer, OS, compiler, toolkit, etc. you are using -- can you tell us?  

Generally speaking, C does not contain "specific instructions" (statements) for serial I/O.  On the other hand, 'outportb' and 'inportb' aren't standard C, so perhaps you're using a specialized version of C that DOES have special statements.  See your documentation.

For best results you should connect not only RX,TX, and GND, but also the other RS-232 control lines (e.g. RTS, DTR, CTS, CD).  Some serial hardware will work with only RX/TX/GND, but some will not.

Commented:
What C compiler you plan to use?
Tx, Rx and Gnd pins are all u need yes! , i have some code here to help,  it sends a text file,  maybe it can help you :)

#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define COM1 0x3F8
#define Inter COM1 + 1                 /* Interrupts On/Off                                                       */
#define FIFO COM1 + 2                /* FIFO Control Register                                                   */      
#define LCR  COM1 + 3               /* Settings for Baudrate/DLab etc                                       */
#define LSR COM1 + 5               /* Line Status Register                                                 */


  /* Serial Ports Base Address */
  /* COM1 0x3F8                */


void cominit(void);                     /* Function Prototypes */
void readfile(void);
void polling(void);

main()
{

  cominit();
  readfile();
  polling();

 return 0;
}

/************************************ Functions *****************************************/

void cominit(void)
{
      outp(Inter , 0);   /*            Turn off interrupts                                                       */

 /*                                     COM1 - Communication Settings                                               */

       outp(LCR , 0x80);  /*            Set DLAB On                                                                   */
       outp(COM1 , 0x03); /*            Set Baud rate - Divisor Latch Low Byte                               */
   
                             /* Default 0x03 =  38,400 BPS       */
                        /*         0x01 = 115,200 BPS       */
                        /*         0x02 =  57,600 BPS       */
                        /*         0x06 =  19,200 BPS       */
                        /*         0x0C =   9,600 BPS       */
                        /*         0x18 =   4,800 BPS       */
                        /*         0x30 =   2,400 BPS       */

       outp(COM1 + 1 , 0x00);              /* Set Baud rate - Divisor Latch High Byte                   */
       outp(LCR , 0x03);                          /* 8 Bits, No Parity, 1 Stop Bit                              */
       outp(FIFO , 0xC7);                    /* FIFO Buffer Register Settings                             */

 }

/****************************************************************************************/

void readfile(void)
{
 int data,i,flen,charcount;
 char filename[30];                 /* Filename 30 chars maximum                                    */
 char fdata[50000];                      /* Filesize 100,000 maximum                                          */
 FILE *fp;                               /* File Pointer                                                            */

 getfile:
 printf("Enter filename to transmit as string\n");
 scanf("%s", &filename);

 flen = strlen(filename);                     /* If filename > 30, Error                                    */

 if (flen > 30)
 {
      printf("Filename too large\n");
      goto getfile;
 }

 fp = fopen(filename,"r");                       /* Open file for a read                                     */
             if ( (fp == NULL))
             {
                   printf("Error in opening the file for reading\n");
                  goto getfile;
             }
                   data = getc( fp );                   /* Read first char                                                */

   i=0;
   charcount=0;                                      /* Count up num of characters in file                   */

      while ( !feof( fp ))
         {                                                          /* Not end of file, so read char                           */
      
         fdata[i] = data;                        /* Read characters into array                              */
             data = getc( fp );                   /* Read the next char                                        */
         i++;
         charcount=charcount+1;                  /* Increment character count                               */
      }
             fclose(fp);

   i=0;
   while (i != charcount)
         {
               outp(COM1,fdata[i]);
            printf("%c",fdata[i]);
            delay(5);
            i++;
         }
   outp(COM1,0x00);                                    /* Send End of Transmisson Character                  */
   printf("\nFile Sent Successfully");
   getch();
 }

/***************************************************************************************/

 void polling(void)
{
 int c=0,charcount,i;
 char data=0x01;
 char filename[20];                                     /* Filename 20 chars maximum                              */
 char fdata[50000];
 FILE *fp;

 i=0;
 charcount=0;
 while ( data!=0x00 )                              /* Data not equal to End of Transmission            */
 {
 c = inp(LSR);
 while(!(c&1))
      {
            c = inp(LSR);                            /* Check to see if char has been recieved            */      
      }

            data = inp(COM1);                        /* Get the next char                                          */
      
             fdata[i] = data;                         /* Write the char into array                            */
         i++;
         charcount = charcount +1;
            

 }
      printf("Enter path to save the file to\n");
    scanf("%s",filename);

 fp = fopen(filename,"w");                        /* Open a file for a write                                    */

 if ( (fp == NULL) )
 {
       printf("Error in opening the file for writing\n");
 }
 i=0;
             while(i != charcount)
      {
                    putc(fdata[i], fp );             /* Write chars to file                                          */
                     i++;
      }
     
      fclose(fp);
    printf("\nFile Received Successfully");
 }
 


Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

I have this completed now and can send any file over the link,  .exe, .doc  whatever!  i can get you that code if u would like
hmm if the circuit you wanna comunicate with is a transciever device or radio device u will need more than these 3 pins yeah,  are u converting the RS-232 logics with a max-232 chip yes?
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to TDC_LuCiFeR

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial