/[H7]/branches/linux-serial/H7Serial.cpp
ViewVC logotype

Annotation of /branches/linux-serial/H7Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 49 - (hide annotations) (download)
Mon Feb 5 07:06:43 2007 UTC (17 years, 4 months ago) by torben
File size: 1831 byte(s)
Test against the correct #define macro (_WINDOWS -> _MSC_VER)
1 torben 44
2 torben 38 #include "StdAfx.h"
3    
4 torben 44 #include "H7Serial.h"
5     #include <stdexcept>
6 torben 46 #include <iostream>
7 torben 44
8 torben 38 CH7Serial::CH7Serial(void)
9     : CSlipSerial()
10     {
11     }
12    
13     CH7Serial::CH7Serial(char* port, int bitrate)
14     : CSlipSerial(port, bitrate)
15     {
16     }
17    
18     CH7Serial::~CH7Serial(void)
19     {
20     }
21    
22     short CH7Serial::readTarget(unsigned char target)
23     {
24 torben 46 using namespace std;
25     cout << "read start" << endl;
26 torben 38 short retval;
27     unsigned char frame[1];
28     unsigned char hi_target = (target & 0x0F) << 4;
29     frame[0] = hi_target | 0x00;
30    
31     writeFrame(frame,1);
32 torben 46 cout << "read mid" << endl;
33 torben 38 Sleep(50); //be nice and wait a little
34     std::vector<unsigned char> answer = readFrame();
35 torben 46 cout << "read done" << endl;
36 torben 38
37     if (answer.size() == 0)
38 torben 44 throw std::runtime_error("No reply");
39 torben 38
40     if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
41 torben 44 throw std::runtime_error("Request not acknowledged");
42 torben 38
43     if ( (answer[0] & 0xF0) != hi_target)
44 torben 44 throw std::runtime_error("Incorrect reply");
45 torben 38
46     if (answer.size() == 2)
47     retval = answer[1];
48     else if (answer.size() == 3)
49     retval = (answer[1]<<8) | answer[2];
50     else
51     retval = -1;
52    
53     return retval;
54     }
55    
56     void CH7Serial::writeTarget(unsigned char target, unsigned char data)
57     {
58 torben 46 using namespace std;
59 torben 38 unsigned char frame[2];
60     unsigned char hi_target = (target & 0x0F) << 4;
61     frame[0] = hi_target | 0x01;
62     frame[1] = data;
63 torben 46 cout << "write start" << endl;
64 torben 38
65     writeFrame(frame,2);
66 torben 46 cout << "write mid" << endl;
67 torben 38 Sleep(50); //be nice and wait a little
68     std::vector<unsigned char> answer = readFrame();
69 torben 46
70     cout << "write stop" << endl;
71 torben 38 if (answer.size() == 0)
72 torben 44 throw std::runtime_error("No reply");
73 torben 38
74     if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
75 torben 44 throw std::runtime_error("Request not acknowledged");
76 torben 38
77     if ( (answer[0] & 0xF0) != hi_target)
78 torben 44 throw std::runtime_error("Incorrect reply");
79 torben 38 }
80    
81    

  ViewVC Help
Powered by ViewVC 1.1.20