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 ...
- 12-27-2010 #1
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
WasifLast edited by mwasif; 12-27-2010 at 06:38 PM.
- 12-27-2010 #2Just 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:
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.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); }
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:
To receive data, use read function.Code:n = write(fd, "ATZ\r", 4); if (n < 0) fputs("write() of 4 bytes failed!\n", stderr);
After you are done, please call close function to release the serial device, as:
good luck!Code:close(fd);
- 12-27-2010 #3
I run this code, this gives an error:
serial_exp.c is the name of file containing code.Code:[Shaheen@Shaheen ~]$ gcc -o serial_exp serial_exp.c [Shaheen@Shaheen ~]$ ./serial_exp open_port: Unable to open /dev/ttyS0 - : Permission denied
How can i deal with this error?Last edited by mwasif; 12-27-2010 at 08:32 PM.
- 12-27-2010 #4Just 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.
- 12-27-2010 #5
- 12-27-2010 #6Just 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.
- 12-30-2010 #7


Reply With Quote
