-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCanTcpDriver.cpp
272 lines (231 loc) · 5.85 KB
/
KCanTcpDriver.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
*
* Copyright (C) 2014 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#if defined(__PYTHON__)
#include <Python.h>
#endif
#define NULL_CHAR 0x1000 /* no character in buffer */
#if defined(__LINUX__) && !defined(__WINE__)
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <stdio.h>
#include <string.h>
#include <sys/timeb.h>
#include "NTypes.h"
#include "NUtils.h"
#include "KCanTcpDriver.h"
KCanTcpDriver::KCanTcpDriver()
{
tel_len = 0;
time_out = 50; // 50 msec
}
KCanTcpDriver::~KCanTcpDriver()
{
Close();
}
bool KCanTcpDriver::Init(const char * TcpAddr)
{
strcpy(dev, "localhost");
if (TcpAddr && strlen(TcpAddr) < High(dev)-2)
strcpy(dev, TcpAddr);
char * port_str = NULL;
unsigned port_nr = 5524;
if (strchr(dev, ':')) // port
{
port_str = strchr(dev, ':');
*port_str++ = 0;
const char * p_str = port_str;
if (!NUtils::GetUnsigned(p_str, port_nr))
return false;
}
SetRemoteHost(TcpAddr);
SetRemotePort(port_nr);
return true;
}
int KCanTcpDriver::ReadFromCOM()
{
int res;
char buffer[32];
bool Read;
if (!KTcpClient::Select(&Read, NULL, NULL, NULL) ||
!KTcpClient::ReadBuffer(buffer, 1))
res = NULL_CHAR;
else
res = (unsigned char) buffer[0];
if (res == NULL_CHAR && tel_len == 0)
{
NUtils::SleepMs(1);
return res;
}
timeb tb;
ftime(&tb);
unsigned now = 1000*(tb.time % 1000000) + tb.millitm;
int div = now - time_stamp;
if (div < 0) // time overflow
div += 1000000000;
if (div > (int) time_out)
tel_len = 0; // clear buffer after timeout
if (res == NULL_CHAR)
return NULL_CHAR;
if (tel_len+1 >= High(tel))
{
tel_len = 0;
return 0x2000;
}
tel[tel_len++] = (unsigned char) res;
tel[tel_len] = 0;
time_stamp = now;
return res;
}
bool KCanTcpDriver::Connect()
{
return KTcpClient::Connect(100);
}
void KCanTcpDriver::Close()
{
KTcpClient::Disconnect();
}
////////////////////////////////////////////////////////////////////////////////
//
// socat -d -d pty,raw,echo=0 tcp-listen:5524,debug,reuseaddr
//
#if defined(__TEST_MICROCHIP__)
// S4: 125 kbit/s
#define INIT_STR "\rC\rX1\rV\rS4\rO\r"
#else
// S1: 20kbit/s
#define INIT_STR "\rC\rX1\rV\rS1\rO\r"
#endif
bool KCan232Tcp::Init(const char * TcpAddr)
{
tel_len = 0;
WaitSentFrame = false;
KCanTcpDriver::Init(TcpAddr);
if (Connect())
{
SetBlockMode(bmNonBlocking);
if (AsSimulation)
return true;
const char * init = INIT_STR;
while (init)
{
const char * p = strstr(init, "\r");
if (!p)
break;
SendBuffer(init, (int)(p - init) + 1);
NUtils::SleepMs(100);
init += p - init + 1;
}
return true;
}
return false;
}
bool KCan232Tcp::ReceiveData(KCanFrame & Frame)
{
int res = ReadFromCOM();
while (res != NULL_CHAR)
{
if (res < NULL_CHAR)
{
switch (char(res))
{
case 7: // bell means error
WaitSentFrame = false;
break;
case 't':
case 'T':
case 'r':
case 'R':
tel_len = 1; // synchronize
tel[0] = (unsigned char) res;
break;
case 'V':
if (AsSimulation)
{
tel_len = 0;
SendBuffer("V1324\r", 6);
NUtils::SleepMs(10);
}
break;
case '\r':
if (tel_len == 2)
{
// USBtin respose for "t..." is z<CR>
// CAN232: 'Z' for switching time stamp
if (tel[0] == 'Z' || tel[0] == 'z')
{
if (WaitSentFrame)
{
tel_len = 0;
WaitSentFrame = false;
Frame = SentFrame;
return true;
}
}
} else
if (tel_len > 1)
{
tel[tel_len-1] = 0;
KCan232Frame can232((char *)tel);
if (can232.SetCanFrame(Frame))
{
NUtils::Time(Frame.TimeStampDay, Frame.TimeStampMs);
tel_len = 0;
if (Trace && AsSimulation)
{
char str[64];
Frame.PrintTime(str, false);
printf("recv: %s %s\n", str, tel);
}
return true;
}
}
tel_len = 0;
break;
default:
break;
}
}
res = ReadFromCOM();
}
NUtils::SleepMs(1);
return false;
}
bool KCan232Tcp::SendData(const KCanFrame & Frame)
{
WaitSentFrame = false;
KCan232Frame can232;
if (can232.GetCanFrame(Frame))
{
strcat(can232.msg, "\r");
if (SendBuffer(can232.msg, (int) strlen(can232.msg)))
{
tel_len = 0;
WaitSentFrame = true;
SentFrame = Frame;
NUtils::Time(SentFrame.TimeStampDay, SentFrame.TimeStampMs);
if (Trace && AsSimulation)
{
char str[64];
SentFrame.PrintTime(str, false);
printf("send: %s %s\n", str, can232.msg);
}
return true;
}
}
return false;
}