i need to configure it so that i can read without any handshaking.
the problem with my code is that im using the newtio.c_cc[VEOF] = 3; so i can only read when there was byte with value 3, cant i configure it so that i will read 20 bytes every time i do read ?
data trans
2; 5; 7; 52; 3;
begin data end
2;5;7;52;3; 2;25;7;8;3; 2;5;33;8;3; 2;5;88;8;3 = 20 bytes
protocol explanation
2 begin of data
3 end of data
so i have
newtio.c_cc[VEOF] = 3;
then i read 4 times from my port in a for loop
but the big problem is when one of the data bytes has a value of 3!!!!
so is there a way to configure the termios.h that it will read 20 bytes
every time i use "res = read(fd,buf,15); " ??
CODE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdlib.h>
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B9600
/* change this definition for the correct port */
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
using namespace std;
main()
{
char test[255];
int fd,c, res,testen;
int sercmd, serstat;
struct termios oldtio,newtio;
unsigned char buf[255];
/*
Open modem device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR );
if (fd <0){
cout<<"error \n";
}
tcgetattr(fd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL |CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR | ICRNL | IGNBRK;
/*
Raw output.
*/
newtio.c_oflag = 0;
/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
newtio.c_lflag = ICANON;
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and are given
in the comments, but we don't need them here
*/
newtio.c_cc[VEOF] = 3;
newtio.c_cc[VMIN] = 5; /* blocking read until 5 character arrives */
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
//RTS DISABLE !!!! <--------- this is required to be able to communicate with my PowerMeter
ioctl(fd, TIOCMGET, &serstat);
sercmd = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &sercmd); // Set the RTS pin.
ioctl(fd, TIOCMGET, &serstat);
while (STOP==FALSE) {
//Protocol is write " " to powermeter, then the powermeter will send its data back
char spatie[2] = " ";
if (write(fd,spatie,1) < 1)
{
cout<<"-------------error--------------\n";
}
sleep(2);
for (int j = 0; j <4; j++){
res = read(fd,buf,15);
cout << "alle data \n";
for(int i= 0; i <res; i++){
cout <<" buf ["<< i << "] :"<< (long)buf[i]<<"\n";
}
cout<<"\ngefilterde data \n";
int sizebuf = sizeof buf;
cout << " aantal data = " << res << "\n";
cout << " data 1 : " << (int) buf[res-3] << "\n";
cout << " data 2 : " << (int) buf[res-2]<< "\n";
cout << " data 3 : " << (int) buf[res-1]<< "\n";
}
getchar(); // Wait for the return key before continuing.
}
/* restore the old port settings */
tcsetattr(fd,TCSANOW,&oldtio);
cout<<"-----einde-----\n";
}