-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectionPanel.cpp
More file actions
196 lines (136 loc) · 4.87 KB
/
connectionPanel.cpp
File metadata and controls
196 lines (136 loc) · 4.87 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
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
#include "connectionPanel.h"
wxBEGIN_EVENT_TABLE(connectionPanel, wxPanel)
EVT_BUTTON(10001, ConnButtonClicked)
EVT_BUTTON(10002, RefreshButtonClicked)
EVT_BUTTON(10003, DisconnButtonClicked)
EVT_BUTTON(10004, ClearRawBox)
EVT_BUTTON(10005, SaveRawOutput)
wxEND_EVENT_TABLE()
connectionPanel::connectionPanel(wxFrame* parent, dataPanel* new_rawPanel, COM_Communication* new_comHandle) : wxPanel(parent, wxID_ANY, wxPoint(0, 0), wxSize(250, 350))
{
this->SetBackgroundColour(wxColor(39, 39, 39));
rawPanel = new_rawPanel;
comHandle = new_comHandle;
t_COMTitle = new wxStaticText(this, wxID_ANY, "Select COM port ");
t_COMTitle->SetForegroundColour(wxColor(245, 247, 250));
c_ComSelector = new wxChoice(this, 20001, wxDefaultPosition, wxSize(250, 30));
t_BaudTitle = new wxStaticText(this, wxID_ANY, "Select Baudrate ");
t_BaudTitle->SetForegroundColour(wxColor(245, 247, 250));
c_BaudSelector = new wxChoice(this, 20001, wxDefaultPosition, wxSize(250, 30));
wxFont connFont = t_COMTitle->GetFont();
connFont.SetPointSize(connFont.GetPointSize() + 1);
t_COMTitle->SetFont(connFont);
t_BaudTitle->SetFont(connFont);
FillComboBoxes(); // Fill selectors with stuff
// -------------------------- Buttons -------------------------- //
b_BtnConn = new wxButton(this, 10001, "Connect", wxDefaultPosition, wxSize(250, 30));
b_BtnRefresh = new wxButton(this, 10002, "Refresh COM ports", wxDefaultPosition, wxSize(250, 30));
b_BtnDisconn = new wxButton(this, 10003, "Disconnect", wxDefaultPosition, wxSize(250, 30));
b_BtnClearRaw = new wxButton(this, 10004, "Clear Raw Box", wxDefaultPosition, wxSize(250, 30));
b_BtnSaveOutput = new wxButton(this, 10005, "Save Raw Output", wxDefaultPosition, wxSize(250, 30));
// --------------------------- Sizer --------------------------- //
s_connectlSizer = new wxBoxSizer(wxVERTICAL);
s_connectlSizer->Add(t_COMTitle, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(c_ComSelector, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(t_BaudTitle, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(c_BaudSelector, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(b_BtnConn, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(b_BtnRefresh, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(b_BtnDisconn, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(b_BtnClearRaw, 0, wxEXPAND | wxALL, 5);
s_connectlSizer->Add(b_BtnSaveOutput, 0, wxEXPAND | wxALL, 5);
this->SetSizer(s_connectlSizer);
}
connectionPanel::~connectionPanel()
{
}
// -------------------------- Button handlers -------------------------- //
void connectionPanel::ConnButtonClicked(wxCommandEvent& evt)
{
comHandle->ConnectHandle(c_ComSelector->GetStringSelection());
rawPanel->AddTextToBox(comHandle->GetStatus());
comHandle->ConfigureComPort(wxAtoi(c_BaudSelector->GetStringSelection()));
rawPanel->AddTextToBox(comHandle->GetStatus());
comHandle->SetTimeouts();
rawPanel->AddTextToBox(comHandle->GetStatus());
comHandle->InitCOMReader(rawPanel);
}
void connectionPanel::DisconnButtonClicked(wxCommandEvent& evt)
{
comHandle->DisconnectHandle();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
rawPanel->AddTextToBox("\nDisconnected!\n");
evt.Skip();
}
void connectionPanel::ClearRawBox(wxCommandEvent& evt)
{
rawPanel->ClearTextBox();
}
void connectionPanel::SaveRawOutput(wxCommandEvent& evt)
{
rawPanel->SaveRawOutput();
}
void connectionPanel::RefreshButtonClicked(wxCommandEvent& evt)
{
c_ComSelector->Clear();
c_BaudSelector->Clear();
FillComboBoxes();
rawPanel->AddTextToBox("COM ports refreshed!");
}
// ------------------------ Non button functions ------------------------ //
void connectionPanel::FillComboBoxes()
{
std::list<int> portsList = GetAvailableComPorts();
for (int item : portsList)
{
c_ComSelector->AppendString("COM" + std::to_string(item));
}
c_ComSelector->SetSelection(0);
for (int item : BaudRates)
{
c_BaudSelector->AppendString(std::to_string(item));
}
c_BaudSelector->SetSelection(6);
}
std::list<int> connectionPanel::GetAvailableComPorts()
{
wchar_t szDevices[1024];
std::list<int> portList;
for (int i = 0; i < 255; i++)
{
std::wstring str = L"COM" + std::to_wstring(i);
DWORD dwRet = QueryDosDeviceW(
str.c_str(),
szDevices,
1024
);
if (dwRet != 0)
{
portList.push_back(i);
}
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
std::cout << "ERROR_INSUFFICIENT_BUFFER";
}
}
return portList;
}
std::string connectionPanel::get_desktop_path()
{
TCHAR desktopPath[MAX_PATH];
if (SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, 0, desktopPath) != S_OK) {
//t_DataText->AppendText("Error in getting desktop path");
}
std::wstring desktopPathW(desktopPath);
std::string desktopPathA(desktopPathW.begin(), desktopPathW.end());
std::string cleanDesktop;
for (char c : desktopPathA) {
if (c == '\\') {
cleanDesktop += "\\\\";
}
else {
cleanDesktop += c;
}
}
return cleanDesktop;
}