/[H7]/trunk/H7 Server/H7Serial.cpp
ViewVC logotype

Annotation of /trunk/H7 Server/H7Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (hide annotations) (download)
Thu Feb 1 13:49:01 2007 UTC (17 years, 4 months ago) by hedin
File size: 1676 byte(s)
Found a "feature" more, about the COM port...
Made some comments in the code
1 torben 23 #include "StdAfx.h"
2     #include ".\h7serial.h"
3    
4     CH7Serial::CH7Serial(void)
5     : CSlipSerial()
6     {
7     }
8    
9     CH7Serial::CH7Serial(char* port, int bitrate)
10     : CSlipSerial(port, bitrate)
11     {
12     }
13    
14     CH7Serial::~CH7Serial(void)
15     {
16     }
17    
18     short CH7Serial::readTarget(unsigned char target)
19     {
20 hedin 33 serialLock.Lock();
21 torben 23 short retval;
22     unsigned char frame[1];
23     unsigned char hi_target = (target & 0x0F) << 4;
24     frame[0] = hi_target | 0x00;
25    
26     writeFrame(frame,1);
27 torben 25 Sleep(50); //be nice and wait a little
28 torben 23 std::vector<unsigned char> answer = readFrame();
29    
30     if (answer.size() == 0)
31     throw std::exception("No reply");
32    
33     if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
34     throw std::exception("Request not acknowledged");
35    
36 hedin 39 if ( (answer[0] & 0xF0) != hi_target)
37     throw std::exception("Incorrect reply");
38 torben 23
39 hedin 33
40 torben 23 if (answer.size() == 2)
41     retval = answer[1];
42     else if (answer.size() == 3)
43     retval = (answer[1]<<8) | answer[2];
44     else
45     retval = -1;
46    
47 hedin 35 Sleep(5); //forced delay before unlock our sync.object
48 hedin 33 serialLock.Unlock();
49 torben 23 return retval;
50     }
51    
52     void CH7Serial::writeTarget(unsigned char target, unsigned char data)
53     {
54 hedin 33 serialLock.Lock();
55 torben 23 unsigned char frame[2];
56     unsigned char hi_target = (target & 0x0F) << 4;
57     frame[0] = hi_target | 0x01;
58     frame[1] = data;
59    
60     writeFrame(frame,2);
61     Sleep(50); //be nice and wait a little
62     std::vector<unsigned char> answer = readFrame();
63    
64     if (answer.size() == 0)
65     throw std::exception("No reply");
66    
67     if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
68     throw std::exception("Request not acknowledged");
69    
70     if ( (answer[0] & 0xF0) != hi_target)
71     throw std::exception("Incorrect reply");
72 hedin 33
73     serialLock.Unlock();
74 torben 23 }
75    
76    

  ViewVC Help
Powered by ViewVC 1.1.20