-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.cpp
269 lines (240 loc) · 7.66 KB
/
Unit1.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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "uart.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
void Update_COM_Port_list(void)
{
std::list<_TCHAR*> port_list;
int prev_ItemIndex = Form1->ComboBox_COMPORTS->ItemIndex;
Form1->ComboBox_COMPORTS->Items->Clear();
Get_port_list(port_list);
for (std::list<_TCHAR*>::iterator it = port_list.begin(); it != port_list.end(); it++)
Form1->ComboBox_COMPORTS->Items->Add(*it);
if (port_list.size() > 0) {
if (prev_ItemIndex < 0) {
Form1->ComboBox_COMPORTS->ItemIndex = 0;
} else {
Form1->ComboBox_COMPORTS->ItemIndex = prev_ItemIndex;
}
} else {
Form1->ComboBox_COMPORTS->ItemIndex = -1;
Form1->ComboBox_COMPORTS->Text = L"";
}
Free_port_list(port_list);
}
void Edit_RegDataHEX_changed(void)
{
uint16_t RegData;
UnicodeString temp_str;
int i;
RegData = (uint16_t)wcstoul(Form1->Edit_RegDataHEX->Text.c_str(), NULL, 16);
temp_str = L"";
for(i=0;i<16;i++) {
if (RegData & (1 << i)) {
temp_str.Insert(L"1", 0);
} else {
temp_str.Insert(L"0", 0);
}
}
Form1->Edit_RegDataBIN->Text = temp_str;
temp_str.printf(L"%d", RegData);
Form1->Edit_RegDataDEC->Text = temp_str;
}
void Edit_RegDataBIN_changed(void)
{
uint16_t RegData;
UnicodeString temp_str;
RegData = (uint16_t)wcstoul(Form1->Edit_RegDataBIN->Text.c_str(), NULL, 2);
if (Form1->CheckBox_RegWidth->Checked) temp_str.printf(L"%04x", RegData);
else temp_str.printf(L"%02x", RegData);
Form1->Edit_RegDataHEX->Text = temp_str;
temp_str.printf(L"%d", RegData);
Form1->Edit_RegDataDEC->Text = temp_str;
}
void Edit_RegDataDEC_changed(void)
{
uint16_t RegData;
UnicodeString temp_str;
int i;
RegData = (uint16_t)wcstoul(Form1->Edit_RegDataDEC->Text.c_str(), NULL, 10);
temp_str = L"";
for(i=0;i<16;i++) {
if (RegData & (1 << i)) {
temp_str.Insert(L"1", 0);
} else {
temp_str.Insert(L"0", 0);
}
}
Form1->Edit_RegDataBIN->Text = temp_str;
if (Form1->CheckBox_RegWidth->Checked) temp_str.printf(L"%04x", RegData);
else temp_str.printf(L"%02x", RegData);
Form1->Edit_RegDataHEX->Text = temp_str;
}
void ReadReg(void)
{
int ret;
uint16_t reg_addr;
uint8_t width;
uint16_t reg_data = 0;
UnicodeString temp_str;
Form1->Button_read->Enabled = false;
Form1->Button_write->Enabled = false;
Application->ProcessMessages();
if (Form1->ComboBox_COMPORTS->Text.Length() == 0) {
Form1->Label_status->Caption = L"Âûáåðèòå COM ïîðò";
Form1->Label_status->Font->Color = clRed;
goto exit;
}
Form1->Label_status->Caption = L"×òåíèå ðåãèñòðà...";
Form1->Label_status->Font->Color = clBlue;
Application->ProcessMessages();
if (Form1->Edit_RegAddr->Text.Length() == 0) {
Form1->Label_status->Caption = L"Ââåäèòå àäðåñ ðåãèñòðà";
Form1->Label_status->Font->Color = clRed;
goto exit;
}
reg_addr = (uint16_t)wcstoul(Form1->Edit_RegAddr->Text.c_str(), NULL, 16);
if (Form1->CheckBox_RegWidth->Checked) width = 16;
else width = 8;
ret = Read_Register(Form1->ComboBox_COMPORTS->Text.c_str(), reg_addr, width, ®_data);
if (ret) {
temp_str.printf(L"Îøèáêà %d", ret);
Form1->Label_status->Caption = temp_str;
Form1->Label_status->Font->Color = clRed;
} else {
if (Form1->CheckBox_RegWidth->Checked) temp_str.printf(L"%04x", reg_data);
else temp_str.printf(L"%02x", reg_data);
Form1->Edit_RegDataHEX->Text = temp_str;
Edit_RegDataHEX_changed();
Form1->Label_status->Caption = L"Ðåãèñòð ñ÷èòàí";
Form1->Label_status->Font->Color = clGreen;
}
exit:
Form1->Button_read->Enabled = true;
Form1->Button_write->Enabled = true;
}
void WriteReg(void)
{
int ret;
uint16_t reg_addr;
uint8_t width;
uint16_t reg_data;
UnicodeString temp_str;
Form1->Button_read->Enabled = false;
Form1->Button_write->Enabled = false;
Application->ProcessMessages();
if (Form1->ComboBox_COMPORTS->Text.Length() == 0) {
Form1->Label_status->Caption = L"Âûáåðèòå COM ïîðò";
Form1->Label_status->Font->Color = clRed;
goto exit;
}
Form1->Label_status->Caption = L"Çàïèñü ðåãèñòðà...";
Form1->Label_status->Font->Color = clBlue;
Application->ProcessMessages();
if (Form1->Edit_RegAddr->Text.Length() == 0) {
Form1->Label_status->Caption = L"Ââåäèòå àäðåñ ðåãèñòðà";
Form1->Label_status->Font->Color = clRed;
goto exit;
}
reg_addr = (uint16_t)wcstoul(Form1->Edit_RegAddr->Text.c_str(), NULL, 16);
if (Form1->CheckBox_RegWidth->Checked) width = 16;
else width = 8;
reg_data = (uint16_t)wcstoul(Form1->Edit_RegDataHEX->Text.c_str(), NULL, 16);
ret = Write_Register(Form1->ComboBox_COMPORTS->Text.c_str(), reg_addr, width, reg_data);
if (ret) {
temp_str.printf(L"Îøèáêà %d", ret);
Form1->Label_status->Caption = temp_str;
Form1->Label_status->Font->Color = clRed;
} else {
Form1->Label_status->Caption = L"Ðåãèñòð çàïèñàí";
Form1->Label_status->Font->Color = clGreen;
}
exit:
Form1->Button_read->Enabled = true;
Form1->Button_write->Enabled = true;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Update_COM_Port_list();
Form1->Label_status->Caption = L"";
Form1->Label_status->Font->Color = clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_COMPort_updateClick(TObject *Sender)
{
Update_COM_Port_list();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataHEXKeyPress(TObject *Sender, System::WideChar &Key)
{
if (((Key >= L'0')&&(Key <= L'9'))
||((Key >= L'a')&&(Key <= L'f'))
||(Key == L'\b'))
{
// OK
} else {
Key = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataHEXKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
Edit_RegDataHEX_changed();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataBINKeyPress(TObject *Sender, System::WideChar &Key)
{
if (((Key >= L'0')&&(Key <= L'1'))
||(Key == L'\b'))
{
// OK
} else {
Key = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataBINKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
Edit_RegDataBIN_changed();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataDECKeyPress(TObject *Sender, System::WideChar &Key)
{
if (((Key >= L'0')&&(Key <= L'9'))
||(Key == L'\b'))
{
// OK
} else {
Key = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit_RegDataDECKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
Edit_RegDataDEC_changed();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_readClick(TObject *Sender)
{
ReadReg();
Update_COM_Port_list();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_writeClick(TObject *Sender)
{
WriteReg();
Update_COM_Port_list();
}
//---------------------------------------------------------------------------