/
SerialPortManager.cxx
150 lines (128 loc) · 4.12 KB
/
SerialPortManager.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//============================================================================
//
// K K RRRR OOO K K CCCC OOO M M
// K K R R O O K K C O O MM MM
// KKK RRRR O O KKK C O O M M M "Krokodile Cart software"
// K K R R O O K K C O O M M
// K K R R OOO K K CCCC OOO M M
//
// Copyright (c) 2009-2020 by Stephen Anthony <sa666666@gmail.com>
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include <cstring>
#include "SerialPortManager.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SerialPortManager::SerialPortManager()
{
myPort.setBaud(115200);
myPort.setControlSwap(false);
myPort.closePort();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SerialPortManager::~SerialPortManager()
{
myPort.closePort();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SerialPortManager::setDefaultPort(const string& port)
{
myPortName = port;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SerialPortManager::connectKrokCart()
{
myPort.closePort();
myFoundKrokCart = false;
myVersionID = "";
// Find (and open) the KrokCart serial port
// First try the port that was successful the last time
if(myPortName != "" && connect(myPortName))
{
// myPortName already contains the correct name
}
else // Search through all ports
{
const StringList& ports = myPort.getPortNames();
for(uInt32 i = 0; i < ports.size(); ++i)
{
if(connect(ports[i]))
break;
}
}
// Re-initialize the port; make sure we start in a known state
myPort.closePort();
if(myFoundKrokCart)
myPort.openPort(myPortName);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool SerialPortManager::connect(const string& device)
{
if(myPort.openPort(device))
{
uInt8 tx[100]; // transmit buffer
uInt8 rx[100]; // receive buffer
uInt8 ver[100]; // cart version info
// -------------------------------------------------------------
// Setup "Send Version Info" Command
// -------------------------------------------------------------
memset(rx, 0, 100);
memset(tx, 0, 100);
memset(ver, 0, 100);
tx[0] = 1; // Mark start of command
tx[1] = 2; // Command # for 'Send Version'
// -------------------------------------------------------------
// Search for KrokCart device
// -------------------------------------------------------------
if(myPort.sendBlock(tx, 2) == 0)
{
myPort.closePort();
return false;
}
// Wait for device to respond to command (get ACK)
myPort.receiveBlock(rx, 1);
// -------------------------------------------------------------
// If ACK is not sent, the device is not present
// -------------------------------------------------------------
int VCnt = 0;
int BytesRead = 0;
do
{
BytesRead = myPort.receiveBlock(rx, 1);
ver[VCnt++] = rx[0];
}
while(rx[0] != 0 && VCnt < 100 && BytesRead > 0);
if(VCnt > 10)
myPort.sendBlock(tx, 1); // Send an Ack
myPort.closePort();
if(ver[0] != 0 && VCnt > 10)
{
myFoundKrokCart = true;
myPortName = device;
myVersionID = (const char*)ver;
return true;
}
}
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool SerialPortManager::krokCartAvailable() const
{
return myFoundKrokCart;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SerialPort& SerialPortManager::port()
{
return myPort;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string& SerialPortManager::portName() const
{
return myPortName;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string& SerialPortManager::versionID() const
{
return myVersionID;
}