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

Contents of /trunk/H7 Server/H7Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations) (download)
Wed Jan 31 17:22:59 2007 UTC (17 years, 3 months ago) by hedin
File size: 1623 byte(s)
Made the selection of baud rate work (well, make everything work - including multiple clients)
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 serialLock.Lock();
21 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 Sleep(50); //be nice and wait a little
28 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 // if ( (answer[0] & 0xF0) != hi_target)
37 // throw std::exception("Incorrect reply");
38
39
40 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 serialLock.Unlock();
48 return retval;
49 }
50
51 void CH7Serial::writeTarget(unsigned char target, unsigned char data)
52 {
53 serialLock.Lock();
54 unsigned char frame[2];
55 unsigned char hi_target = (target & 0x0F) << 4;
56 frame[0] = hi_target | 0x01;
57 frame[1] = data;
58
59 writeFrame(frame,2);
60 Sleep(50); //be nice and wait a little
61 std::vector<unsigned char> answer = readFrame();
62
63 if (answer.size() == 0)
64 throw std::exception("No reply");
65
66 if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
67 throw std::exception("Request not acknowledged");
68
69 if ( (answer[0] & 0xF0) != hi_target)
70 throw std::exception("Incorrect reply");
71
72 serialLock.Unlock();
73 }
74
75

  ViewVC Help
Powered by ViewVC 1.1.20