Logo Search packages:      
Sourcecode: marote version File versions

k2.cpp

#include "k2.h"

using namespace std;

// **** Functions ****

void k2::init_k2(int p)
{
    const char *sp;
    sp =  "/dev/ttyS0"; // just to prevent null pointers
    switch(p)
    {
    case 1:
 sp =  "/dev/ttyS0";
 break;
    case 2:
 sp = "/dev/ttyS1";
 break;
    }
    
    fd = open(sp, O_RDWR | O_NDELAY );
    if (fd < 0)
    {
qDebug("Unable to open the serial port! Exiting");
 exit(-1);
    }  
    // ************* Term I/O setup ******************
    // Clear the entire struct first
    bzero(&newtio, sizeof(newtio));
    // Control Modes
    newtio.c_cflag = B4800 | CS8 | CREAD | CLOCAL | CSTOPB;
    // Input Modes
    newtio.c_iflag =  IGNBRK;
    // Output Modes
    newtio.c_oflag = 0;
    // Local Modes 
    newtio.c_lflag = 0;
    // Control Chars
    newtio.c_cc[VTIME] = 0; // ignore timer 
    newtio.c_cc[VMIN] = 0; // no blocking read 
    
    // Clear out any junk we have not read
    tcflush(fd, TCIFLUSH);
    // Push our settings out
    tcsetattr(fd,TCSANOW,&newtio);
}


QString k2::read_k2(void)
{
    qbuf = "";
    bzero(buf, 3);
    // Begin read loop
    while(1==1){
 mutex.lock();
 erno = read(fd, buf, 1);    
 mutex.unlock();
 if(qbuf2.length() > 0)
 {
 qbuf = qbuf2;
 qbuf2 = "";
 }
 if( buf[0] == ';' )
 { 
     return qbuf; 
 }   
 if(erno == 1){
 qbuf.append(buf[0]);        
    }else{
    // Save what we have so far and return
    qbuf2 = qbuf;
    qbuf = "";
    return qbuf;
    }
    }
}

void k2::cmd_k2( char * dtx)
{
    int slen = strlen(dtx);  
    mutex.lock(); 
    write(fd, dtx, slen); 
    mutex.unlock();    
}

Generated by  Doxygen 1.6.0   Back to index