File: k2.cpp

package info (click to toggle)
marote 2.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 556 kB
  • ctags: 224
  • sloc: cpp: 1,925; makefile: 46
file content (84 lines) | stat: -rw-r--r-- 1,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#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();    
}