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

Contents of /trunk/H7 Server/H7Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations) (download)
Wed Jan 31 08:41:26 2007 UTC (17 years, 3 months ago) by torben
File size: 1527 byte(s)
Added:
 CSerial - abstraction of a serial port,
 CSlipSerial - (Inherits from CSerial) abstraction of the SLIP datalink protocol
 CH7Serial - (Inherits from CSlipSerial) abstraction of our own protocol
1 #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 short retval;
21 unsigned char frame[1];
22 unsigned char hi_target = (target & 0x0F) << 4;
23 frame[0] = hi_target | 0x00;
24
25 writeFrame(frame,1);
26 Sleep(200); //be nice and wait a little
27 std::vector<unsigned char> answer = readFrame();
28
29 if (answer.size() == 0)
30 throw std::exception("No reply");
31
32 if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
33 throw std::exception("Request not acknowledged");
34
35 if ( (answer[0] & 0xF0) != hi_target)
36 throw std::exception("Incorrect reply");
37
38 if (answer.size() == 2)
39 retval = answer[1];
40 else if (answer.size() == 3)
41 retval = (answer[1]<<8) | answer[2];
42 else
43 retval = -1;
44
45 return retval;
46 }
47
48 void CH7Serial::writeTarget(unsigned char target, unsigned char data)
49 {
50 unsigned char frame[2];
51 unsigned char hi_target = (target & 0x0F) << 4;
52 frame[0] = hi_target | 0x01;
53 frame[1] = data;
54
55 writeFrame(frame,2);
56 Sleep(50); //be nice and wait a little
57 std::vector<unsigned char> answer = readFrame();
58
59 if (answer.size() == 0)
60 throw std::exception("No reply");
61
62 if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
63 throw std::exception("Request not acknowledged");
64
65 if ( (answer[0] & 0xF0) != hi_target)
66 throw std::exception("Incorrect reply");
67 }
68
69

  ViewVC Help
Powered by ViewVC 1.1.20