程序的运行界面:
全部资料51hei下载地址:
5、COM_LoRa模块设置.zip
(3.62 MB, 下载次数: 98)
部分源码预览:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Threading;
- using System.Collections;
- using System.IO;
- namespace COM_A
- {
- public partial class Form1 : Form
- {
- public byte[] myBy;//传递串口接收到的数据
- public int myByLenth = 0;//串口接收到的数据长度
- public string myStr;//串口接收的字符串
- public bool receiveDtatFlag = false;
- public int send_count = 0;
- public Form1()
- {
- InitializeComponent();
- //this.ControlBox = false;//最小化最大化关闭按钮处理
- }
- private void Form1_Load(object sender, EventArgs e)//页面加载
- {
- #region 串口设置初始化加载
- try
- {
- foreach (string com in System.IO.Ports.SerialPort.GetPortNames())//串口检测
- comboBox1.Items.Add(com);
- //comboBox1.Text = (string)comboBox1.Items[0];//初始化选择串口
- comboBox1.Text = myConfing.ConfingGetValue("COM");//使用配置文件加载
- if (comboBox1.Text == "")
- {
- comboBox1.Text = (string)comboBox1.Items[0];//初始化选择串口
- }
- }
- catch (Exception er)
- {
- MessageBox.Show("端口加载失败!" + er.Message, "提示");
- }
- //列出常用的波特率
- string[] ss = new string[] { "1200", "2400", "4800", "9600", "19200", "38400", "43000", "56000", "57600", "115200" };
- comboBox2.DataSource = ss;
- /*for (int temp = 0; temp < 10; temp++)
- {
- comboBox2.Items.Add(ss[temp]);
- }*/
- comboBox2.Text = myConfing.ConfingGetValue("buadRate");
- if (comboBox2.Text == "")
- {
- comboBox2.SelectedIndex = 3;
- }
- //comboBox2.SelectedIndex = 3;
- //列出奇偶校验位
- comboBox3.DataSource = Enum.GetNames(typeof(Parity));
- //comboBox3.Items.Add("无校验");
- //comboBox3.Items.Add("奇校验");
- //comboBox3.Items.Add("偶校验");
- //comboBox3.SelectedIndex = 0;
- comboBox3.Text = myConfing.ConfingGetValue("Parity");
- if (comboBox3.Text == "")
- {
- comboBox3.Text = "None";
- }
- //列出数据位
- ss = new string[] { "5", "6", "7", "8", "9", "10" };
- comboBox4.DataSource = ss;
- //comboBox4.Text = "8";
- comboBox4.Text = myConfing.ConfingGetValue("dataBits");
- if (comboBox4.Text == "")
- {
- comboBox4.SelectedIndex = 3;
- }
- //列出停止位
- comboBox5.DataSource = Enum.GetNames(typeof(StopBits));
- //comboBox5.Text = Enum.Format(typeof(StopBits), StopBits.One, "G");
- comboBox5.Text = myConfing.ConfingGetValue("stopBits");
- if (comboBox5.Text == "")
- {
- comboBox5.SelectedIndex = 1;
- }
- button1.Text = "打开串口";
- //开始执行后台操作
- //backgroundWorker1.RunWorkerAsync();
- checkBox1.Checked = true;
- checkBox2.Checked = true;
- checkBox3.Checked = true;
- checkBox4.Checked = true;
- #endregion
- //this.button3.Visible = false;
- #region 加载开选择的模块
- cmbSetParaLoRaDataRate.Items.Clear();
- cmbSetParaWakeUpTime.Items.Clear();
- if (cmbSelectLoRaMode.Text == "杭州塔石")//
- {
- //LoRa的空中速率
- cmbSetParaLoRaDataRate.Items.Add("000_0.3K");
- cmbSetParaLoRaDataRate.Items.Add("001_0.6K");
- cmbSetParaLoRaDataRate.Items.Add("010_1.2K");
- cmbSetParaLoRaDataRate.Items.Add("011_2.5K");
- cmbSetParaLoRaDataRate.Items.Add("100_4.5K");
- cmbSetParaLoRaDataRate.Items.Add("101_9.1K");
- cmbSetParaLoRaDataRate.Items.Add("110_18.2K");
- cmbSetParaLoRaDataRate.Items.Add("111_21.8K");//(默认)
- //cmbSetParaLoRaDataRate.Text = "111_21.8K";//(默认)
- cmbSetParaLoRaDataRate.Text = cmbSetParaLoRaDataRate.Items[7].ToString();
- //唤醒时间
- cmbSetParaWakeUpTime.Items.Add("000_500");//(默认)
- cmbSetParaWakeUpTime.Items.Add("001_1000");
- cmbSetParaWakeUpTime.Items.Add("010_1500");
- cmbSetParaWakeUpTime.Items.Add("011_2000");
- cmbSetParaWakeUpTime.Items.Add("100_2500");
- cmbSetParaWakeUpTime.Items.Add("101_3000");
- cmbSetParaWakeUpTime.Items.Add("110_3500");
- cmbSetParaWakeUpTime.Items.Add("111_4000");
- //cmbSetParaWakeUpTime.Text = "000_500";//(默认)
- cmbSetParaWakeUpTime.Text = cmbSetParaWakeUpTime.Items[0].ToString();
- }
- else
- {
- //LoRa的空中速率
- cmbSetParaLoRaDataRate.Items.Add("000_0.3K");
- cmbSetParaLoRaDataRate.Items.Add("001_0.6K");
- cmbSetParaLoRaDataRate.Items.Add("010_1.2K");
- cmbSetParaLoRaDataRate.Items.Add("011_2.4K");//(默认)
- cmbSetParaLoRaDataRate.Items.Add("100_4.8K");
- cmbSetParaLoRaDataRate.Items.Add("101_9.6K");
- cmbSetParaLoRaDataRate.Items.Add("110_19.2K");
- cmbSetParaLoRaDataRate.Items.Add("111_19.2K");
- //cmbSetParaLoRaDataRate.Text = "011_2.4K";//(默认)
- cmbSetParaLoRaDataRate.Text = cmbSetParaLoRaDataRate.Items[3].ToString();
- //唤醒时间
- cmbSetParaWakeUpTime.Items.Add("000_250");//(默认)
- cmbSetParaWakeUpTime.Items.Add("001_500");
- cmbSetParaWakeUpTime.Items.Add("010_750");
- cmbSetParaWakeUpTime.Items.Add("011_1000");
- cmbSetParaWakeUpTime.Items.Add("100_1250");
- cmbSetParaWakeUpTime.Items.Add("101_1500");
- cmbSetParaWakeUpTime.Items.Add("110_1750");
- cmbSetParaWakeUpTime.Items.Add("111_2000");
- //cmbSetParaWakeUpTime.Text = "000_250";//(默认)
- cmbSetParaWakeUpTime.Text = cmbSetParaWakeUpTime.Items[0].ToString();
- }
- #endregion
- }
- private void comboBox1_DropDown_1(object sender, EventArgs e)//串口下拉框更新COM口
- {
- try
- {
- comboBox1.Items.Clear();
- foreach (string com in System.IO.Ports.SerialPort.GetPortNames())//串口检测
- comboBox1.Items.Add(com);
- }
- catch (Exception er)
- {
- MessageBox.Show("端口加载失败!" + er.Message, "提示");
- }
- }
- #region 打开串口按钮处理
- public void RefrespictureBox1()//刷新打开串口按键显示以及颜色
- {
- if (serialPort1.IsOpen)
- {
- button1.BackColor = SystemColors.GradientActiveCaption;
- button1.Text = "关闭串口";
- }
- else
- {
- button1.BackColor = SystemColors.Control;
- button1.Text = "打开串口";
- }
- myConfing.SetValue("COM", comboBox1.Text);
- myConfing.SetValue("buadRate", comboBox2.Text);
- myConfing.SetValue("Parity", comboBox3.Text);
- myConfing.SetValue("dataBits", comboBox4.Text);
- myConfing.SetValue("stopBits", comboBox5.Text);
- }
- private void button1_Click(object sender, EventArgs e)//打开串口
- {
- try
- {
- if (serialPort1.IsOpen)//如果串口是开的则关闭
- {
- serialPort1.Close();
- comboBox1.Enabled = true;
- comboBox2.Enabled = true;
- comboBox3.Enabled = true;
- comboBox4.Enabled = true;
- comboBox5.Enabled = true;
- #region
- //button1.Text = "打开串口";
- /*//关闭串口后,串口设置选项便可以继续使用
- comboBox1.Enabled = true;
- comboBox2.Enabled = true;
- comboBox3.Enabled = true;
- comboBox4.Enabled = true;
- comboBox5.Enabled = true;*/
- #endregion
- }
- else//否则打开串口
- {
- //button1.Text = "关闭串口";
- serialPort1.PortName = comboBox1.Text;
- serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
- serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox3.Text);
- serialPort1.DataBits = Int32.Parse(comboBox4.Text);
- serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox5.Text);
- serialPort1.Encoding = Encoding.GetEncoding("Gb2312");//返回与指定代码页名称关联的编码Gb2312
- serialPort1.Open();
- comboBox1.Enabled = false;
- comboBox2.Enabled = false;
- comboBox3.Enabled = false;
- comboBox4.Enabled = false;
- comboBox5.Enabled = false;
- }
- RefrespictureBox1();//刷新打开串口按键显示以及颜色
- }
- catch (Exception)
- {
- //打开串口失败后,相应标志位取消
- MessageBox.Show("串口无效或已被占用!", "错误提示");
- }
- }
- #endregion
- #region 发送区形式处理、串口按键发送、定时自动发送以及其他外部调用发送相关接口
- private bool CheckSendData()//检查串口发送区域
- {
- if (textBox2.Text.Trim() == "") return false;
- return true;
- }
- private void checkBox4_CheckedChanged(object sender, EventArgs e)//发送区HEX处理
- {
- string s = "";
- if (checkBox4.CheckState == CheckState.Checked)//发送HEX显示
- {
- byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(textBox2.Text);
- textBox2.Text = "";
- foreach (byte btmp in by)
- {
- s = "00" + string.Format("{0:X}", btmp);
- textBox2.Text += s.Substring(s.Length - 2, 2);
- textBox2.Text += " ";
- }
- }
- else//发送ASCII显示
- {
- ArrayList al = myClass.Str16ToArrayList(textBox2.Text);
- byte[] by = new byte[al.Count];
- int i = 0;
- foreach (string stmp in al)
- {
- by[i] += Convert.ToByte(stmp, 16);
- i++;
- }
- textBox2.Text = Encoding.GetEncoding("Gb2312").GetString(by);
- }
- }
- private void textBox2_KeyPress(object sender, KeyPressEventArgs e)//发送区有按键键入,
- {
- if (checkBox1.CheckState == CheckState.Checked)//HEX发送时
- {
- if (!Uri.IsHexDigit(e.KeyChar) && e.KeyChar != ' ')//不符合HEX的键入清除
- {
- e.KeyChar = (char)0;
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)//按键发送
- {
- try
- {
- if (serialPort1.IsOpen)
- {
- /*if (!CheckSendData())//检测要发送的数据
- {
- MessageBox.Show("请输入要发送的数据!", "错误提示");
- return;
- }*/
- string s = "";
- //MessageBox.Show(checkBox4.Checked.ToString());
- if (checkBox4.Checked)//十六进制发送
- {
- ArrayList al = myClass.Str16ToArrayList(textBox2.Text);
- byte[] by = new byte[al.Count];
- int i = 0;
- foreach (string stmp in al)
- {
- by[i] += Convert.ToByte(stmp, 16);//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
- i++;
- }
- serialPort1.Write(by, 0, i);//发送字节
- //s = Encoding.GetEncoding("Gb2312").GetString(by);//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
- }
- else//ASCII发送
- {
- s = textBox2.Text;
- serialPort1.Write(s);//串口发送字符串
- }
- //MessageBox.Show(s);
- }
- else
- {
- MessageBox.Show("串口未打开!", "错误提示");
- }
- }
- catch (Exception)
- {
- MessageBox.Show("发送数据时发生错误!", "错误提示");
- }
- }
- public void sendData(string dataIn)//调用的发送函数
- {
- try
- {
- if (serialPort1.IsOpen)
- {
- /*if (!CheckSendData())//检测要发送的数据
- {
- MessageBox.Show("请输入要发送的数据!", "错误提示");
- return;
- }*/
- string s = "";
- textBox2.Text = dataIn;
- //MessageBox.Show(checkBox4.Checked.ToString());
- if (checkBox4.Checked)//十六进制发送
- {
- ArrayList al = myClass.Str16ToArrayList(textBox2.Text);
- byte[] by = new byte[al.Count];
- int i = 0;
- foreach (string stmp in al)
- {
- by[i] += Convert.ToByte(stmp, 16);//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
- i++;
- }
- serialPort1.Write(by, 0, i);//发送字节
- //s = Encoding.GetEncoding("Gb2312").GetString(by);//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
- }
- else//ASCII发送
- {
- s = textBox2.Text;
- serialPort1.Write(s);//串口发送字符串
- }
- //MessageBox.Show(s);
- }
- else
- {
- MessageBox.Show("串口未打开!", "错误提示");
- }
- }
- catch (Exception)
- {
- MessageBox.Show("发送数据时发生错误!", "错误提示");
- }
- }
- public void sendData_NoDispaly(string dataIn)//调用的发送函数并且不显示在发送区域
- {
- try
- {
- if (serialPort1.IsOpen)
- {
- /*if (!CheckSendData())//检测要发送的数据
- {
- MessageBox.Show("请输入要发送的数据!", "错误提示");
- return;
- }*/
- string s = "";
- //textBox2.Text = dataIn;//不显示在发送区域
- //MessageBox.Show(checkBox4.Checked.ToString());
- if (checkBox4.Checked)//十六进制发送
- {
- ArrayList al = myClass.Str16ToArrayList(textBox2.Text);
- byte[] by = new byte[al.Count];
- int i = 0;
- foreach (string stmp in al)
- {
- by[i] += Convert.ToByte(stmp, 16);//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
- i++;
- }
- serialPort1.Write(by, 0, i);//发送字节
- //s = Encoding.GetEncoding("Gb2312").GetString(by);//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
- }
- else//ASCII发送
- {
- s = textBox2.Text;
- serialPort1.Write(s);//串口发送字符串
- }
- //MessageBox.Show(s);
- }
- else
- {
- MessageBox.Show("串口未打开!", "错误提示");
- }
- }
- catch (Exception)
- {
- MessageBox.Show("发送数据时发生错误!", "错误提示");
- }
- }
- private void timer1_Tick(object sender, EventArgs e)//定时自动发送
- {
- timer1.Interval = Convert.ToInt32(textBox1.Text);
- try
- {
- if ((serialPort1.IsOpen) && (checkBox5.Checked == true))
- {
- /*if (!CheckSendData())//检测要发送的数据
- {
- MessageBox.Show("请输入要发送的数据!", "错误提示");
- return;
- }*/
- string s = "";
- //textBox2.Text = dataIn;
- //MessageBox.Show(checkBox4.Checked.ToString());
- if (checkBox4.Checked)//十六进制发送
- {
- ArrayList al = myClass.Str16ToArrayList(textBox2.Text);
- byte[] by = new byte[al.Count];
- int i = 0;
- foreach (string stmp in al)
- {
- by[i] += Convert.ToByte(stmp, 16);//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
- i++;
- }
- serialPort1.Write(by, 0, i);//发送字节
- //s = Encoding.GetEncoding("Gb2312").GetString(by);//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
- }
- else//ASCII发送
- {
- s = textBox2.Text;
- serialPort1.Write(s);//串口发送字符串
- }
- send_count++;
- textBox5.Text = send_count.ToString();
- }
- else
- {
- //MessageBox.Show("串口未打开!", "错误提示");
- }
- }
- catch (Exception)
- {
- //MessageBox.Show("发送数据时发生错误!", "错误提示");
- }
- }
- #endregion
- #region 接收方法一用到的线程处理,不用
- delegate void SetTextCallback(string text);//委托,使用回调函数时考虑使用
- private void SetText(string text)
- {
- try
- {
- if (this.richTextBox1.InvokeRequired)//获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。
- {
- SetTextCallback d = new SetTextCallback(SetText);//回调函数
- this.Invoke(d, new object[] { text });//执行delegate里的搭载函数
- }
- else
- {
- this.richTextBox1.Text += text;
- }
- //this.richTextBox2.Focus();//获取焦点
- //this.richTextBox2.Select(this.richTextBox2.TextLength, 0);//光标定位到文本最后
- /* //设定光标所在位置
- this.richTextBox1.SelectionStart = richTextBox1.TextLength;
- //滚动到当前光标处
- this.richTextBox1.ScrollToCaret();*/
- }
- catch (Exception)
- {
- }
- }
- #endregion
- #region 串口接收处理
- //int receive_count_test = 0;
- private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//清除接收
- {
- richTextBox1.Text = "";
- send_count = 0;
- //receive_count_test = 0;
- textBox5.Text = "";
- //textBox5.Text = "";
- }
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)//接收事件触发
- {
- #region
- /* char[] c;
- //StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
- try
- {
- if (serialPort1.IsOpen)
- {
- c = new char[serialPort1.BytesToRead];
- serialPort1.Read(c, 0, c.Length);//串口读取
- if (c.Length > 0)
- {
- if (checkBox1.Checked)//接收HEX显示
- {
- if (checkBox3.Checked)//接收时间显示
- {
- SetText(DateTime.Now.ToString());
- SetText(" ");
- }
- byte[] by = Encoding.GetEncoding("Gb2312").GetBytes(c);
- myBy = by;
- myByLenth = by.Length;
- /*if (by[0] == 0x7b)
- {
- MessageBox.Show("正确");
- }
- else
- {
- MessageBox.Show(by[0].ToString());
- }*
- foreach (byte btmp in by)
- {
- string s = "00" + string.Format("{0:X}", btmp);//将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式
- s = s.Substring(s.Length - 2, 2);
- SetText(new string(s.ToCharArray()));
- SetText(" ");
- }
- if (checkBox2.Checked)//接收换行显示
- {
- SetText("\n");
- }
- }
- else
- {
- if (checkBox3.Checked)//接收时间显示
- {
- SetText(DateTime.Now.ToString());
- }
- SetText(new string(c));
- if (checkBox3.Checked)//接收换行显示
- {
- SetText("\n");
- }
- }
- //this.richTextBox2.Text += "\n";
- }
- }
- }
- catch (Exception)
- {
- }
- if (checkBox2.Checked)//接收HEX显示
- {
- Thread.Sleep(500);//延时500ms 等待接收完数据
- }
- else
- {
- Thread.Sleep(10);//延时100ms 等待接收完数据
- }*/
- /* //设定光标所在位置
- this.richTextBox1.SelectionStart = richTextBox1.TextLength;
- //滚动到当前光标处
- this.richTextBox1.ScrollToCaret();*/
- //追加的形式添加到文本框末端,并滚动到最后。
- //this.richTextBox1.AppendText(builder.ToString());
- #endregion
- #region
- Thread.Sleep(Convert.ToInt32(textBox5.Text.Trim()));//延时100ms 等待接收完数据
- //第二种接收方式
- StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
- long received_count = 0;//接收计数
- int n = serialPort1.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
- byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
- received_count += n;//增加接收计数
- serialPort1.Read(buf, 0, n);//读取缓冲数据
- builder.Clear();//清除字符串构造器的内容
- myBy = buf;
- myByLenth = buf.Length;
- //因为要访问ui资源,所以需要使用invoke方式同步ui。
- this.Invoke((EventHandler)(delegate
- {
- if (checkBox3.Checked)//接收时间显示
- {
- this.richTextBox1.AppendText(DateTime.Now.ToString(@"yyyy\:MM\:dd") + " ");
- this.richTextBox1.AppendText(DateTime.Now.ToString(@"HH\:mm\:ss\:fff") + " ");
- }
- //判断是否是显示为16禁止
- if (checkBox1.Checked)
- {
- //依次的拼接出16进制字符串
- foreach (byte b in buf)
- {
- builder.Append(b.ToString("X2") + " ");//在此实例的结尾追加指定字符串的副本
- }
- }
- else
- {
- //直接按ASCII规则转换成字符串
- builder.Append(Encoding.ASCII.GetString(buf));
- }
- //追加的形式添加到文本框末端,并滚动到最后。
- this.richTextBox1.AppendText(builder.ToString());
- myStr = builder.ToString();//字符串输出
- if (checkBox2.Checked)//接收换行显示
- {
- this.richTextBox1.AppendText("\n");
- }
- //修改接收计数
- //labelGetCount.Text = "Get:" + received_count.ToString();
- try
- {
- if (receiveDtatFlag == true)//保存数据
- {
- string fName = saveFileDialog1.FileName;
- FileStream fs = File.Open(fName, FileMode.Append);
- StreamWriter sw = new StreamWriter(fs);
- if (checkBox3.Checked)//接收时间显示
- {
- //this.richTextBox1.AppendText(DateTime.Now.ToString() + " ");
- sw.Write(DateTime.Now.ToString(@"hh\:mm\:ss\:fff") + " ");
- }
- sw.Write(builder.ToString());
- if (checkBox2.Checked)//接收换行显示
- {
- sw.WriteLine("\n");
- }
- //sw.WriteLine(richTextBox1.Text);
- //textBox3.Text = fName;
- sw.Close();
- fs.Close();
- }
- }
- catch
- {
- }
- myReceDataAnalysis(myStr);//数据解析处理
- //analysis myans = new analysis();
- //myans.analysisData(myStr);
- }));//*/
- #endregion
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)//接收窗处理
- {
- if (richTextBox1.Lines.Length > 8000)//大于8000行时清楚前面的
- {
- int n = 500;
- int start = richTextBox1.GetFirstCharIndexFromLine(0);//第一行第一个字符索引
- int end = richTextBox1.GetFirstCharIndexFromLine(n);//第n行第一个字符索引
- richTextBox1.Select(start, end);//选中n行
- richTextBox1.SelectedText = "";//设置前n行为空
- }
- richTextBox1.Focus();
- richTextBox1.Select(richTextBox1.TextLength, 0);
- }
- #endregion
- #region 窗体处理
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)//关闭提醒
- {
- Properties.Settings.Default.Save();
- myConfing.SetValue("COM", comboBox1.Text);
- myConfing.SetValue("buadRate", comboBox2.Text);
- myConfing.SetValue("Parity", comboBox3.Text);
- myConfing.SetValue("dataBits", comboBox4.Text);
- myConfing.SetValue("stopBits", comboBox5.Text);
- if (DialogResult.Yes == MessageBox.Show("程序正在使用中,确认退出?", "确认退出", MessageBoxButtons.YesNo))
- {
- //MessageBox.Show("立即退出!");
- serialPort1.Close();
- this.Dispose();
- e.Cancel = false;
- }
- else
- {
- e.Cancel = true;
- }
- }
- /*private void Form1_FormClosed(object sender, FormClosedEventArgs e)//关闭整个窗口
- {
- Properties.Settings.Default.Save();
- myConfing.SetValue("COM", comboBox1.Text);
- myConfing.SetValue("buadRate", comboBox2.Text);
- myConfing.SetValue("Parity", comboBox3.Text);
- myConfing.SetValue("dataBits", comboBox4.Text);
- myConfing.SetValue("stopBits", comboBox5.Text);
- }*/
- /*private void button3_Click(object sender, EventArgs e)//窗口隐藏
- {
- //this.Hide();//隐藏窗体
- }*/
- #endregion
- #region 数据文件保存处理
- private void button4_Click(object sender, EventArgs e)//保存路径选择
- {
- //saveFileDialog1.InitialDirectory = @"G:\自制软件\C#练习\20.19 文件保存SaveFileDialog控件\51zxw";
- saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
- saveFileDialog1.FilterIndex = 1;
- saveFileDialog1.FileName = "ReceiveData";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fName = saveFileDialog1.FileName;
- FileStream fs = File.Open(fName, FileMode.Append);
- StreamWriter sw = new StreamWriter(fs);
- //sw.WriteLine(richTextBox1.Text);
- textBox3.Text = fName;
- sw.Close();
- fs.Close();
- }
- }
- private void checkBox6_CheckedChanged(object sender, EventArgs e)//保存数据勾选处理
- {
- if (checkBox6.CheckState == CheckState.Checked)//选中
- {
- receiveDtatFlag = true;
- }
- else
- {
- receiveDtatFlag = false;
- }
- }
- #endregion
- #region 数据处理过程
- private void btnSetPara_Click(object sender, EventArgs e)//设置参数
- {
- string mySendDataTemp = "";
- //帧头
- string mySendHead_0 = "";
- if (chbSetParaSave.Checked == true)//掉电保存
- {
- mySendHead_0 = "C0" + " ";
- }
- else
- {
- mySendHead_0 = "C2" + " ";
- }
- //地址
- if (txbSetParaAddr.Text.Trim().Length != 4)
- {
- MessageBox.Show("地址长度不为4!", "错误提示");
- return;
- }
- string mySendAddr_1_2 = txbSetParaAddr.Text.Trim() + " ";
- //字节3
- string mySendChar_3 = "";
- byte mySendByte_3 = 0;
- //串口校验位,mySendByte_3的7、6位
- switch (cmbSetParaDataBit.Text.Trim().Substring(0, 2))
- {
- case "00"://8N1(默认)
- mySendByte_3 &= 0x3F;
- break;
- case "01"://8O1
- mySendByte_3 |= (1<<6);
- break;
- case "10"://8E1
- mySendByte_3 |= (2 << 6);
- break;
- case "11"://8N1(默认)
- mySendByte_3 &= 0x3F;
- break;
- default:
- mySendByte_3 &= 0x3F;//8N1(默认)
- break;
- }
- //TTL 串口速率(bps),mySendByte_3的5、4、3位
- switch (cmbSetParaBuadRate.Text.Trim().Substring(0, 3))
- {
- case "000"://1200
- mySendByte_3 &= 0xC7;
- break;
- case "001"://2400
- mySendByte_3 |= (1 << 3);
- break;
- case "010"://4800
- mySendByte_3 |= (2 << 3);
- break;
- case "011"://9600(默认)
- mySendByte_3 |= (3 << 3);
- break;
- case "100"://19200
- mySendByte_3 |= (4 << 3);
- break;
- case "101"://38400
- mySendByte_3 |= (5 << 3);
- break;
- case "110"://57600
- mySendByte_3 |= (6 << 3);
- break;
- case "111"://115200
- mySendByte_3 |= (7 << 3);
- break;
- default:
- mySendByte_3 |= (3 << 3);//9600(默认)
- break;
- }
- //无线空中速率(bps)
- switch (cmbSetParaLoRaDataRate.Text.Trim().Substring(0, 3))
- {
- case "000"://0.3K
- mySendByte_3 &= 0xF8;
- break;
- case "001"://0.6K
- mySendByte_3 |= 1 ;
- break;
- case "010"://1.2K
- mySendByte_3 |= 2 ;
- break;
- case "011"://2.5K/2.4k(默认)
- mySendByte_3 |= 3 ;
- break;
- case "100"://4.5K/4.8k
- mySendByte_3 |= 4 ;
- break;
- case "101"://9.1K/9.6k
- mySendByte_3 |= 5 ;
- break;
- case "110"://18.2K/19.2k
- mySendByte_3 |= 6 ;
- break;
- case "111"://21.8K(默认)/19.2k
- mySendByte_3 |= 7 ;
- break;
- default:
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- mySendByte_3 |= 7 ;//21.8K(默认)
- }
- else
- {
- mySendByte_3 |= 3;//2.4K(默认)
- }
- break;
- }
- mySendChar_3 = mySendByte_3.ToString("X2") + " ";
- //信道
- string mySendChannel_4 = "";//txbSetParaLoRaChannel.Text.Trim().Substring(0, 2) + " ";
- Int16 mySendChannelTempInt = Convert.ToInt16(txbSetParaLoRaChannel.Text.Trim());
- //byte mySendChannelTempByte = Convert.ToByte((mySendChannelTempInt - 398).ToString("X2"));
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- mySendChannel_4 = (mySendChannelTempInt - 398).ToString("X2");
- }
- else
- {
- mySendChannel_4 = (mySendChannelTempInt - 410).ToString("X2");
- }
- //字节5
- string mySendChar_5 = "";
- byte mySendByte_5 = 0;
- //发送模式,mySendByte_5的1位
- if (cmbSetParaSendMode.Text.Trim().Substring(0, 1) == "1")
- {
- mySendByte_5 |= (1 << 7);
- }
- else
- {
- mySendByte_5 &= 0x7F;//默认
- }
- //IO 驱动方式(默认1),mySendByte_5的6位
- if (cmbSetParaIOMode.Text.Trim().Substring(0, 1) == "1")
- {
- mySendByte_5 |= (1 << 6);
- }
- else
- {
- mySendByte_5 &= 0xBF;//默认
- }
- //无线唤醒时间,mySendByte_5的5、4、3位
- switch (cmbSetParaWakeUpTime.Text.Trim().Substring(0, 3))
- {
- case "000"://500ms(默认)/250ms(默认)
- mySendByte_5 &= 0xC7;
- break;
- case "001"://1000ms/500ms
- mySendByte_5 |= (1 << 3);
- break;
- case "010"://1500ms/750ms
- mySendByte_5 |= (2 << 3);
- break;
- case "011"://2000ms/1000ms
- mySendByte_5 |= (3 << 3);
- break;
- case "100"://2500ms/1250ms
- mySendByte_5 |= (4 << 3);
- break;
- case "101"://3000ms/1500ms
- mySendByte_5 |= (5 << 3);
- break;
- case "110"://3500ms/1750ms
- mySendByte_5 |= (6 << 3);
- break;
- case "111"://4000ms/2000ms
- mySendByte_5 |= (7 << 3);
- break;
- default:
- mySendByte_5 &= 0xC7;//500ms(默认)/250ms(默认)
- break;
- }
- //FEC 开关,mySendByte_5的2位
- if (chbSetParaOpenFec.Checked == true)//定点传输
- {
- mySendByte_5 |= (1 << 2);
- }
- else//透明传输
- {
- mySendByte_5 &= 0xFB;
- }
- //发射功率,mySendByte_5的1、0位
- switch (cmbSetParaLoRaDataRate.Text.Trim().Substring(0, 3))
- {
- case "00":////20dBm(默认)
- mySendByte_5 &= 0xFC;
- break;
- case "01"://17dBm
- mySendByte_5 |= 1;
- break;
- case "10"://14dBm
- mySendByte_5 |= 2;
- break;
- case "11"://10dBm
- mySendByte_5 |= 3;
- break;
- default:
- mySendByte_5 &= 0xFC;//20dBm(默认)
- break;
- }
- mySendChar_5 = mySendByte_5.ToString("X2");
- //综合
- mySendDataTemp = mySendHead_0 + mySendAddr_1_2 + mySendChar_3 + mySendChannel_4 + mySendChar_5;
- sendData(myClass.strAddSpace(mySendDataTemp));
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\green.png");//bin文件中
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- }
- private void btnReadPara_Click(object sender, EventArgs e)//读取参数
- {
- sendData("C1 C1 C1");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\green.png");//bin文件中
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- }
- private void btnReadVersion_Click(object sender, EventArgs e)//读取版本号
- {
- sendData("C3 C3 C3");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\green.png");//bin文件中
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- }
- private void btnReset_Click(object sender, EventArgs e)//复位
- {
- sendData("C4 C4 C4");
- this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox3.Image = Image.FromFile(Application.StartupPath + "\\red.png");//bin文件中
- this.pictureBox4.Image = Image.FromFile(Application.StartupPath + "\\green.png");//bin文件中
- }
- //数据解析过程
- public void myReceDataAnalysis(string str)
- {
- byte[] myReceByte = myClass.strAsciiToHexBy(str);
- if (myReceByte.Length >= 4)
- {
- try
- {
- switch (myReceByte[0])
- {
- case 0xC0:
- case 0xC2://读取工作参数
- #region //工作参数显示
- //字节0掉电是否保存
- if (myReceByte[0] == 0xC0)//要掉电保存的参数
- {
- chbSetParaSave.Checked = true;
- }
- else
- {
- chbSetParaSave.Checked = false;
- }
- //字节1、2 地址
- txbSetParaAddr.Text = myReceByte[1].ToString("X2") + myReceByte[2].ToString("X2");
- //字节3
- //串口校验位,字节3的7、6位
- cmbSetParaDataBit.Text = cmbSetParaDataBit.Items[(myReceByte[3] >> 6) & 0x03].ToString();
- #region
- /*switch ((myReceByte[3] >> 6) & 0x03)
- {
- case 0://8N1(默认)
- cmbSetParaDataBit.Text = "00_8N1";
- break;
- case 1://8O1
- cmbSetParaDataBit.Text = "01_8O1";
- break;
- case 2://8E1
- cmbSetParaDataBit.Text = "01_8E1";
- break;
- case 3://8N1(默认)
- cmbSetParaDataBit.Text = "00_8N1";
- break;
- default:
- cmbSetParaDataBit.Text = "error";
- break;
- }*/
- #endregion
- //TTL 串口速率(bps),字节3的5、4、3位
- cmbSetParaBuadRate.Text = cmbSetParaBuadRate.Items[(myReceByte[3] >> 3) & 0x07].ToString();
- #region
- /*switch ((myReceByte[3] >> 3) & 0x07)
- {
- case 0://1200
- cmbSetParaBuadRate.Text = "000_1200";
- break;
- case 1://2400
- cmbSetParaBuadRate.Text = "001_2400";
- break;
- case 2://4800
- cmbSetParaBuadRate.Text = "010_4800";
- break;
- case 3://9600(默认)
- cmbSetParaBuadRate.Text = "011_9600";
- break;
- case 4://19200
- cmbSetParaBuadRate.Text = "100_19200";
- break;
- case 5://38400
- cmbSetParaBuadRate.Text = "101_38400";
- break;
- case 6://57600
- cmbSetParaBuadRate.Text = "110_57600";
- break;
- case 7://115200
- cmbSetParaBuadRate.Text = "111_115200";
- break;
- default:
- cmbSetParaBuadRate.Text = "error";
- break;
- }*/
- #endregion
- //无线空中速率(bps),字节3的2、1、0位
- cmbSetParaLoRaDataRate.Text = cmbSetParaLoRaDataRate.Items[(myReceByte[3] >> 0) & 0x07].ToString();
- #region
- /*switch ((myReceByte[3] >> 0) & 0x07)
- {
- case 0://0.3K
- cmbSetParaLoRaDataRate.Text = "000_0.3K";
- break;
- case 1://0.6K
- cmbSetParaLoRaDataRate.Text = "001_0.6K";
- break;
- case 2://1.2K
- cmbSetParaLoRaDataRate.Text = "010_1.2K";
- break;
- case 3://2.5K/2.4k
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- cmbSetParaLoRaDataRate.Text = "011_2.5K";
- }
- else
- {
- cmbSetParaLoRaDataRate.Text = "011_2.4K";
- }
- break;
- case 4://4.5K/4.8k
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- cmbSetParaLoRaDataRate.Text = "100_4.5K";
- }
- else
- {
- cmbSetParaLoRaDataRate.Text = "100_4.8K";
- }
- break;
- case 5://9.1K/9.6K
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- cmbSetParaLoRaDataRate.Text = "101_9.1K";
- }
- else
- {
- cmbSetParaLoRaDataRate.Text = "101_9.6K";
- }
- break;
- case 6://18.2K/19.2K
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- cmbSetParaLoRaDataRate.Text = "110_18.2K";
- }
- else
- {
- cmbSetParaLoRaDataRate.Text = "110_19.2K";
- }
- break;
- case 7://21.8K(默认)
- if (cmbSelectLoRaMode.Text == "杭州塔石")
- {
- cmbSetParaLoRaDataRate.Text = "111_21.8K";
- }
- else
- {
- cmbSetParaLoRaDataRate.Text = "111_19.2K";
- }
- break;
- default:
- cmbSetParaLoRaDataRate.Text = "error";
- break;
- }*/
- #endregion
- //字节4信道号
- txbSetParaLoRaChannel.Text = (myReceByte[4] + 398).ToString();
- //字节5
- //发送模式,字节5的第7位
- cmbSetParaSendMode.Text = cmbSetParaSendMode.Items[(myReceByte[5] >> 7) & 0x01].ToString();
- #region
- /*if (((myReceByte[5] >> 7) & 0x01) == 1)//定点
- {
- cmbSetParaSendMode.Text = "1_定点传输";
- }
- else//透明
- {
- cmbSetParaSendMode.Text = "0_透明传输";
- }*/
- #endregion
- //IO 驱动方式(默认1),字节5的第6位
- cmbSetParaIOMode.Text = cmbSetParaIOMode.Items[(myReceByte[5] >> 6) & 0x01].ToString();
- #region
- /*if (((myReceByte[5] >> 6) & 0x01) == 1)//
- {
- cmbSetParaIOMode.Text = "1_推挽上拉";
- }
- else//
- {
- cmbSetParaIOMode.Text = "0_开漏浮空";
- }*/
- #endregion
- //无线唤醒时间,字节5的第5、4、3位
- cmbSetParaWakeUpTime.Text = cmbSetParaWakeUpTime.Items[(myReceByte[5] >> 3) & 0x07].ToString();
- #region
- /*switch ((myReceByte[5] >> 3) & 0x07)
- {
- case 0://500ms(默认)
- cmbSetParaWakeUpTime.Text = "000_500";
- break;
- case 1://1000ms
- cmbSetParaWakeUpTime.Text = "001_1000";
- break;
- case 2://1500ms
- cmbSetParaWakeUpTime.Text = "010_1500";
- break;
- case 3://2000ms
- cmbSetParaWakeUpTime.Text = "011_2000";
- break;
- case 4://2500ms
- cmbSetParaWakeUpTime.Text = "100_2500";
- break;
- case 5://3000ms
- cmbSetParaWakeUpTime.Text = "101_3000";
- break;
- case 6://3500ms
- cmbSetParaWakeUpTime.Text = "110_3500";
- break;
- case 7://4000ms
- cmbSetParaWakeUpTime.Text = "111_4000";
- break;
- default:
- cmbSetParaWakeUpTime.Text = "error";
- break;
- }*/
- #endregion
- //FEC 开关(默认1),字节5的第3位
- if (((myReceByte[5] >> 2) & 0x01) == 1)//
- {
- chbSetParaOpenFec.Checked = true;
- }
- else//
- {
- chbSetParaOpenFec.Checked = false;
- }
- //发射功率,字节5的第1、0位
- cmbSetParaSendPower.Text = cmbSetParaSendPower.Items[(myReceByte[5] >> 0) & 0x03].ToString();
- #region
- /*switch ((myReceByte[5] >> 0) & 0x03)
- {
- case 0://20dBm(默认)
- cmbSetParaSendPower.Text = "00_20";
- break;
- case 1://17dBm
- cmbSetParaSendPower.Text = "01_17";
- break;
- case 2://14dBm
- cmbSetParaSendPower.Text = "10_14";
- break;
- case 3://10dBm
- cmbSetParaSendPower.Text = "11_10";
- break;
- default:
- cmbSetParaSendPower.Text = "error";
- break;
- }*/
- #endregion
- #endregion
- break;
- case 0xC3://读取版本号
- txbReadVersionOut.Text = str;
- if (myReceByte[1] == 0x28)//杭州塔石
- {
- cmbSelectLoRaMode.Text = cmbSelectLoRaMode.Items[0].ToString();
- }
- else//成都亿佰特
- {
- cmbSelectLoRaMode.Text = cmbSelectLoRaMode.Items[1].ToString();
- }
- break;
- default:
- break;
- }
- }
- catch (Exception)
- {
- }
- }
- }
- #endregion//*/
- #region 没有使用
- public void portIDGet()//串口自动获取
- {
- try
- {
- comboBox1.Items.Clear();
- foreach (string com in System.IO.Ports.SerialPort.GetPortNames())//串口检测
- {
- comboBox1.Items.Add(com);
- }
- //comboBox1.Text = myConfing.ConfingGetValue("COM");//使用配置文件加载
- }
- catch (Exception er)
- {
- MessageBox.Show("端口加载失败!" + er.Message, "提示");
- }
- }
- private void comboBox1_DragDrop(object sender, DragEventArgs e)
- {
- portIDGet();
- }
- private void comboBox1_DropDown(object sender, EventArgs e)
- {
- portIDGet();
- }
- #endregion
- #region 模块选择
- private void cmbSelectLoRaMode_TextChanged(object sender, EventArgs e)//选择模块
- {
- cmbSetParaLoRaDataRate.Items.Clear();
- cmbSetParaWakeUpTime.Items.Clear();
- if (cmbSelectLoRaMode.Text == "杭州塔石")//
- {
- //LoRa的空中速率
- cmbSetParaLoRaDataRate.Items.Add("000_0.3K");
- cmbSetParaLoRaDataRate.Items.Add("001_0.6K");
- cmbSetParaLoRaDataRate.Items.Add("010_1.2K");
- cmbSetParaLoRaDataRate.Items.Add("011_2.5K");
- cmbSetParaLoRaDataRate.Items.Add("100_4.5K");
- cmbSetParaLoRaDataRate.Items.Add("101_9.1K");
- cmbSetParaLoRaDataRate.Items.Add("110_18.2K");
- cmbSetParaLoRaDataRate.Items.Add("111_21.8K");//(默认)
- //cmbSetParaLoRaDataRate.Text = "111_21.8K";//(默认)
- cmbSetParaLoRaDataRate.Text = cmbSetParaLoRaDataRate.Items[7].ToString();
- //唤醒时间
- cmbSetParaWakeUpTime.Items.Add("000_500");//(默认)
- cmbSetParaWakeUpTime.Items.Add("001_1000");
- cmbSetParaWakeUpTime.Items.Add("010_1500");
- cmbSetParaWakeUpTime.Items.Add("011_2000");
- cmbSetParaWakeUpTime.Items.Add("100_2500");
- cmbSetParaWakeUpTime.Items.Add("101_3000");
- cmbSetParaWakeUpTime.Items.Add("110_3500");
- cmbSetParaWakeUpTime.Items.Add("111_4000");
- //cmbSetParaWakeUpTime.Text = "000_500";//(默认)
- cmbSetParaWakeUpTime.Text = cmbSetParaWakeUpTime.Items[0].ToString();
- }
- else
- {
- //LoRa的空中速率
- cmbSetParaLoRaDataRate.Items.Add("000_0.3K");
- cmbSetParaLoRaDataRate.Items.Add("001_0.6K");
- cmbSetParaLoRaDataRate.Items.Add("010_1.2K");
- cmbSetParaLoRaDataRate.Items.Add("011_2.4K");//(默认)
- cmbSetParaLoRaDataRate.Items.Add("100_4.8K");
- cmbSetParaLoRaDataRate.Items.Add("101_9.6K");
- cmbSetParaLoRaDataRate.Items.Add("110_19.2K");
- cmbSetParaLoRaDataRate.Items.Add("111_19.2K");
- //cmbSetParaLoRaDataRate.Text = "011_2.4K";//(默认)
- cmbSetParaLoRaDataRate.Text = cmbSetParaLoRaDataRate.Items[3].ToString();
- //唤醒时间
- cmbSetParaWakeUpTime.Items.Add("000_250");//(默认)
- cmbSetParaWakeUpTime.Items.Add("001_500");
- cmbSetParaWakeUpTime.Items.Add("010_750");
- cmbSetParaWakeUpTime.Items.Add("011_1000");
- cmbSetParaWakeUpTime.Items.Add("100_1250");
- cmbSetParaWakeUpTime.Items.Add("101_1500");
- cmbSetParaWakeUpTime.Items.Add("110_1750");
- cmbSetParaWakeUpTime.Items.Add("111_2000");
- //cmbSetParaWakeUpTime.Text = "000_250";//(默认)
- cmbSetParaWakeUpTime.Text = cmbSetParaWakeUpTime.Items[0].ToString();
- }
- }
- #endregion
- }
- }
复制代码 |