zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    数据库系统概论习题集 第八章 数据库并发控制
    数据库系统概论习题集 第七章 数据库恢复技术
    数据库系统概论习题集 第六章 数据库设计
    数据库系统概论习题集 第五章 关系数据理论
    数据库系统概论习题集 第四章 关系系统及其优化
    数据库系统概论习题集 第三章 SQL语言
    【转载】【笔记】vue-router之路由传递参数
    【Dos】复制指定文件夹下所有文件到另外指定文件夹下
    【转载】Vue.js 安装及其环境搭建
    【转载】解决:'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  • 原文地址:https://www.cnblogs.com/weijiazheng/p/10712384.html
Copyright © 2011-2022 走看看