forked from Anonim17PL/BBI2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
145 lines (122 loc) · 3.51 KB
/
dllmain.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
#include "pch.h"
#include "include/CSerialPort/CSerialPort.h"
#include "include/CSerialPort/CSerialPort.c"
#include "include/SimpleIni/SimpleIni.h"
#include "include/SimpleIni/ConvertUTF.h"
#include "include/Wchar_conv/Wchar_conv.h"
#include "include/Wchar_conv/Wchar_conv.cpp"
#include "include/timercpp.h"
#include <iostream>
using namespace std;
PORT port;
HANDLE mainth;
HANDLE systh;
float val[10000];
float prevval[10000];
float sysval[25];
float prevsysval[25];
string strval[99];
string prevstrval[99];
bool trigger[999];
bool abortthread=false;
int serialport;
int baudrate;
int sl_autosend;
float valsave[10000][1];
unsigned short maxLvar = 0;
unsigned short maxStvar = 0;
unsigned short maxSvar = 0;
DWORD WINAPI MainThread(LPVOID lpParam)
{
SendData(port, ("START:" + to_string(47) + ":" + to_string(11) + '\n').c_str());
while (!abortthread) {
for (int x = 0; x <= maxLvar; x++) {
if (strval[x] != prevstrval[x]) {
SendData(port, ("$V:" + to_string(x) + ":" + strval[x] + '\n').c_str());
prevstrval[x] = strval[x];
}
}
for (int x = 0; x < 10000; x++) {
if (val[x] != prevval[x]) {
SendData(port, ("LV:" + to_string(x) + ":" + to_string(val[x]) + '\n').c_str());
prevval[x] = val[x];
}
}
}
return(0);
}
void czekaj(int iMilisekundy)
{
clock_t koniec = clock() + iMilisekundy * CLOCKS_PER_SEC / 1000.0;
while (clock() < koniec) continue;
}
void loadconfigfile() {
CSimpleIniA ini;
ini.SetUnicode();
SI_Error rc = ini.LoadFile(".\\plugins\\Omsi2Komsi.opl");
if (rc >= 0) {
serialport = atoi(ini.GetValue("OMSI2KOMSI", "COMx"));
baudrate = atoi(ini.GetValue("OMSI2KOMSI", "BAUD_RATE"));
}
}
std::string encode(const std::wstring& wstr, int codePage = GetACP())
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(codePage, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(codePage, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
__declspec(dllexport) void __stdcall PluginStart(void* aOwner)
{
//AllocConsole();
//freopen("CONOUT$", "w", stdout);
// int msgbox = MessageBox(0, "PluginStart", "Debug", MB_OK);
loadconfigfile();
cout << serialport << endl;
if (serialport != 0) {
cout << serialport << endl;
port = OpenPort(serialport);
SetPortBoudRate(port, baudrate);
mainth = CreateThread(NULL, 0, MainThread, NULL, 0, NULL);
}
}
__declspec(dllexport) void __stdcall AccessVariable(unsigned short varindex, float* value, bool* write)
{
if (maxLvar < varindex) {
maxLvar = varindex;
}
if (valsave[varindex][1] == 1) {
valsave[varindex][1] = 0;
*write = 1;
*value = valsave[varindex][0];
}
val[varindex] = round( (float)*value);
//*write = 0;
}
__declspec(dllexport) void __stdcall AccessStringVariable(unsigned short varindex, wchar_t* value, bool* write)
{
if (maxStvar < varindex) {
maxStvar = varindex;
}
strval[varindex] = encode(value);
}
__declspec(dllexport) void __stdcall AccessTrigger(unsigned short triggerindex, bool* active)
{
*active = trigger[triggerindex];
}
__declspec(dllexport) void __stdcall AccessSystemVariable(unsigned short varindex, float* value)
{
if (maxSvar < varindex) {
maxSvar = varindex;
}
sysval[varindex] = (float)*value;
}
__declspec(dllexport) void __stdcall PluginFinalize()
{
abortthread = true;
WaitForSingleObject(mainth, 2000);
CloseHandle(mainth);
ClosePort(port);
}