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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (hide annotations) (download)
Sun Feb 4 23:11:02 2007 UTC (17 years, 4 months ago) by torben
File size: 1856 byte(s)
First take at making this linux-serial-thingie work...


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

  ViewVC Help
Powered by ViewVC 1.1.20