适合初学者
软件下载地址
comtestnovateloem7串口配置软件指令学习软件数据处理分析软件-C#文档类资源-优快云下载
显示串口数据时,最好用定时更新的方式,在加上异步委托,避免界面卡死等情况。
private void disp(object source, System.Timers.ElapsedEventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
this.BeginInvoke((EventHandler)(delegate //控件异步委托,BeginInvoke不会阻止主线程,等当前线程之后再执行BeginInvoke
{
byte[] buf1 = new byte[al.Count];
al.CopyTo(buf1);
textBox2.AppendText(Encoding.ASCII.GetString(buf1));
al.Clear();
textBox2.ClearUndo();
}));
}
功能区相关的部分,以下状态字供参考
//状态字(Receiver Error)
if ((byteArray9 & 0x1) > 0) { Form3_F3.textBox1.AppendText("DRAM存储器错误\r\n"); }
if ((byteArray9 & 0x2) > 0) { Form3_F3.textBox1.AppendText("固件错误\r\n"); }
if ((byteArray9 & 0x4) > 0) { Form3_F3.textBox1.AppendText("ROM存储器错误\r\n"); }
if ((byteArray9 & 0x10) > 0) { Form3_F3.textBox1.AppendText("电子序列号错误\r\n"); }
if ((byteArray9 & 0x20) > 0) { Form3_F3.textBox1.AppendText("授权码错误\r\n"); }
if ((byteArray9 & 0x80) > 0) { Form3_F3.textBox1.AppendText("供电错误\r\n"); }
if ((byteArray9 & 0x100) > 0) { Form3_F3.textBox1.AppendText("温度计异常\r\n"); }
if ((byteArray9 & 0x200) > 0) { Form3_F3.textBox1.AppendText("温度异常\r\n"); }
if ((byteArray9 & 0x400) > 0) { Form3_F3.textBox1.AppendText("MINOS状态异常\r\n"); }
if ((byteArray9 & 0x800) > 0) { Form3_F3.textBox1.AppendText("RF射频锁相环硬件错误\r\n"); }
if ((byteArray9 & 0x1000) > 0) { Form3_F3.textBox1.AppendText("RF2射频锁相环硬件错误\r\n"); }
if ((byteArray9 & 0x2000) > 0) { Form3_F3.textBox1.AppendText("RF3射频锁相环硬件错误\r\n"); }
if ((byteArray9 & 0x4000) > 0) { Form3_F3.textBox1.AppendText("RF4射频锁相环硬件错误\r\n"); }
if ((byteArray9 & 0x8000) > 0) { Form3_F3.textBox1.AppendText("NVM非易失存储器错误\r\n"); }
if ((byteArray9 & 0x10000) > 0) { Form3_F3.textBox1.AppendText("软件资源限制\r\n"); }
if ((byteArray9 & 0x20000) > 0) { Form3_F3.textBox1.AppendText("Model不可用\r\n"); }
if ((byteArray9 & 0x100000) > 0) { Form3_F3.textBox1.AppendText("开始远程加载\r\n"); }
if ((byteArray9 & 0x200000) > 0) { Form3_F3.textBox1.AppendText("出口限制\r\n"); }
if ((byteArray9 & 0x400000) > 0) { Form3_F3.textBox1.AppendText("安全模式\r\n"); }
if ((byteArray9 & 0x80000000) > 0) { Form3_F3.textBox1.AppendText("硬件错误\r\n"); }
//状态字(Receiver Status)
if ((byteArray7 & 0x1) > 0) { Form3_F3.textBox1.AppendText("接收机存在异常\r\n"); }
if ((byteArray7 & 0x2) > 0) { Form3_F3.textBox1.AppendText("温度异常\r\n"); }
if ((byteArray7 & 0x4) > 0) { Form3_F3.textBox1.AppendText("供电错误\r\n"); }
if ((byteArray7 & 0x8) > 0) { Form3_F3.textBox1.AppendText("主天线供电异常\r\n"); }
if ((byteArray7 & 0x10) > 0) { Form3_F3.textBox1.AppendText("天线LNA异常r\n"); }
if ((byteArray7 & 0x20) > 0) { Form3_F3.textBox1.AppendText("主天线开路\r\n"); }
if ((byteArray7 & 0x40) > 0) { Form3_F3.textBox1.AppendText("主天线短路\r\n"); }
if ((byteArray7 & 0x80) > 0) { Form3_F3.textBox1.AppendText("CPU超负荷\r\n"); }
if ((byteArray7 & 0x100) > 0) { Form3_F3.textBox1.AppendText("COM口发送缓存区溢出\r\n"); }
if ((byteArray7 & 0x800) > 0) { Form3_F3.textBox1.AppendText("链路溢出\r\n"); }
if ((byteArray7 & 0x1000) > 0) { Form3_F3.textBox1.AppendText("COM口输入缓存区溢出\r\n"); }
if ((byteArray7 & 0x2000) > 0) { Form3_F3.textBox1.AppendText("Aux发送溢出\r\n"); }
if ((byteArray7 & 0x4000) > 0) { Form3_F3.textBox1.AppendText("天线自动增益控制超限\r\n"); }
if ((byteArray7 & 0x8000) > 0) { Form3_F3.textBox1.AppendText("射频干扰\r\n"); }
if ((byteArray7 & 0x10000) > 0) { Form3_F3.textBox1.AppendText("INS重启\r\n"); }
if ((byteArray7 & 0x20000) > 0) { Form3_F3.textBox1.AppendText("IMU通信失败\r\n"); }
if ((byteArray7 & 0x40000) > 0) { Form3_F3.textBox1.AppendText("未知的历书或UTC时间\r\n"); }
if ((byteArray7 & 0x80000) > 0) { Form3_F3.textBox1.AppendText("无法定位\r\n"); }
if ((byteArray7 & 0x100000) > 0) { Form3_F3.textBox1.AppendText("使用fix指令设置了固定位置\r\n"); }
if ((byteArray7 & 0x200000) > 0) { Form3_F3.textBox1.AppendText("时钟锁定异常\r\n"); }
if ((byteArray7 & 0x400000) > 0) { Form3_F3.textBox1.AppendText("时钟模式异常\r\n"); }
if ((byteArray7 & 0x800000) > 0) { Form3_F3.textBox1.AppendText("外部时钟锁定\r\n"); }
if ((byteArray7 & 0x1000000) > 0) { Form3_F3.textBox1.AppendText("软件资源超限\r\n"); }
if ((byteArray7 & 0x8000000) > 0) { Form3_F3.textBox1.AppendText("HDR跟踪模式\r\n"); }
if ((byteArray7 & 0x10000000) > 0) { Form3_F3.textBox1.AppendText("使能数字滤波\r\n"); }
if ((byteArray7 & 0x20000000) > 0) { Form3_F3.textBox1.AppendText("Auxiliary 3状态事件标记\r\n"); }
if ((byteArray7 & 0x40000000) > 0) { Form3_F3.textBox1.AppendText("Auxiliary 2状态事件标记\r\n"); }
if ((byteArray7 & 0x80000000) > 0) { Form3_F3.textBox1.AppendText("Auxiliary 1状态事件标记\r\n"); }
//Auxiliary 1 status
if ((byteArray15 & 0x1) > 0) { Form3_F3.textBox1.AppendText("RF1射频干扰\r\n"); }
if ((byteArray15 & 0x2) > 0) { Form3_F3.textBox1.AppendText("RF2射频干扰\r\n"); }
if ((byteArray15 & 0x4) > 0) { Form3_F3.textBox1.AppendText("RF3射频干扰\r\n"); }
if ((byteArray15 & 0x8) > 0) { Form3_F3.textBox1.AppendText("位置平均\r\n"); }
if ((byteArray15 & 0x10) > 0) { Form3_F3.textBox1.AppendText("RF4射频干扰\r\n"); }
if ((byteArray15 & 0x20) > 0) { Form3_F3.textBox1.AppendText("RF5射频干扰\r\n"); }
if ((byteArray15 & 0x40) > 0) { Form3_F3.textBox1.AppendText("RF6射频干扰\r\n"); }
if ((byteArray15 & 0x80) > 0) { Form3_F3.textBox1.AppendText("USB未连接\r\n"); }
if ((byteArray15 & 0x100) > 0) { Form3_F3.textBox1.AppendText("USB1缓存区溢出\r\n"); }
if ((byteArray15 & 0x200) > 0) { Form3_F3.textBox1.AppendText("USB2缓存区溢出\r\n"); }
if ((byteArray15 & 0x400) > 0) { Form3_F3.textBox1.AppendText("USB3缓存区溢出\r\n"); }
if ((byteArray15 & 0x1000) > 0) { Form3_F3.textBox1.AppendText("配置文件错误\r\n"); }
if ((byteArray15 & 0x4000) > 0) { Form3_F3.textBox1.AppendText("RF1自动增益控制超限\r\n"); }
if ((byteArray15 & 0x8000) > 0) { Form3_F3.textBox1.AppendText("RF2自动增益控制超限\r\n"); }
if ((byteArray15 & 0x10000) > 0) { Form3_F3.textBox1.AppendText("RF3自动增益控制超限\r\n"); }
if ((byteArray15 & 0x20000) > 0) { Form3_F3.textBox1.AppendText("RF4自动增益控制超限\r\n"); }
if ((byteArray15 & 0x40000) > 0) { Form3_F3.textBox1.AppendText("以太网未连接\r\n"); }
if ((byteArray15 & 0x80000) > 0) { Form3_F3.textBox1.AppendText("ICOM1缓存区溢出\r\n"); }
if ((byteArray15 & 0x100000) > 0) { Form3_F3.textBox1.AppendText("ICOM2缓存区溢出\r\n"); }
if ((byteArray15 & 0x200000) > 0) { Form3_F3.textBox1.AppendText("ICOM3缓存区溢出\r\n"); }
if ((byteArray15 & 0x400000) > 0) { Form3_F3.textBox1.AppendText("NCOM1缓存区溢出\r\n"); }
if ((byteArray15 & 0x800000) > 0) { Form3_F3.textBox1.AppendText("NCOM2缓存区溢出\r\n"); }
if ((byteArray15 & 0x1000000) > 0) { Form3_F3.textBox1.AppendText("NCOM3缓存区溢出\r\n"); }
if ((byteArray15 & 0x2000000) > 0) { Form3_F3.textBox1.AppendText("XCOM1缓存区溢出\r\n"); }
if ((byteArray15 & 0x4000000) > 0) { Form3_F3.textBox1.AppendText("XCOM2缓存区溢出\r\n"); }
if ((byteArray15 & 0x8000000) > 0) { Form3_F3.textBox1.AppendText("XCOM3缓存区溢出\r\n"); }
if ((byteArray15 & 0x10000000) > 0) { Form3_F3.textBox1.AppendText("RF5自动增益控制超限\r\n"); }
if ((byteArray15 & 0x20000000) > 0) { Form3_F3.textBox1.AppendText("RF6自动增益控制超限\r\n"); }
//Auxiliary 2 status
if ((byteArray19 & 0x1) > 0) { Form3_F3.textBox1.AppendText("SPI通讯异常\r\n"); }
if ((byteArray19 & 0x2) > 0) { Form3_F3.textBox1.AppendText("I2C通讯异常\r\n"); }
if ((byteArray19 & 0x4) > 0) { Form3_F3.textBox1.AppendText("COM4缓存区溢出\r\n"); }
if ((byteArray19 & 0x8) > 0) { Form3_F3.textBox1.AppendText("COM5缓存区溢出\r\n"); }
if ((byteArray19 & 0x10) > 0) { Form3_F3.textBox1.AppendText("COM6缓存区溢出\r\n"); }
if ((byteArray19 & 0x20) > 0) { Form3_F3.textBox1.AppendText("COM7缓存区溢出\r\n"); }
if ((byteArray19 & 0x40) > 0) { Form3_F3.textBox1.AppendText("COM8缓存区溢出\r\n"); }
if ((byteArray19 & 0x80) > 0) { Form3_F3.textBox1.AppendText("COM9缓存区溢出\r\n"); }
if ((byteArray19 & 0x100) > 0) { Form3_F3.textBox1.AppendText("COM10缓存区溢出\r\n"); }
if ((byteArray19 & 0x200) > 0) { Form3_F3.textBox1.AppendText("COM1缓存区溢出\r\n"); }
if ((byteArray19 & 0x400) > 0) { Form3_F3.textBox1.AppendText("COM2缓存区溢出\r\n"); }
if ((byteArray19 & 0x800) > 0) { Form3_F3.textBox1.AppendText("COM3缓存区溢出\r\n"); }
if ((byteArray19 & 0x1000) > 0) { Form3_F3.textBox1.AppendText("PLL RF1未锁定\r\n"); }
if ((byteArray19 & 0x2000) > 0) { Form3_F3.textBox1.AppendText("PLL RF2未锁定\r\n"); }
if ((byteArray19 & 0x4000) > 0) { Form3_F3.textBox1.AppendText("PLL RF3未锁定\r\n"); }
if ((byteArray19 & 0x8000) > 0) { Form3_F3.textBox1.AppendText("PLL RF4未锁定\r\n"); }
if ((byteArray19 & 0x10000) > 0) { Form3_F3.textBox1.AppendText("PLL RF5未锁定\r\n"); }
if ((byteArray19 & 0x20000) > 0) { Form3_F3.textBox1.AppendText("PLL RF6未锁定\r\n"); }
if ((byteArray19 & 0x80000000) > 0) { Form3_F3.textBox1.AppendText("设备重启\r\n"); }
//Auxiliary 3 status
if ((byteArray23 & 0x2) > 0) { Form3_F3.textBox1.AppendText("WCOM1缓存区溢出\r\n"); }
if ((byteArray23 & 0x4) > 0) { Form3_F3.textBox1.AppendText("FILE缓存区溢出\r\n"); }
if ((byteArray23 & 0x8) > 0) { Form3_F3.textBox1.AppendText("蓝牙连接缓存区溢出\r\n"); }
if ((byteArray23 & 0x100) > 0) { Form3_F3.textBox1.AppendText("Wi-Fi供电正常\r\n"); }
if ((byteArray23 & 0x200) > 0) { Form3_F3.textBox1.AppendText("Wi-Fi AP模式\r\n"); }
if ((byteArray23 & 0x400) > 0) { Form3_F3.textBox1.AppendText("Wi-Fi连接正常\r\n"); }
if ((byteArray23 & 0x800) > 0) { Form3_F3.textBox1.AppendText("蓝牙供电正常\r\n"); }
if ((byteArray23 & 0x1000) > 0) { Form3_F3.textBox1.AppendText("蓝牙SPP连接\r\n"); }
if ((byteArray23 & 0x2000) > 0) { Form3_F3.textBox1.AppendText("蜂窝网模块供电正常\r\n"); }
if ((byteArray23 & 0x4000) > 0) { Form3_F3.textBox1.AppendText("SIM卡无法识别\r\n"); }
if ((byteArray23 & 0x8000) > 0) { Form3_F3.textBox1.AppendText("SIM卡锁定\r\n"); }
if ((byteArray23 & 0x10000) > 0) { Form3_F3.textBox1.AppendText("蜂窝网处于漫游服务区域\r\n"); }
if ((byteArray23 & 0x20000) > 0) { Form3_F3.textBox1.AppendText("蜂窝网无服务\r\n"); }
if ((byteArray23 & 0x200000) > 0) { Form3_F3.textBox1.AppendText("双天线供电异常\r\n"); }
if ((byteArray23 & 0x400000) > 0) { Form3_F3.textBox1.AppendText("内部应用程序重启\r\n"); }
if ((byteArray23 & 0x1000000) > 0) { Form3_F3.textBox1.AppendText("双卡连接错误\r\n"); }
if ((byteArray23 & 0x2000000) > 0) { Form3_F3.textBox1.AppendText("VDC输出异常\r\n"); }
if ((byteArray23 & 0x4000000) > 0) { Form3_F3.textBox1.AppendText("U盘异常\r\n"); }
if ((byteArray23 & 0x8000000) > 0) { Form3_F3.textBox1.AppendText("内部存储器异常\r\n"); }
HEX视图显示功能
//处理16进制显示
if (checkBoxRHex.Checked)
{
foreach (byte b in buf)
//foreach为对象集合中的每个元素重复一个操作
{
buf1 += b.ToString("X2") + " ";
//X表示的是16进制,数字2表示显示位数
}
buf2 = System.Text.Encoding.Default.GetBytes(buf1);
ThreadMethodTxt(buf2);
}
//实时修改波特率+实时修改串口号
private void comboBaudrate_TextChanged(object sender, EventArgs e)
{
if (!comm.IsOpen)
{
return;
}
else
{
if (comm.IsOpen)
{
comm.Close();
checkBox_send.Enabled = false;
comm.PortName = comboPortNme.Text;
try
{
comm.BaudRate = int.Parse(comboBaudrate.Text);
}
catch (Exception ex)
{
MessageBox.Show("波特率不正确:" + comm.BaudRate + "!");
comboBaudrate.Text = comm.BaudRate.ToString();
}
try
{
comm.Open();
checkBox_send.Enabled = true;
checkBox1.Enabled = true;
}
catch (Exception ex)
{
comm = new SerialPort();
MessageBox.Show(ex.Message);
}
}
this.buttonOpenport.Text = comm.IsOpen ? "关闭端口" : "打开端口";
this.buttonSend.Enabled = comm.IsOpen;
}
}
检测USB转串口插入或者被拔出,没有使用这个功能
//protected override void DefWndProc(ref Message m)
//{
// if (m.Msg == 0x0219)//WM_DEVICECHANGE
// {
// switch (m.WParam.ToInt32())
// {
// case 0x8000://DBT_DEVICEARRIVAL USB插入
// {
// //MessageBox.Show("USB插入!");
// //if (comm.IsOpen)
// //{
// // MessageBox.Show("USB连接成功,请选择串口!");
// //}
// try
// {
// buttonOpenport.Text = "打开串口";
// comboPortNme.Text = "";
// }
// catch (Exception ex)
// {
// }
// return;
// }
// case 0x8004://DBT_DEVICEREMOVECOMPLETE
// {
// //MessageBox.Show("USB被拔出,请关闭此程序!");
// if (comm.IsOpen)
// {
// try
// {
// //comm.Close();
// buttonOpenport.Text = "打开串口";
// comboPortNme.Text = "";
// }
// catch (Exception ex)
// {
// MessageBox.Show("USB被拔出,请重新选择串口!");
// buttonOpenport.Text = "打开串口";
// comboPortNme.Text = "";
// }
// }
// return;
// }
// }
// }
// base.DefWndProc(ref m);
//}
C#文本框中默认是不允许使用ctrl+A这类的快捷键
/// 加入快捷键功能,加入以下代码
private void ContentBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
textBox2.SelectAll();
}
private void SendBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)//使用CTRL+A全选
SendBox.SelectAll();
if (checkBoxSHex.Checked)
{
if (e.Control && e.KeyCode == Keys.V)//使用CTRL+V
SendBox.Paste();
if (e.Control && e.KeyCode == Keys.C)//使用CTRL+C
SendBox.Copy();
if (e.Control && e.KeyCode == Keys.X)//使用CTRL+X
SendBox.Cut();
}
发送数据选中二进制时,要判断输入范围
if (checkBoxSHex.Checked)
{
//允许输入数字0~9,A~F范围内,则返回false
e.Handled = "0123456789ABCDEF ".IndexOf(char.ToUpper(e.KeyChar)) < 0;
if (e.KeyChar == (char)8) //允许输入回退键
{
e.Handled = false;
}
}
输入ASC码字符后,勾选hex,将ASC字符转换为16进制
/// 字符串转16进制字节数组
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
使用Fn快捷键
private void Button_Fn_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1 && comm.IsOpen)
{
buttonVersion_Click(null, null);
buttonAutoSend_Click();
}
if (e.KeyCode == Keys.F2 && comm.IsOpen)
{
buttonComconfig_Click(null, null);
buttonAutoSend_Click();
}
if (e.KeyCode == Keys.F3 && comm.IsOpen)
{
buttonLoglist_Click(null, null);
buttonAutoSend_Click();
}
if (e.KeyCode == Keys.F4 && comm.IsOpen)
{
buttonAuthcodes_Click(null, null);
buttonAutoSend_Click();
}
}
异或校验功能
private static string XOR(string cmdString)
{
try
{
int XORCode = 0;
//将字符串拆分成为16进制字节数据然后两位两位进行异或校验
for (int i = 1; i < cmdString.Length / 2; i++)
{
string cmdHex = cmdString.Substring(i * 2, 2);
if (i == 1)
{
string cmdPrvHex = cmdString.Substring((i - 1) * 2, 2);
XORCode = (byte)Convert.ToInt32(cmdPrvHex, 16) ^ (byte)Convert.ToInt32(cmdHex, 16);
}
else
{
XORCode = (byte)XORCode ^ (byte)Convert.ToInt32(cmdHex, 16);
}
}
return Convert.ToString(XORCode, 16).ToUpper();//返回16进制校验码
}
catch
{
throw;
}
}
通过硬件的方式设置当前串口发送串口参数恢复默认值的功能
textBox2.AppendText("\r\n正在恢复当前串口的默认配置(9600 N 8 1 N OFF ON NOVATEL NOVATEL ON),请等待5秒...");
for (int i = 0; i < 480; i++)
breakSignal[i] = 0x46;
try
{
comm.Write(breakSignal, 0, breakSignal.Length);
Thread.Sleep(1500);
comm.Write(breakSignal, 0, breakSignal.Length);
}