Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,132 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,803 people online right now. Registration is fast and FREE... Join Now!




Serial port communication in C

 
Reply to this topicStart new topic

Serial port communication in C, how to communicate through serial port

Rating  5
anand mohan
29 May, 2007 - 03:39 AM
Post #1

New D.I.C Head
*

Joined: 29 May, 2007
Posts: 3


My Contributions
CODE

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main(void)
{
      FILE *input;
      FILE *output;
      input = fopen("/dev/tty", "r");      //open the terminal keyboard
      output = fopen("/dev/tty", "w");
      int fd; /* File descriptor for the port */
      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
       /*
    * Could not open the port.
    */
    perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
    {
        int c=fgetc(input);
        while(c!=0)
        {
            fputc(c,output);//output to screen
            write(fd, c, 1);//write to port
            c=fgetc(input);//get char from keyboard
            fcntl(fd, F_SETFL, 0);//read from port

        }
      printf("%d",fd);
    return (fd);
   }

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Serial Port Communication In C
29 May, 2007 - 05:18 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Do you have a question? Or is this meant as a code snippet to be used by others?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Serial Port Communication In C
29 May, 2007 - 06:23 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,442



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
If you want to communicate with a device via the serial port in Unix, just use the command cu.
User is online!Profile CardPM
+Quote Post

anand mohan
RE: Serial Port Communication In C
6 Jun, 2007 - 01:02 AM
Post #4

New D.I.C Head
*

Joined: 29 May, 2007
Posts: 3


My Contributions
QUOTE(anand mohan @ 29 May, 2007 - 04:39 AM) *

CODE

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main(void)
{
      FILE *input;
      FILE *output;
      input = fopen("/dev/tty", "r");      //open the terminal keyboard
      output = fopen("/dev/tty", "w");
      int fd; /* File descriptor for the port */
      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
       /*
    * Could not open the port.
    */
    perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
    {
        int c=fgetc(input);
        while(c!=0)
        {
            fputc(c,output);//output to screen
            write(fd, c, 1);//write to port
            c=fgetc(input);//get char from keyboard
            fcntl(fd, F_SETFL, 0);//read from port

        }
      printf("%d",fd);
    return (fd);
   }



User is offlineProfile CardPM
+Quote Post

anand mohan
RE: Serial Port Communication In C
6 Jun, 2007 - 01:09 AM
Post #5

New D.I.C Head
*

Joined: 29 May, 2007
Posts: 3


My Contributions
I was writing a program in C to communicate with the serial port.....i know certain functions like

fcntl(fd, F_SETFL, FNDELAY);//to read from port
but this only returns the number of characters in the buffer....how to read the characters and display to screen??

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:28PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month