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 }
相关文章
相关标签/搜索