-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndicator.cpp
More file actions
97 lines (82 loc) · 1.72 KB
/
Indicator.cpp
File metadata and controls
97 lines (82 loc) · 1.72 KB
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
#include "stdafx.h"
#include "Indicator.h"
Indicator::Indicator(NLog* log)
{
port = 0;
this->log = log;
SetSecret(0);
}
Indicator::~Indicator()
{
Disconnect();
}
bool Indicator::Connect(CString PortName, LONG baudRate) {
digits = 6;
canText = false;
Disconnect();
port = new COMPort(log);
HRESULT res = port->OpenPort(PortName, baudRate);
if (!SUCCEEDED(res)) {
delete port;
port = 0;
return false;
}
return true;
}
void Indicator::Disconnect() {
//common deinit:
if (port) {
try {
port->ClosePort();
delete port;
}
catch (...) {
}
port = 0;
}
return;
}
void Indicator::Print(CString text) {
if (!port) return;
CString a;
a.Format(TEXT("p %s\n"), text);
int len = WideCharToMultiByte(CP_UTF8,0,a.GetBuffer(),-1,0,0,0,0);
char* buffer = new char[len];
WideCharToMultiByte(CP_UTF8, 0, a.GetBuffer(), -1, buffer, len, 0, 0);
Encode(buffer, len);
port->WriteData(buffer, len);
}
void Indicator::PrintWeight(double weight) {
CString a;
a.Format(TEXT("%.3f"), weight);
Print(a);
}
void Indicator::SetDisplayTime(int millis) {
CString str;
str.Format(TEXT("to %d"), millis);
Print(str);
}
int Indicator::GetDigits()
{
return digits;//must ask for number of digits
}
bool Indicator::CanText()
{
return canText;//must ask capability to write alpha chars
}
void Indicator::Encode(char* buffer, int size) {
for (int i = 0; i<size; i++) {
buffer[i] = buffer[i] ^ key[i % 4];
}
}
void Indicator::Decode(char* buffer, int size) {
for (int i = 0; i<size; i++) {
buffer[i] = buffer[i] ^ key[i % 4];
}
}
void Indicator::SetSecret(DWORD secret) {
this->key[0] = (secret >> 24) & 0xff;
this->key[1] = (secret >> 16) & 0xff;
this->key[2] = (secret >> 8) & 0xff;
this->key[3] = (secret)& 0xff;
}