Find the answer to your Linux question:
Results 1 to 7 of 7
Hi everyone! I am doing work on serial port. I want to send/receive data to/from Microcontrolar through serial port. But I don't know how to initialize, receive and send data ...
  1. #1
    Just Joined! mwasif's Avatar
    Join Date
    Dec 2010
    Posts
    4

    Question How to send /receive data through Serial port in Fedora??

    Hi everyone!

    I am doing work on serial port. I want to send/receive data to/from Microcontrolar through serial port. But I don't know how to initialize, receive and send data to Serial port. i am new in fedora so i hav'nt any deep concept of fedora.
    can anyone send me C code , which communicate through serial port in fedora (or atleast help me). I am using Fedora 11.
    Thanks in advance

    Best Regards

    Wasif
    Last edited by mwasif; 12-27-2010 at 06:38 PM.

  2. #2
    Just Joined!
    Join Date
    Dec 2010
    Posts
    6

    serial communication code

    Since Fedora is also sort of GNU/Linux operating system, it does not make difference for any linux distro. However to initialize ( I mean open the serial port), the code part is:
    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 */
    
        /*
         * 'open_port()' - Open serial port 1.
         *
         * Returns the file descriptor on success or -1 on error.
         */
    
        int
        open_port(void)
        {
          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
    	fcntl(fd, F_SETFL, 0);
    
          return (fd);
        }
    In the initialize code, fcntl(fd, F_SETFL, 0) function is called to make functions blocking, if you want nonblocking communication, which means read-write return instantly regardless of the data arrived (ready), delete that function.

    When you acquire file descriptor of the serial device, you can use standard linux system API. For instance, to send data to serial, use write function as it follows:

    Code:
    n = write(fd, "ATZ\r", 4);
        if (n < 0)
          fputs("write() of 4 bytes failed!\n", stderr);
    To receive data, use read function.

    After you are done, please call close function to release the serial device, as:

    Code:
    close(fd);
    good luck!

  3. #3
    Just Joined! mwasif's Avatar
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by artuyild View Post
    Since Fedora is also sort of GNU/Linux operating system, it does not make difference for any linux distro. However to initialize ( I mean open the serial port), the code part is:

    . . .

    good luck!
    I run this code, this gives an error:

    Code:
    [Shaheen@Shaheen ~]$ gcc -o serial_exp serial_exp.c
    [Shaheen@Shaheen ~]$ ./serial_exp
    open_port: Unable to open /dev/ttyS0 - 
    : Permission denied
    serial_exp.c is the name of file containing code.
    How can i deal with this error?
    Last edited by mwasif; 12-27-2010 at 08:32 PM.

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Posts
    6
    You should be root to open this device. Root user is the account of "administrator" like windows os.

    So be root as typing in the terminal as "su -", and then enter your root password, re-run the program. or use "sudo <applicationpathtorun>" to have temporary root privilege if sudo config file is set, of course.

  5. #5
    Just Joined! mwasif's Avatar
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by artuyild View Post
    You should be root to open this device. Root user is the account of "administrator" like windows os.

    So be root as typing in the terminal as "su -", and then enter your root password, re-run the program. or use "sudo <applicationpathtorun>" to have temporary root privilege if sudo config file is set, of course.
    Thanks
    But I don't know weather the data is received or not. How can I check this?

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Posts
    6
    himmmm I think that depends on your design of microcontroller board, At the past, I made some experiments on pic controller and check the data received via simple LEDs. You too can make simple board to detect the signal.

  7. #7
    Just Joined! mwasif's Avatar
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by artuyild View Post
    himmmm I think that depends on your design of microcontroller board, At the past, I made some experiments on pic controller and check the data received via simple LEDs. You too can make simple board to detect the signal.
    Thanks

    How can I set the properties (baud rate, parity bit and number of data bits etc)?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...