ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: redhat 9 , fedora 5,6,7,DSL,Debian 3, mandrake 9.2 , knoppix 3.8, red hat 7.3
Posts: 38
Rep:
Serial Port
Hello everyone, i'm writing a small c program, but i have stucked .
I have connected a small device on serial port, when i send a "9G" command to a device he's respond is "9G12,22;".
This perfectly works in minicom.
Parameters:
9600
8N1
NO HARDWARE FLOW CONTROL
NO SOFTWARE FLOW CONTROL
/dev/ttyS0
/dev/ttS0 is accessible,
because i did
chmod a+rw /dev/ttyS0
The same response i'm trying to achieve with my code, but i got no response I think there is an error in serial port parameters settings in code. Please HEEELP:
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 */
#include <stdio.h>
int open_port(void)
{
int fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd<0)
{
printf("Unable to open port\n");
}
return fd;
}
/* ************************************* Setting parameters for serial port **************************************/
void set_parameters(int fd)
{
struct termios options;
fcntl(fd, F_SETFL, FNDELAY); /* Configure port reading */
tcgetattr(fd, &options); /* Get the current options for the port */
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
cfsetospeed(&options, B9600);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
options.c_lflag &= ~(ICANON | ECHO | ISIG); /* RAW mode */
/* Set the new options for the port */
tcsetattr(fd, TCSANOW, &options);
}
/********************************* main **********************************************************************************/
int main(void)
{
int fd,n;
char buf[8];
fd=open_port();
set_parameters(fd);
n=write(fd,"9G",3);
if(n!=3)
printf("Write to port failed!\n");
read(fd,buf,8) ;
printf("Read from serial:%s",buf);
close(fd) ;
return 0;
}
First thing I notice is that what you send in minicom is not the same as what you send in your program.
In minicom, you only send 2 characters (or 3 or 4 including a CR and/or LF). In your program however you send 2 and a string terminator. I think that your device needs a CR and/or LF to see the end of the command, so I would change
Further some advice for debugging:
If your PC has 2 serial ports, you can connect them, run minicom on the one and your program on the other.
If you don't have 2 serial ports, you can use pseudo terminals. Start minicom as below and change /dev/ttyS0 in your program to /dev/ttyp0. minicom -o -p /dev/ptyp0
Both allow you to check in minicom what you're sending and send replies back and check what your program is reading.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.