Results 1 to 2 of 2
Hi, I'm currently writing a program that only reads characters from the serial port. The device connected to the port only puts out data. I would then like to convert ...
- 07-30-2008 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 7
serial port configuration
Hi, I'm currently writing a program that only reads characters from the serial port. The device connected to the port only puts out data. I would then like to convert the byte values into hex.
To get the data from the serial port I used the following tutorial:
Serial Programming Guide for POSIX Operating Systems - Michael R Sweet
however, I don't think I am understanding all of it because I am sending ASCII characters from a windows machine and my program isn't displaying the data to the screen. I am using a crossover able between the two computers.
My hunch is that I do not have the port configured right. I barely understood that part of the tutorial.
If any of you could help me figure out what is wrong and help me find a way to simplify the port configuration, I would be very grateful!
Thank you very much for your time,
windell
Here is what my code looks like...I took out the none essential parts.
Code:#include <termios.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #define BYTE_COUNT 38 int main() { char port_name[] = "/dev/ttyUSB0"; //set port name here. /*variable declarations*/ int serial_port; struct termios options; char input[50]; char old_string[50]; int i,print_flag; /*initialize arrays*/ for(i=0;i<50;i++){ input[i]='n'; old_string[i]='n'; } //-------------open port--------------------------------- serial_port = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_port < 0){ printf("unable to open %s\n",port_name); exit(0); } else { printf("Connected to %s\n",port_name); // fcntl(serial_port,F_SETFL,0); } //---------------------------------------------------------- /*configure port (don't know what this stuff does)*******************/ //get current options for the port. tcgetattr(serial_port, &options); //set the receive baud rate to 115200 baud cfsetispeed(&options, B115200); //enable the receiver and set local mode... options.c_cflag |= (CLOCAL|CREAD); //set the new options for the port. tcsetattr(serial_port, TCSANOW, &options); //8N1. options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; //disable flow control. options.c_cflag &= ~(IXON | IXOFF | IXANY); //choose raw input. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /**********************************************/ while (1) { read(serial_port,input,BYTE_COUNT); //store incoming data. input[BYTE_COUNT]='\0'; //terminate string. printf("%s\n",input); //print the input to screen. /*nonessential code here.*/ } //----------------------------------------------------------- /*dont know what these do!*/ tcsetattr(serial_port, TCSANOW, &options); tcflush(serial_port, TCOFLUSH); tcflush(serial_port, TCIFLUSH); //close the serial port. close(serial_port); serial_port = -1; return 0; }
- 08-03-2008 #2Just Joined!
- Join Date
- Aug 2008
- Posts
- 17
>tcgetattr(serial_port, &options);
puts the current ports settings in a struct
>cfsetispeed(&options, B115200);
linespeed (you should set out speed too)
>//set the new options for the port.
>tcsetattr(serial_port, TCSANOW, &options);
You probably want to do this after the following lines.
>//8N1.
>options.c_cflag &= ~PARENB;
>options.c_cflag &= ~CSTOPB;
>options.c_cflag &= ~CSIZE;
>options.c_cflag |= CS8;
>//disable flow control.
>options.c_cflag &= ~(IXON | IXOFF | IXANY);
>//choose raw input.
>options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
this is where you do the tcsetattr (because else it doesn't make sense to
change the struct members in options
>/**********************************************/
>while (1)
>{
.... snip ....
>/*dont know what these do!*/
>tcsetattr(serial_port, TCSANOW, &options);
set option to be effective immediately
>tcflush(serial_port, TCOFLUSH);
>tcflush(serial_port, TCIFLUSH);
flush the serial IO buffers
those 5 lines go before the 'tcsetattr' too
>//close the serial port.
>close(serial_port);
>serial_port = -1;
>return 0;


Reply With Quote