📄 form1.cs
字号:
//
this.columnHeader5.Text = "终端出口IP地址";
this.columnHeader5.Width = 100;
//
// columnHeader6
//
this.columnHeader6.Text = "终端出口IP端口";
this.columnHeader6.Width = 100;
//
// tbSend
//
this.tbSend.Location = new System.Drawing.Point(8, 240);
this.tbSend.Name = "tbSend";
this.tbSend.Size = new System.Drawing.Size(544, 21);
this.tbSend.TabIndex = 4;
this.tbSend.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(560, 240);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 24);
this.button3.TabIndex = 5;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(528, 192);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 32);
this.button4.TabIndex = 6;
this.button4.Text = "清除显示";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// tbPort
//
this.tbPort.Location = new System.Drawing.Point(528, 40);
this.tbPort.Name = "tbPort";
this.tbPort.Size = new System.Drawing.Size(88, 21);
this.tbPort.TabIndex = 7;
this.tbPort.Text = "5002";
//
// label1
//
this.label1.Location = new System.Drawing.Point(528, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 8;
this.label1.Text = "端口:";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(536, 160);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(56, 24);
this.checkBox1.TabIndex = 9;
this.checkBox1.Text = "回应";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 413);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbPort);
this.Controls.Add(this.tbSend);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button4_Click(object sender, System.EventArgs e)
{
textBox1.Text = "";
}
private void button1_Click(object sender, System.EventArgs e)
{
SetWorkMode(2);
//启动服务
StringBuilder mess = new StringBuilder(100);
//start_gprs_server(this.Handle, WM_DTU, int.Parse(tbPort.Text), mess);
start_net_service(this.Handle, WM_DTU, int.Parse(tbPort.Text), mess);
textBox1.AppendText(mess.ToString() + "\n");
}
protected override void WndProc(ref Message m)
{
// TODO: 添加 Form1.WndProc 实现
int i;
//响应DTU消息
if (m.Msg == WM_DTU)
{
GPRS_DATA_RECORD recdPtr = new GPRS_DATA_RECORD();
StringBuilder mess = new StringBuilder(100);
//读取DTU数据
do_read_proc(ref recdPtr, mess, checkBox1.Checked);
switch (recdPtr.m_data_type)
{
case 0x01: //注册包
GPRS_USER_INFO user_info = new GPRS_USER_INFO();
ushort usPort;
if (get_user_info(recdPtr.m_userid, ref user_info) == 0)
{
//已经注册过
for (i = 0; i < listView1.Items.Count; i++)
if (listView1.Items[i].Text == user_info.m_userid)
{
listView1.Items[i].SubItems.Add(inet_ntoa(ntohl(user_info.m_local_addr)));
usPort = user_info.m_local_port;
listView1.Items[i].SubItems.Add(usPort.ToString());
listView1.Items[i].SubItems.Add(user_info.m_logon_date);
listView1.Items[i].SubItems.Add(inet_ntoa(ntohl(user_info.m_sin_addr)));
usPort = user_info.m_sin_port;
listView1.Items[i].SubItems.Add(usPort.ToString());
return;
}
//没有注册过
ListViewItem lvi = listView1.Items.Add(user_info.m_userid);
lvi.SubItems.Add(inet_ntoa(ntohl(user_info.m_local_addr)));
usPort = user_info.m_local_port;
lvi.SubItems.Add(usPort.ToString());
lvi.SubItems.Add(user_info.m_logon_date);
lvi.SubItems.Add(inet_ntoa(ntohl(user_info.m_sin_addr)));
usPort = user_info.m_sin_port;
lvi.SubItems.Add(usPort.ToString());
}
break;
case 0x02: //注销包
for (i = 0; i < listView1.Items.Count; i++)
if (listView1.Items[i].Text == recdPtr.m_userid)
{
listView1.Items[i].Remove();
break;
}
break;
case 0x04: //无效包
break;
case 0x09: //数据包
textBox1.AppendText(recdPtr.m_data_buf + "\n");
break;
default:
break;
}
}
else
{
//缺省消息处理
base.WndProc (ref m);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
StringBuilder mess = new StringBuilder(100);
//停止服务
//stop_gprs_server(mess);
stop_net_service(mess);
textBox1.AppendText(mess.ToString());
}
private void button3_Click(object sender, System.EventArgs e)
{
StringBuilder mess = new StringBuilder(100);
int i;
//这段代码是发送文本框的内容,即ASCII字符
for (i = 0; i < listView1.Items.Count; i++)
if (listView1.Items[i].Checked)
{
//注意对中文的处理,TextBox取得的字符串会把中文当一个字符,导致发送的字节数减少。我这里没有处理这个
//使用System.Text.Encoding.Default.GetByteCount可以获得字节数
if (do_send_user_data(listView1.Items[i].Text, tbSend.Text, System.Text.Encoding.Default.GetByteCount(tbSend.Text), mess) == -1)
textBox1.AppendText(mess.ToString());
}
//////////////////////////////////////////////////////////////////////////
/*
* 这段代码是字节数组的传送例子,数组的值自己填,我这里采用固定的值
* 经测试,传送的数据没有问题。
*/
/*
string s = new string((char)0, 300); //分配300字节长度的字符串数组
byte[] bc = System.Text.Encoding.Default.GetBytes(s); //转换到字节数组
for (i = 0; i < 300; i++) //通过字节数组进行赋值
bc[i] = (byte)(i * 2);
string ss = System.Text.Encoding.Default.GetString(bc, 0, 300); //将该字节数组转换到字符串进行传送
*/
/*
* 注意:
* 字符串s的作用只是分配空间,
* 字节数组bc的作用是赋值,赋值时要注意类型转换
* 字符串ss的作用是作为do_send_user_data的参数
*/
/*
for (i = 0; i < listView1.Items.Count; i++)
if (listView1.Items[i].Checked)
{
//发送全部300字节
if (do_send_user_data(listView1.Items[i].Text, ss, 300, mess) == -1)
textBox1.AppendText(mess.ToString());
}
*/
/*
* 调试的时候要注意char 和 byte类型的区别,char 显示大于127的值时会显示负数
* 下面是DTU收到的数据,显示为16进制
* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19
* 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33
* 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D
* 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67
* 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81
* 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B
* 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5
* B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF
* D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9
* EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 00 01 02 03
* 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
* 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B
*/
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -