-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
259 lines (210 loc) · 8.19 KB
/
Form1.cs
File metadata and controls
259 lines (210 loc) · 8.19 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
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
using Peak.Can.Basic;
using System.Diagnostics;
namespace PCanMiniView
{
public partial class Form1 : Form
{
private PcanChannel channel;
private Bitrate baudRate;
private PcanStatus result;
private readonly List<CanMessage> canMessages = new List<CanMessage>();
public Form1()
{
InitializeComponent();
selectChennel.SelectedIndex = 0;
selectBaudRate.SelectedIndex = 0;
}
private void ProcessMessage(PcanMessage msg, ulong msgTimeStamp = 0)
{
foreach (CanMessage lstMsg in canMessages)
{
if (lstMsg.Msg.ID == msg.ID && lstMsg.Msg.MsgType == msg.MsgType)
{
lstMsg.UpdateMsg(msg, msgTimeStamp);
return;
}
}
CanMessage canMsg = new CanMessage(msg, msgTimeStamp, msgViewList.Items.Count);
canMessages.Add(canMsg);
ListViewItem newlstItem = msgViewList.Items.Add(canMsg.Msg.MsgType.ToString());
newlstItem.SubItems.Add((canMsg.Msg.MsgType == MessageType.Extended ? canMsg.Msg.ID.ToString("X8") : canMsg.Msg.ID.ToString("X3")) + 'h');
newlstItem.SubItems.Add(canMsg.Msg.Length.ToString());
newlstItem.SubItems.Add(BitConverter.ToString(canMsg.Msg.Data).Replace("-", " "));
newlstItem.SubItems.Add(((canMsg.MsgTime - canMsg.MsgPrevTime) * 0.001).ToString("F1"));
newlstItem.SubItems.Add(canMsg.MsgCount.ToString());
}
private void SendMessage(string id, string len, string data, bool isExtended)
{
PcanMessage msg = new PcanMessage
{
ID = GetIDFromText(id, isExtended),
MsgType = isExtended ? MessageType.Extended : MessageType.Standard,
DLC = Convert.ToByte(len),
Data = new byte[Convert.ToUInt16(len)]
};
byte[] dataByteArr = data.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
for (int i = 0; i < dataByteArr.Length; i++)
{
msg.Data[i] = dataByteArr[i];
}
result = Api.Write(channel, msg);
if (result == PcanStatus.OK)
{
ProcessMessage(msg);
}
else
{
Api.GetErrorText(result, out var errorText);
MessageBox.Show(errorText, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ReadTimer_Tick(object sender, EventArgs e)
{
do
{
result = Api.Read(channel, out PcanMessage msg, out ulong msgTimeStamp);
if (result == PcanStatus.OK)
{
ProcessMessage(msg, msgTimeStamp);
}
else
{
if ((result & PcanStatus.ReceiveQueueEmpty) != PcanStatus.ReceiveQueueEmpty)
{
Api.GetErrorText(result, out var errorText);
Debug.WriteLine(errorText);
break;
}
}
} while ((result & PcanStatus.ReceiveQueueEmpty) != PcanStatus.ReceiveQueueEmpty);
}
private void ViewTimer_Tick(object sender, EventArgs e)
{
ListViewItem lstItem;
foreach (CanMessage lstMsg in canMessages)
{
string[] dataArr = BitConverter.ToString(lstMsg.Msg.Data).Split("-");
lstItem = msgViewList.Items[lstMsg.MsgIdx];
lstItem.SubItems[2].Text = lstMsg.Msg.Length.ToString();
lstItem.SubItems[3].Text = "";
for (int i = 0; i < lstMsg.Msg.Length; i++) lstItem.SubItems[3].Text += dataArr[i] + " ";
lstItem.SubItems[3].Text.Trim();
lstItem.SubItems[4].Text = ((lstMsg.MsgTime - lstMsg.MsgPrevTime) * 0.001).ToString("F1");
lstItem.SubItems[5].Text = lstMsg.MsgCount.ToString();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
readTimer.Stop();
viewTimer.Stop();
Api.Uninitialize(channel);
}
private void MsgSendBtn_Click(object sender, EventArgs e)
{
string writeID = wIDTxBox.Text;
string writeLEN = wLenTxBox.Text;
string writeData = wDataTxBox.Text;
bool isExt = wIsExtendedCkBox.Checked;
if (UtilityHelpers.IsBlank(writeID) || UtilityHelpers.IsBlank(writeLEN) || UtilityHelpers.IsBlank(writeData))
{
MessageBox.Show("ID, 길이, 데이터를 입력해주세요.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SendMessage(writeID, writeLEN, writeData, isExt);
}
private void WLenTxBox_TextChanged(object sender, EventArgs e)
{
int len = Convert.ToInt32(wLenTxBox.Text);
if (len <= 0 || UtilityHelpers.IsBlank(wLenTxBox.Text))
{
len = 0;
wLenTxBox.Text = "0";
wDataTxBox.Enabled = false;
wDataTxBox.ReadOnly = true;
}
else
{
wDataTxBox.Enabled = true;
wDataTxBox.ReadOnly = false;
}
if (len > 8)
{
len = 8;
wLenTxBox.Text = "8";
}
string[] data = new string[len];
for (int i = 0; i < len; i++)
{
data[i] = "00";
}
wDataTxBox.Text = string.Join(" ", data);
}
// 화면 깜빡임 없애기
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
// ID 범위에 맞게 변환
// 11bit : 0x7FF
// 29bit : 0x1FFFFFFF
private uint GetIDFromText(string id, bool isExtended)
{
ulong intId = Convert.ToUInt64(id, 16);
if (!isExtended && intId > 0x7FF)
intId = 0x7FF;
else if (isExtended && intId > 0x1FFFFFFF)
intId = 0x1FFFFFFF;
return (uint)intId;
}
private void wIDTxBox_TextChanged(object sender, EventArgs e)
{
wIDTxBox.Text = GetIDFromText(wIDTxBox.Text, wIsExtendedCkBox.Checked).ToString("X");
}
private void initBtn_Click(object sender, EventArgs e)
{
int selectedChannel = selectChennel.SelectedIndex;
int selectedBaudRate = selectBaudRate.SelectedIndex;
channel = UtilityHelpers.ToPcanChannel(selectedChannel);
baudRate = UtilityHelpers.ToBitrate(selectedBaudRate);
result = Api.Initialize(channel, baudRate);
if (result != PcanStatus.OK)
{
Api.GetErrorText(result, out var errorText);
MessageBox.Show("초기화가 실패했습니다. \n에러메시지: " + errorText, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
channelTxBox.Text = channel.ToString();
baudRateTxBox.Text = baudRate.ToString();
selectBaudRate.Enabled = false;
selectChennel.Enabled = false;
initBtn.Enabled = false;
unInitBtn.Enabled = true;
readTimer.Start();
viewTimer.Start();
}
}
private void unInitBtn_Click(object sender, EventArgs e)
{
readTimer.Stop();
viewTimer.Stop();
Api.Uninitialize(channel);
canMessages.Clear();
msgViewList.Items.Clear();
selectBaudRate.Enabled = true;
selectChennel.Enabled = true;
initBtn.Enabled = true;
unInitBtn.Enabled = false;
}
private void listClearBtn_Click(object sender, EventArgs e)
{
canMessages.Clear();
msgViewList.Items.Clear();
}
}
}