Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,492 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,320 people online right now. Registration is fast and FREE... Join Now!




termios.h

 
Reply to this topicStart new topic

termios.h

akira300
1 Mar, 2007 - 09:12 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
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";
        }


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Termios.h
1 Mar, 2007 - 06:07 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
c_cc[VMIN ] tells the length of the input buffer. You have it set to 5, try setting it to 20. Next you use c_cc[VEOF] = 3 to say that char 3 represents the EOF when it seems that you may mean EOL. Also ignoring this char and just reading data into your buffer and then manualy determining the meaning of each char might work better than using the VEOF/VEOL.

The bottom of this page gives some info on these modes.
User is offlineProfile CardPM
+Quote Post

akira300
RE: Termios.h
2 Mar, 2007 - 01:57 AM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
QUOTE(NickDMax @ 1 Mar, 2007 - 07:07 PM) *

Also ignoring this char and just reading data into your buffer and then manualy determining the meaning of each char might work better than using the VEOF/VEOL.


how can i do this ? can you give me an example ?

thnx in advanced

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Termios.h
2 Mar, 2007 - 02:41 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
no actually I can't as I am on a windows platform and can't seem to find a copy of termios.h for any of my compilers. So I could not test anything that I wrote.
User is offlineProfile CardPM
+Quote Post

akira300
RE: Termios.h
2 Mar, 2007 - 07:57 AM
Post #5

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
ah with that link you gave i manged to get it to work properly. thnx
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month