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

Contents of /trunk/H7 Server/H7Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download)
Thu Feb 1 07:39:56 2007 UTC (17 years, 3 months ago) by hedin
File size: 1680 byte(s)
Make TcpClientClass responsible for all the serial communication
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 Sleep(5); //forced delay before unlock our sync.object
48 serialLock.Unlock();
49 return retval;
50 }
51
52 void CH7Serial::writeTarget(unsigned char target, unsigned char data)
53 {
54 serialLock.Lock();
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::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
73 serialLock.Unlock();
74 }
75
76

  ViewVC Help
Powered by ViewVC 1.1.20