Results 1 to 4 of 4
Can any body help me who have experience on handling the serial port using Linux that is
How to open the serial port ?
How to configure serial port like ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-04-2011 #1Just Joined!
- Join Date
- Jul 2011
- Posts
- 18
Linux Serial port Programming
Can any body help me who have experience on handling the serial port using Linux that is
How to open the serial port ?
How to configure serial port like setting Baud rate, how to set parity , stop bit ?
How to write and read from the serial port ?
Any body have used these options regarding serial Communication can guide me please
Thanks in advance
- 10-06-2011 #2
Well, that would depend on which language you are using....
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
The Fifth Continent reborn
- 10-06-2011 #3Just Joined!
- Join Date
- Jul 2011
- Posts
- 18
I have written the code , but I am facing problem while reading the data from the port, I just write the simple programe to Initialize, configure , write and reading the same data as I wrote . Some time it reads only 10 bytes and stuck in the read loop and some time only 1 byte and this depends on the sequence or the values of data which I am sending ... it's quite strange . I just want it to read the same as I have sended . I have pasted my code can you please have a look and help me in this regard
#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 2.
*
* Returns the file descriptor on success or -1 on error.
*/
// opens the serial port
// return code:
// > 0 = fd for the port
// -1 = open failed
// ******** opening port ******************
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS1 - ");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("open sucess ttyS1\n");
}
return (fd);
}
// ******** Main Program ********************
int main (void)
{
int fd= open_port();
unsigned char data[20] = { 0x40, 0x28, 0x6b, 0xfe,0x70, 0x08, 0x09, 0x50,0x40, 0x67, 0x81, 0x00,
0x00, 0x00, 0x00, 0x00,0xd9, 0xff, 0xff, 0xff} ;
// Configure Serial port
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 19200...
*/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
// tcsetattr(fd, TCSAFLUSH, &options); // flush i/o buffer and apply the change
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options); // Make changes immediately without waiting for output
//data to finish sending or input data to finish receiving
// setting data bits
// setting parity checking
options.c_cflag |= PARENB ; // Enable parity bit
options.c_cflag &= ~PARODD; // use even parity
options.c_cflag &= ~CSTOPB;// 2 stop bits (1 otherwise)
options.c_cflag &= ~CSIZE; // Bit mask for data bits
options.c_cflag |= CS8; // 8 data bits
// setting input parity options
options.c_iflag |= (INPCK | ISTRIP);
// Timeout Settings
options.c_cc[VMIN] = 0; // Minimum number of characters to read
options.c_cc[VTIME] = 10; // Time to wait for data(tenth of seconds)
// writing data to the port
int n=0 ;
n = write(fd, data, 20);
// tcflush(fd, TCOFLUSH);
if (n < 0)
fputs("write() of 20 bytes failed!\n", stderr);
printf("transmitted with n=%d\n", n);
// Reading data
int count = 0;
while(count < 20) {
n = read(fd, data, 1);
count += n;
if( n > 0)
{
printf("Received Byte=%x\n",data[0]);
fflush(stdout);
}
}
// closing serial port
close(fd);
return 0;
}
- 10-07-2011 #4
I'm afraid I'm not an advanced enough c programmer to be much help here, but, many years ago when I had to program serial communications in C#, I needed to write a state machine to handle the asynchronous nature of the communication. It may be different in pure c.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
The Fifth Continent reborn


Reply With Quote
