hi, I've a problem on my C/C++ program with Posix Library.
I have to read data from the serial but I have incorrect data, in fact I get a bunch of zeros:
"2953.3174, 2785.2126, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0,0, "
I should get something like this:
3087,3102,2492,1860,1403,1074,1004,1468,2152,2645,3007,3143,2714,2006,1503,1148,971,1308,1998,2536,2 925,3163,2884,2182,1625,1234,979,1170,1820,2411,2834,3135,3013,2381,1721,1328,1021,1076,1646,2290,27 46,3079,3115,2589,1901,1430,1095,1001,1442,2128,2630,2994,3156,2758 ETC...
My class that i use is this:
int cClassSerialLIB::ReadPort(/*QChar * Response*/)
{
long int iIn;
counter_uart2 =0;
for (int i=0;i<6000;i++)
array_uart2='\0';
if (fd < 1)
{
printf(" port is not open\n");
return -1;
}
iIn = read(fd, array_uart2, sizeof(array_uart2));
static int count=0;
if (iIn < 0)
{
if (errno == EAGAIN)
{
//qDebug("Read nothing");
return 0; // assume that command generated no response
}
else
{
printf("read error %d %s\n", errno, strerror(errno));
}
}
counter_uart2=iIn;
return iIn;
}
and open function:
bool cClassSerialLIB::OpenPort(bool bloccante)
{
// make sure port is closed
ClosePort();
//ttymxc4
QByteArray Porta_byte_array = Porta.toLatin1();
const char* Porta_char=Porta_byte_array.data();
fd = open(Porta_char/*"/dev/ttymxc4"*/, O_RDWR |
O_NOCTTY | /* tells UNIX that this program doesn't want to be the "controlling terminal" for that port*/
O_NDELAY); /* tells UNIX that this program doesn't care what state the DCD signal line is in */
if (fd < 0)
{
qDebug("errore in apertura %d %s\n", errno, strerror(errno));
return false;
}
else
{
qDebug("ok %d",fd);
struct termios options;
/* The FNDELAY option causes the read function to return 0 if no
characters are available on the port. To restore normal (blocking) behavior,
call fcntl() without the FNDELAY option: */
if(bloccante)
{
printf("bloccante\n");
fcntl(fd, F_SETFL, 0);
}
else
{
printf("NON bloccante");
fcntl(fd, F_SETFL, FNDELAY);
}
//cfmakeraw(&options); //ABILITARLO NEL CASO DI ERRORE!
tcgetattr(fd, &options);
//set port speed 115200
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD);
//set 8n1
options.c_cflag &= ~PARENB; /* Enable parity bit */
options.c_cflag &= ~CSTOPB; /* 2 stop bits (1 otherwise) */
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_iflag &= ~IXON; //RIMUOVE X0N/XOFF control
options.c_iflag &= ~IXOFF;
options.c_iflag &= ~IGNCR;
//set raw input
options.c_lflag &= ~(ICANON | /*Enable canonical input (else raw)*/
ECHO | /*Enable echoing of input characters*/
ECHOE | /*Echo erase character as BS-SP-BS*/
ISIG); /*Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals*/
tcsetattr(fd, TCSANOW, &options); /*Make changes now without waiting for data to complete*/
}
return true;
}
how i must set option in open function? thanks..