Re: Serial Programming trouble
- From: dieter.verslype@xxxxxxxxx
- Date: 14 Sep 2006 01:29:37 -0700
I got it to work, i needed to wait for some small interval between
sending characters.
Here's my code:
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
/*
* Opens the serial port
*/
void open_port(int* fd) {
*fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if (*fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
} else
fcntl(*fd, F_SETFL, FNDELAY);
}
/*
* Closes the serial port
*/
void close_port(int* fd) {
close(*fd);
}
/*
* Prepares the serial port for communication
*/
void configure_communication(int fd) {
struct termios options;
// get current options
tcgetattr(fd, &options);
// set i/o baud rate
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// cflag options
options.c_cflag = ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
options.c_cflag &= ~CRTSCTS;
options.c_cflag &= ~HUPCL;
// iflag options
options.c_iflag = IGNBRK;
options.c_iflag &= ~IXON;
options.c_iflag &= ~IXOFF;
options.c_iflag &= ~IXANY;
// lflag options
options.c_lflag = ~OPOST;
options.c_lflag &= ~OLCUC;
options.c_lflag &= ~OCRNL;
options.c_lflag &= ~ONLCR;
options.c_lflag &= ~ONOCR;
options.c_lflag &= ~ONLRET;
options.c_lflag &= ~OFILL;
options.c_lflag &= ~OFDEL;
options.c_lflag |= NL0;
options.c_lflag |= CR0;
options.c_lflag |= TAB0;
options.c_lflag |= BS0;
options.c_lflag |= VT0;
options.c_lflag |= FF0;
// oflag options
options.c_oflag = 0;
// set port with current options
tcsetattr(fd, TCSANOW, &options);
}
/*
* Actively waits for some milliseconds
*/
void active_wait(int milliseconds)
{
timeval system_time;
gettimeofday(&system_time,NULL);
double start_time = (system_time.tv_sec*1000.0) +
(system_time.tv_usec/1000.0);
gettimeofday(&system_time,NULL);
double current_time = (system_time.tv_sec*1000.0) +
(system_time.tv_usec/1000.0);
while (current_time - start_time < milliseconds)
{
gettimeofday(&system_time,NULL);
current_time = (system_time.tv_sec*1000.0) +
(system_time.tv_usec/1000.0);
}
}
/*
* Sends a character string with intervals of 100 milliseconds
*/
void send_string(char* sp, int* fd)
{
while(*sp != '\0')
{
write(*fd,sp,1);
active_wait(100);
sp++;
}
}
int main(int argc, char **argv) {
int fd;
open_port(&fd);
configure_communication(fd);
send_string("DM", &fd); // Start PC communication session
send_string("DM?", &fd); // Request software version and date
char buf[255];
int n;
while ((n = read(fd, buf, 255))>0)
{
printf("read %d bytes: ", n);
for(int i=0; i<n; i++)
{
printf("%c", buf[i]);
}
printf("\n");
}
close_port(&fd);
}
.
- Follow-Ups:
- Re: Serial Programming trouble
- From: Floyd L. Davidson
- Re: Serial Programming trouble
- References:
- Serial Programming trouble
- From: dieter . verslype
- Re: Serial Programming trouble
- From: Patrick
- Re: Serial Programming trouble
- From: Grant Edwards
- Serial Programming trouble
- Prev by Date: Re: modern computer books online pdf
- Next by Date: Re: Help me choose the Linux desktop distribution l should use
- Previous by thread: Re: Serial Programming trouble
- Next by thread: Re: Serial Programming trouble
- Index(es):
Relevant Pages
|