Re: Serial Programming trouble



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);
}

.



Relevant Pages

  • Re: Serial Programming trouble
    ... You want to also flag that with O_NONBLOCK. ... Closes the serial port ... void close_port{ ... void send_string(char* sp, int* fd) ...
    (comp.os.linux.misc)
  • Re: gcc -c -g test5 -o test5 ...
    ... int main(void) ... But your real problem is going to be that normal programs can't directly ... Is the device connected to the serial port (that's what ...
    (comp.os.linux.development.apps)
  • Re: Debug serial port
    ... the serial port has been closed. ... >> void OEMInitDebugSerialKernel ... >>> beitman AT applieddata DOT net ... >>> Applied Data Systems ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Debug serial port
    ... it made USB occupy the serial port. ... dial-up prgram via the modem, ... >> void OEMInitDebugSerialKernel ... >>> beitman AT applieddata DOT net ...
    (microsoft.public.windowsce.platbuilder)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)