论坛元老
 
- 积分
- 8734
- 金钱
- 8734
- 注册时间
- 2020-5-11
- 在线时间
- 4265 小时
|
程序采用 Microsoft Visual Studio 2010 中的 Visual Basic 与 Visual C# 编写。
(二者界面的控件可直接复制,这功能很方便)
“串口助手_VBnet1.0”与“串口助手1.0”的功能较全,可通过分割条调节接收区与发送区的大小,
“多字符串发送”可通过文本输入框输入数字发送对应的指令。
“串口test_VB”与“串口test_C#”为简化版,只有基本的串口相关功能。
串口助手_VBnet1.0.rar
(116.39 KB, 下载次数: 18)
串口test_VB.rar
(74.85 KB, 下载次数: 22)
串口助手1.0_C#.rar
(93.01 KB, 下载次数: 29)
串口test_C#.rar
(56.2 KB, 下载次数: 23)
部分代码:
- //--------------------------------------------------
- //发送字符串,tHex为true时字符串转为hex 来发送
- //--------------------------------------------------
- private void SendStr(string str, bool tHex)
- {
- int i, slen;
- int n;
- string str0, str1 = "", str2 = "";
- byte[] txbuf = { };
- slen = 0;
- n = 0;
- if (str == "") return;
- if (SerialPort1.IsOpen == false) return;
- if (tHex == true)//hex 发送
- {
- for (i = 0; i < str.Length; i++)
- {
- //if (((str[i] >= '0') && (str[i] <= '9')) || ((str[i] >= 'a') && (str[i] <= 'f'))|| ((str[i] >= 'A') && (str[i] <= 'F')))//提取16进制字符
- if (isHex(str[i]) == true)
- {
- str1 = str1 + str[i];
- n++;
- }
- else//处理只有一个字符的情况如:1 23 4 56
- {
- if (n == 1)
- {
- n++;
- }
- }
- if (i == (str.Length - 1))//结尾单个字符的情况:12 3
- {
- if (n == 1)
- {
- n++;
- }
- }
- if (n >= 2)//两个字符之后添加空格
- {
- str2 = str2 + str1 + " ";
- n = 0;
- str1 = "";
- }
- }
- if (str2.Length > 0)//有数据才发送
- {
- string[] s = str2.Trim().Split(' ');
- txbuf = new byte[s.Length];
- for (i = 0; i < s.Length; i++)//16进制字符串转数组
- {
- txbuf[i] = Convert.ToByte(s[i], 16);
- }
- }
- }
- else//字符串发送
- {
- txbuf = System.Text.Encoding.GetEncoding("Gb2312").GetBytes(str);
- }
- if (txbuf.Length > 0)//长度大于0才发
- {
- SerialPort1.Write(txbuf, 0, txbuf.Length);
- n_txd = (n_txd + txbuf.Length) ;// '防溢出
- LabTxn.Text = n_txd.ToString();
- }
- }
- //串口接收中断
- private void UartRx()
- {
- int i, n, r;
- string apdstr = "", s1 = "";
- byte[] rbuf = { };
- //serialPort1.ReadByte();读单字节
- n = SerialPort1.BytesToRead;
- rbuf = new byte[n];
- SerialPort1.Read(rbuf, 0, n);//读多字节
- //直接转换文本,如果中文断帧则会乱码
- //TextBox1.AppendText(System.Text.Encoding.GetEncoding("Gb2312").GetString(rbuf))
- //为便于处理中文,添加状态机处理
- for (i = 0; i < n; i++)
- {
- //TxtRxd.AppendText(rbuf[i].ToString() );
- r = rbuf[i];
- rec_hex1 = rec_hex1 + r.ToString("X2") + " ";
- if (u_rcha > 0)// '中文低字节
- {
- r = u_rcha * 256 + r;
- rec_str1 = rec_str1 + Asc2C(r); //(char)(r);
- u_rcha = 0;
- }
- else
- {
- if (r < 128)//'英文
- {
- rec_str1 = rec_str1 + (char)r;// Convert.ToString(r);
- }
- else //'中文高字节
- {
- u_rcha = r;
- }
- }
- }
- //'-------------------------存储收到的字符串
- rec_hex = rec_hex + rec_hex1;
- rec_str = rec_str + rec_str1;
- n_rxd = (n_rxd + n) ;
- LabRxn.Text = n_rxd.ToString();
- //'-------------------------添加当前字符串到TxtRxd
- if (ChkRhex.Checked == true)//'hex显示
- apdstr = rec_hex1; //'TxtRxd.AppendText(rec_hex1)
- else
- apdstr = rec_str1; //'TxtRxd.AppendText(rec_str1)
- TxtRxd.AppendText(apdstr);//添加到末尾
- }
复制代码- '--------------------------------------------------
- '发送字符串,tHex为true时字符串转为hex 来发送
- '--------------------------------------------------
- Private Sub SendStr(ByRef str As String, ByVal tHex As Boolean)
- Dim slen As Long '要发送的长度
- 'Dim ts As Long
- Dim state As Integer
- Dim i As Long
- Dim str0 As String, str1 As String = "", str2 As String = ""
- slen = 0
- state = 0
- If str = "" Then Exit Sub '字符串为空,不处理
- If SerialPort1.IsOpen = False Then Exit Sub '串口没打开,不处理
- If tHex = True Then 'hex 发送
- '将字符串中的HEX存入str1
- 'ts = DateTime.Now.Millisecond
- ReDim u_txbuf((str.Length + 1) / 2)
- For i = 0 To str.Length - 1 '16进制字符串转数组
- str0 = str.Substring(i, 1)
- Select Case state
- Case 0
- If isHex(str0) = True Then
- u_txbuf(slen) = Val("&H" & str0)
- slen = slen + 1 '只要有一个字符就长度加1
- state = 1
- End If
- Case 1
- If isHex(str0) = True Then
- u_txbuf(slen - 1) = u_txbuf(slen - 1) * 16 + Val("&H" & str0) '上一字符乘16加本字符
- End If
- state = 0
- End Select
- Next
- ReDim Preserve u_txbuf(slen - 1)
- 'Debug.Print("转换时间:" & DateTime.Now.Millisecond - ts)
- 'u_txbuf = {}
- Else '字符串发送
- u_txbuf = System.Text.Encoding.GetEncoding("Gb2312").GetBytes(str)
- End If
- If u_txbuf.Length > 0 Then '长度大于0才发
- SerialPort1.Write(u_txbuf, 0, u_txbuf.Length)
- 'n_txd += u_txbuf.Length
- n_txd = (n_txd + u_txbuf.Length) And &H6FFFFFFF '防溢出
- LabTxn.Text = n_txd
- End If
- End Sub
- '接收中断
- Private Sub uartint(ByVal sender As Object, ByVal e As EventArgs)
- Dim n As Integer, i As Integer, r As Integer
- Dim apdstr As String
- rec_hex1 = ""
- rec_str1 = ""
- n = SerialPort1.BytesToRead
- Dim rbuf(0 To n - 1) As Byte
- SerialPort1.Read(rbuf, 0, n) '批量读取
- n_rxd = (n_rxd + n) '接收计数
- LabRxn.Text = n_rxd
- '如果中文高低字节间有分包,直接转码是会有乱码的
- 'TextBox1.AppendText(System.Text.Encoding.GetEncoding("Gb2312").GetString(rbuf))
- '利用状态机处理中文
- For i = 0 To n - 1
- r = rbuf(i)
- rec_hex1 = rec_hex1 & Strings.Right("0" & Hex(rbuf(i)), 2) & " " '数据转HEX字符串
- If u_rcha > 0 Then '中文低字节
- r = u_rcha * 256 + r
- rec_str1 = rec_str1 & Chr(r)
- u_rcha = 0
- Else
- If r < 128 Then '英文
- rec_str1 = rec_str1 & Chr(r)
- Else '中文高字节
- u_rcha = r
- End If
- End If
- Next
- rec_hex = rec_hex & rec_hex1 '记录HEX
- rec_str = rec_str & rec_str1 '记录文本
- '-------------------------添加当前字符串到TxtRxd
- If ChkRhex.Checked = True Then 'hex显示
- apdstr = rec_hex1 'TxtRxd.AppendText(rec_hex1)
- Else
- apdstr = rec_str1 'TxtRxd.AppendText(rec_str1)
- End If
- TxtRxd.AppendText(apdstr) '添加到文本框
- End Sub
复制代码
|
|