forked from andig/canprogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKTcpCanClient.cpp
executable file
·141 lines (120 loc) · 3.19 KB
/
KTcpCanClient.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
/*
*
* 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__WINDOWS__)
#include <windows.h>
#endif
#if defined(__LINUX__)
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "NTypes.h"
#include "KTcpCanClient.h"
KTcpCanClient::KTcpCanClient()
{
UseBinaryProtocol = false;
IsServer = true;
}
bool KTcpCanClient::ReadElster(char * Buffer)
{
int Read = ReceiveBuf(Buffer, 1, 0, mReadTimeout);
if (Read == 1 && *Buffer != '\n') // auf LF synchronisieren
{
for (int Tot = Read ; Tot < 3*12; Tot++)
{
Read = ReceiveBuf(Buffer + Tot, 1, 0, 5);
if (Read != 1)
{
Buffer[Tot] = 0;
if (mTrace)
printf("broken %s\n", Buffer);
return false;
}
if (Buffer[Tot] == '\n')
{
Buffer[Tot + 1] = 0;
return true;
}
}
Buffer[3*12] = 0;
if (mTrace)
printf("no LF %s\n", Buffer);
}
return false;
}
bool KTcpCanClient::ReadElster(KComfortFrame & recv)
{
char Buffer[128];
recv.SetOk(false);
int Read = ReceiveBuf(Buffer, 1, 0, mReadTimeout);
if (Read == 1 && (!IsServer || *Buffer == 0x0d))
{
for (int Tot = Read ; Tot < 12; Tot++)
{
Read = ReceiveBuf(Buffer + Tot, 1, 0, 5);
if (Read != 1)
{
return false;
}
}
memcpy(recv.Data, Buffer, sizeof(recv.Data));
bool Ok = recv.CheckSum();
if (!Ok && mTrace)
printf("recv: checksum error\n");
return Ok;
}
return false;
}
bool KTcpCanClient::SendElster(char * Buffer, int timeout)
{
while (strlen(Buffer) && Buffer[strlen(Buffer)-1] == ' ')
Buffer[strlen(Buffer)-1] = 0;
strcat(Buffer, "\n");
return SendBuffer(Buffer, (int) strlen(Buffer), timeout);
}
bool KTcpCanClient::SendElster(const KComfortFrame & send, int timeout)
{
return SendBuffer((const char *) send.Data, sizeof(send.Data), timeout);
}
bool KTcpCanClient::SendComfortFrame(const KComfortFrame & send, KComfortFrame & recv)
{
bool Ok = false;
char str[128];
if (!UseBinaryProtocol)
{
send.SetToString(str);
strcat(str, "\n");
}
Connect(1000);
if (UseBinaryProtocol)
{
if (SendElster(send, 1000))
Ok = ReadElster(recv);
} else {
if (SendElster(str, 1000)) // 1000 ms timeout
Ok = ReadElster(str);
if (Ok)
Ok = recv.SetFromString(str, true);
}
Disconnect();
return Ok;
}