C#把漢字轉換成16進制(HEX)並向串口發送數據


報警器實例:(有發送,無返回獲取)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO.Ports;
 6 using System.Text.RegularExpressions;
 7 using System.Windows.Forms;
 8 
 9 namespace ZKJFJK
10 {
11     /***
12      報警器語音輸出類,只需在調用時填寫需要播報漢字即可
13      * 例:bool TF = new sendvoice().send("機房報警溫度過高");
14      * 其返回一個bool類型值TF,當TF為True時。則發送成功,否則發送失敗;
15      */
16     class sendvoice
17     {
18         SerialPort spformdata = new SerialPort();//實例化串口通訊類
19         public bool send(string voicestr)
20         {
21             spformdata.Close();
22             spformdata.PortName = "COM9";//串口號
23             spformdata.BaudRate = 9600;//波特率
24             spformdata.DataBits = 8;//數據位
25             spformdata.StopBits = (StopBits)int.Parse("1");//停止位
26             spformdata.ReadTimeout = 500;//讀取數據的超時時間,引發ReadExisting異常
27             spformdata.Open();//打開串口
28             byte[] temp = new byte[1];
29             try
30             {
31                 /***************** 漢字轉換為十六進制數(hex)部分 ********************************/
32                 //把漢字轉換為十六進制數(hex)
33                 if ((voicestr.Length % 2) != 0)
34                 {
35                     voicestr += " ";//空格
36                 }
37                 System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
38                 byte[] bytes = chs.GetBytes(voicestr);
39                 string str = "";
40                 for (int i = 0; i < bytes.Length; i++)
41                 {
42                     str += string.Format("{0:X}", bytes[i]);
43                 }
44                 string voicehex = "23" + str + "ff ff ff"; //轉換成功的16進制數,加上報警器格式的開頭與結尾
45 
46                 /***************** 串口發送數據部分 ***********************************************/
47                 //首先判斷串口是否開啟
48                 if (spformdata.IsOpen)
49                 {
50                     int num = 0;   //獲取本次發送字節數
51                     //串口處於開啟狀態,將發送區文本發送
52                     //判斷發送模式
53                     if (true)
54                     {
55                         //以HEX模式發送
56                         //首先需要用正則表達式將用戶輸入字符中的十六進制字符匹配出來
57                         string buf = voicehex;
58                         string pattern = @"\s";
59                         string replacement = "";
60                         Regex rgx = new Regex(pattern);
61                         string send_data = rgx.Replace(buf, replacement);
62                         //不發送新行
63                         num = (send_data.Length - send_data.Length % 2) / 2;
64                         for (int i = 0; i < num; i++)
65                         {
66                             temp[0] = Convert.ToByte(send_data.Substring(i * 2, 2), 16);
67                             spformdata.Write(temp, 0, 1);  //循環發送
68                         }
69                         //自動發送新行
70                         spformdata.WriteLine("");
71                         return true;
72                     }
73                 }
74             }
75             catch (Exception ex)
76             {
77                 spformdata.Close();
78                 //捕獲到異常,創建一個新的對象,之前的不可以再用
79                 spformdata = new System.IO.Ports.SerialPort();
80                 //響鈴並顯示異常給用戶
81                 System.Media.SystemSounds.Beep.Play();
82                 MessageBox.Show(ex.Message);
83             }
84             return false;
85         }
86     }
87 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM