Hello guys,i've a code which objective is to send Hex command to the serial port and then wait for 5 second. Then, read data output from the serial port. But my code doesn't works well. Please help me guys. Thanks in advanced.
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 */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
strerror(errno));
}
return (fd);
}
void main()
{
int mainfd=0,fd; /* File descriptor */
char chout;
struct termios options;
mainfd = open_port();
fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */
/* Get the current options for the port */
tcgetattr(mainfd, &options);
cfsetispeed(&options, B19200); /* Set the baud rates to 19200 */
cfsetospeed(&options, B19200);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
/* Set the new options for the port */
tcsetattr(mainfd, TCSANOW, &options);
write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
usleep(5000000); /* wait for 5 second*/
read(mainfd, &chout, sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */
if (chout != 0)
printf("Got %c.\n", chout);
chout=0;
/* Close the serial port */
close(mainfd);
}