📄 mainfrm.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace RFConfigTool
{
public partial class MainFrm : Form
{
private string PortName = "COM1";
public MainFrm()
{
InitializeComponent();
}
/// <summary>
/// MAC地址校验
/// </summary>
/// <returns></returns>
private bool MacValidate()
{
if (isEmptyString(this.tbMacAddr.Text.Trim()))
{
MessageBox.Show("地址不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbMacAddr.Focus();
return false;
}
if (this.tbMacAddr.Text.Trim().Length != 6)
{
MessageBox.Show("地址必须是16进制字符串!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbMacAddr.Focus();
return false;
}
uint macAddr;
try
{
macAddr = Convert.ToUInt32(this.tbMacAddr.Text.Trim(), 16);
}
catch (FormatException err)
{
MessageBox.Show("地址必须是16进制字符串!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbMacAddr.Focus();
return false;
}
return true;
}
/// <summary>
/// 域名信息校验
/// </summary>
/// <returns></returns>
private bool DomainValidate()
{
if (isEmptyString(this.tbDomain.Text.Trim()))
{
MessageBox.Show("域名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbDomain.Focus();
return false;
}
if (isEmptyString(this.tbPort.Text))
{
MessageBox.Show("端口不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbPort.Focus();
return false;
}
if (!NumberUtil.IsNonZeroPositiveInteger(this.tbPort.Text.Trim()))
{
MessageBox.Show("端口必需是数字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
this.tbPort.Focus();
return false;
}
return true;
}
/// <summary>
/// 将三个字节的小尾数据转换成一个uint地址
/// </summary>
/// <param name="value"></param>
public static uint ToLittleUint(byte[] value, int pos)
{
uint macAddr = 0;
if (value == null || (value.Length - pos) < 3) return macAddr;
byte[] tmp = new byte[4];
tmp[0] = value[pos + 2];
tmp[1] = value[pos + 1];
tmp[2] = value[pos];
tmp[3] = 0x00;
macAddr = BitConverter.ToUInt32(tmp, 0x00);
return macAddr;
}
public static byte[] ToLittleBytes(uint macAddr)
{
byte[] value = new byte[3];
byte[] tmp = BitConverter.GetBytes(macAddr);
value[0] = tmp[2];
value[1] = tmp[1];
value[2] = tmp[0];
return value;
}
/// <summary>
/// 返回一个整数的16进制表示,小尾表示方法
/// </summary>
/// <param name="value"></param>
/// <param name="isAddr">如果是转换成Mac地址则只取后面的6位</param>
/// <returns></returns>
public static string InttoHexString(uint value, bool isAddr)
{
try
{
byte[] bs = BitConverter.GetBytes(value);
if (bs == null || bs.Length == 0)
{
return null;
}
StringBuilder sb = new StringBuilder(10);
String s = null;
for (int i = bs.Length - 1; i >= 0; i--)
{
s = bs[i].ToString("X2");
sb.Append(s);
}
if (isAddr)
{
return sb.ToString().Substring(2);
}
else
{
return sb.ToString();
}
}
catch (Exception e)
{
//Console.WriteLine(e.Message);
return null;
}
}
/// <remarks>判断一个字符串是否是空(null或者“”)</remarks>
public static bool isEmptyString(string str)
{
if (str == null || str == "")
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 获取校验和
/// </summary>
/// <param name="data"></param>
/// <param name="start">起始位置</param>
/// <param name="offset">偏移量</param>
/// <returns>校验和</returns>
private byte CS(byte[] data, int start, int offset)
{
if (data == null || data.Length < start + offset) return 0x00;
uint check = 0;
for (int i = start; i < start + offset; i++)
{
check += data[i];
}
if (check > 0xff)
{
check = check & (uint)0xff;
}
return (byte)check;
}
private void MainFrm_Load(object sender, EventArgs e)
{
this.cbComPort.SelectedIndex = 0;
this.serialPort1.BaudRate = 2400;
this.serialPort1.DataBits = 8;
this.serialPort1.Handshake = Handshake.None;
this.serialPort1.Parity = Parity.Even;
this.serialPort1.StopBits = StopBits.One;
this.serialPort1.ReadTimeout = 10000;
}
/// <summary>
/// 保存端口设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btSave_Click(object sender, EventArgs e)
{
PortName = string.Format("COM{0}", this.cbComPort.SelectedIndex + 1);
}
/// <summary>
/// 写域名
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DomainWrite_Click(object sender, EventArgs e)
{
if (DomainValidate())
{
try
{
this.serialPort1.PortName = this.PortName;
if (this.serialPort1.IsOpen) this.serialPort1.Close();
this.serialPort1.Open();
byte[] send = new byte[32];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -