1 |
|
2 |
#ifdef _WINDOWS
|
3 |
#include "StdAfx.h"
|
4 |
#endif
|
5 |
|
6 |
#include "H7Serial.h"
|
7 |
#include <stdexcept>
|
8 |
|
9 |
CH7Serial::CH7Serial(void)
|
10 |
: CSlipSerial()
|
11 |
{
|
12 |
}
|
13 |
|
14 |
CH7Serial::CH7Serial(char* port, int bitrate)
|
15 |
: CSlipSerial(port, bitrate)
|
16 |
{
|
17 |
}
|
18 |
|
19 |
CH7Serial::~CH7Serial(void)
|
20 |
{
|
21 |
}
|
22 |
|
23 |
short CH7Serial::readTarget(unsigned char target)
|
24 |
{
|
25 |
short retval;
|
26 |
unsigned char frame[1];
|
27 |
unsigned char hi_target = (target & 0x0F) << 4;
|
28 |
frame[0] = hi_target | 0x00;
|
29 |
|
30 |
writeFrame(frame,1);
|
31 |
Sleep(50); //be nice and wait a little
|
32 |
std::vector<unsigned char> answer = readFrame();
|
33 |
|
34 |
if (answer.size() == 0)
|
35 |
throw std::runtime_error("No reply");
|
36 |
|
37 |
if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
|
38 |
throw std::runtime_error("Request not acknowledged");
|
39 |
|
40 |
if ( (answer[0] & 0xF0) != hi_target)
|
41 |
throw std::runtime_error("Incorrect reply");
|
42 |
|
43 |
if (answer.size() == 2)
|
44 |
retval = answer[1];
|
45 |
else if (answer.size() == 3)
|
46 |
retval = (answer[1]<<8) | answer[2];
|
47 |
else
|
48 |
retval = -1;
|
49 |
|
50 |
return retval;
|
51 |
}
|
52 |
|
53 |
void CH7Serial::writeTarget(unsigned char target, unsigned char data)
|
54 |
{
|
55 |
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::runtime_error("No reply");
|
66 |
|
67 |
if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
|
68 |
throw std::runtime_error("Request not acknowledged");
|
69 |
|
70 |
if ( (answer[0] & 0xF0) != hi_target)
|
71 |
throw std::runtime_error("Incorrect reply");
|
72 |
}
|
73 |
|
74 |
|