LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reading com port data problem (https://www.linuxquestions.org/questions/programming-9/reading-com-port-data-problem-539963/)

applee 03-23-2007 12:29 PM

reading com port data problem
 
I'm already get to open port(COM2)to write data out. But, the problem is I can't get to read the data which sent from hardware device. Does anybody know how to solve it? Is there any errors for my code?

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/ttyAM1", O_RDWR | O_NOCTTY);

if (*fd == -1) {
perror("open_port: Unable to open /dev/ttyAM1 - ");
} 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, B115200);
cfsetospeed(&options, B115200);

// 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)
{
struct 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);
}


If the data sent from hardware device are in hex code, how can I convert it so that the data are in character form when display in minicom?

theNbomr 03-23-2007 03:18 PM

Wow, that's a lot of unformatted code! Consider using CODE tags to preserve your source code formatting.
Anyway, try this:
Code:

*fd = open("/dev/ttyAM1", O_RDWR | O_NOCTTY | O_NONBLOCK);
--- rod.


All times are GMT -5. The time now is 10:30 PM.