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

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

Parent Directory Parent Directory | Revision Log Revision Log


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


1
2 #ifdef _WINDOWS
3 #include "StdAfx.h"
4 #endif
5
6 #include "H7Serial.h"
7 #include <stdexcept>
8 #include <iostream>
9
10 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 using namespace std;
27 cout << "read start" << endl;
28 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 cout << "read mid" << endl;
35 Sleep(50); //be nice and wait a little
36 std::vector<unsigned char> answer = readFrame();
37 cout << "read done" << endl;
38
39 if (answer.size() == 0)
40 throw std::runtime_error("No reply");
41
42 if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
43 throw std::runtime_error("Request not acknowledged");
44
45 if ( (answer[0] & 0xF0) != hi_target)
46 throw std::runtime_error("Incorrect reply");
47
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 using namespace std;
61 unsigned char frame[2];
62 unsigned char hi_target = (target & 0x0F) << 4;
63 frame[0] = hi_target | 0x01;
64 frame[1] = data;
65 cout << "write start" << endl;
66
67 writeFrame(frame,2);
68 cout << "write mid" << endl;
69 Sleep(50); //be nice and wait a little
70 std::vector<unsigned char> answer = readFrame();
71
72 cout << "write stop" << endl;
73 if (answer.size() == 0)
74 throw std::runtime_error("No reply");
75
76 if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK
77 throw std::runtime_error("Request not acknowledged");
78
79 if ( (answer[0] & 0xF0) != hi_target)
80 throw std::runtime_error("Incorrect reply");
81 }
82
83

  ViewVC Help
Powered by ViewVC 1.1.20