But before that I've tried to use C++ for a long time but it is unsuccessful. I follow this document http://www.chemie.fu-berlin.de/chemnet/ ... tml#SEC237, the below is my code
Code: Select all
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <iostream>
using namespace std;
int fd;
struct termios tm;
int main() {
fd = open("//dev//ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC);
if (fd == -1)
{
printf ("Could not open port.");
return -1;
}
fcntl(fd, F_SETFL, 0);
memset(&tm, 0, sizeof(struct termios));
if (tcgetattr(fd, &tm) != 0)
{
printf ("error from tggetattr");
return -1;
}
/*9600 Baud, 7 Data Bits, Even Parity, 1 Stop Bit, No Flow Control*/
cfsetispeed(&tm, B9600);
cfsetospeed(&tm, B9600);
tm.c_cflag |= (CLOCAL | CREAD);
tm.c_cflag |= PARENB;
tm.c_cflag &= ~PARODD;
tm.c_cflag &= ~CSTOPB;
tm.c_cflag &= ~CSIZE; /* Mask the character size bits */
tm.c_cflag |= CS7; /* Select 7 data bits */
tm.c_oflag = OPOST;
tm.c_iflag &= ~IGNBRK; // disable break processing
tm.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tm.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tm.c_cc[VTIME] = 5;
tm.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSANOW, &tm) != 0)
{
printf ("error from tcsetattr");
return -1;
}
unsigned char request[] = {0x2f,0x3f,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x37,0x36,0x36,0x31,0x21,0x0d,0x0a};
int result = write(fd, request, sizeof request);
printf("\nSend result [%d]", result);
unsigned char Respon[255];
memset(Respon, 0, sizeof Respon);
result = read(fd, Respon, sizeof Respon);
printf("\nRead result [%d]", result);
for (int j=0 ; j < result; j++)
{
printf(" %02x ", Respon[j]);
}
printf("\n");
return 0;
}
But when I change code for canonical mode of POSIXpi@raspberrypi:~/solarD/src $ ./solar.out
Send result [17]
Read result [0]
pi@raspberrypi:~/solarD/src $ ./solar.out
Send result [17]
Read result [2] 30 30
Code: Select all
tm.c_cc[VTIME] = 5;
tm.c_cc[VMIN] = 255;
And when I dump serial port, it show me that there are something has received as below
pi@raspberrypi:~ $ tail -f /dev/ttyUSB0
00000001766100000000000000000000000000000000000000000000000000000000000000000000000000000002320000000000000000000000000000000000000000000000000000C000C000C0000000000011512080313555702000000000000000000000000000000000000000000000000000000000000!
=00000001766100000000000000000000000000000000000000000000000000000000000000000000000000000002324000000000000000000000000000000000000000000000000000C000C000C0000000000011512080313560302000000000000000000000000000000000000000000000000000000000000!
Anyone can suggest me how can I fix it.