经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#串口通信程序实现无感知签到与答题
来源:cnblogs  作者:无赖痞子  时间:2018/12/7 9:38:16  对本文有异议

最近公司项目上线,之前利用串口通讯实现校牌的无感知签到程序, 项目上线以后刚刚好有时间把之前的出现的问题做下记录,废话不多,直接到主题

串口介绍:

串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度)

正文:

最近在公司让用C#写一个串口通讯程序,下面我将这次遇到的问题和解决方法奉献出来,希望对工作中需要的朋友有所帮助!

我们来看具体的实现步骤。

公司要求实现以下几个功能:

1.)启动程序打开串口通信,接受嵌入式校牌发送过来的16进制形式的数据指令执行业务操作,业务操作完做出回应。

2.)根据需要设置串口通信的必要参数。

3.)通过校牌指令执行相关业务,拉取数据通过访问java的http接口获取数据,并将数据进行处理转换为16进制形式下发给校牌

4.)配置相关接口地址

5.)校牌答题与教室端互动通过本地UPD传递给教室端,

看着好像挺复杂,其实都是纸老虎,一戳就破,前提是你敢去戳。我尽量讲的详细一些,争取说到每个知识点。

C#代码实现:采用SerialPort

实例化一个SerialPort

  1. 1. private SerialPort ComDevice = new SerialPort();

 

我自己写了个串口的类就直接上代码

 

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Configuration;
  4. 4 using System.IO.Ports;
  5. 5 using System.Linq;
  6. 6 using System.Text;
  7. 7 using System.Threading;
  8. 8 using System.Threading.Tasks;
  9. 9
  10. 10 namespace ZPZSerialPort.ComSerialPort
  11. 11 {
  12. 12 public sealed class ComDeviceManager
  13. 13 {
  14. 14 private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();//NLog日志记录串口信息
  15. 15 private static ComDeviceManager _comManager;
  16. 16 private static readonly object _instAsync = new object();
  17. 17 public SerialPort ComDeviceComputerChip { get; private set; }
  18. 18
  19. 19 public Action<Byte[]> ActionComputerChip { get; set; }
  20. 20
  21. 21 /// <summary>
  22. 22 /// 此处配置根据实际串口进行配置,也可以配置为可变的参数
  23. 23 /// </summary>
  24. 24 private ComDeviceManager()
  25. 25 {
  26. 26 ComDeviceComputerChip = new SerialPort();//实例化一个SerialPort
  27. 27 ComDeviceComputerChip.PortName = ConfigurationManager.AppSettings["protnamexyt"];//端口号此处端口号不固定此处配置为可变参数
  28. 28 ComDeviceComputerChip.BaudRate = 115200;// 串行波特率指定为115200
  29. 29 ComDeviceComputerChip.Parity = (Parity)Convert.ToInt32("0");//
  30. 30 ComDeviceComputerChip.DataBits = Convert.ToInt32("8");
  31. 31 ComDeviceComputerChip.StopBits = (StopBits)Convert.ToInt32("1");
  32. 32 ComDeviceComputerChip.DataReceived += ComDevice1_DataReceived;
  33. 33
  34. 34 }
  35. 35 /// <summary>
  36. 36 /// 接受端口数据事件
  37. 37 /// </summary>
  38. 38 /// <param name="sender"></param>
  39. 39 /// <param name="e"></param>
  40. 40 private void ComDevice1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  41. 41 {
  42. 42 byte[] buffers = new byte[ComDeviceComputerChip.BytesToRead];
  43. 43 ComDeviceComputerChip.Read(buffers, 0, buffers.Length);
  44. 44 ActionComputerChip?.Invoke(buffers);
  45. 45 }
  46. 46 /// <summary>
  47. 47 /// 当前设备
  48. 48 /// </summary>
  49. 49 public static ComDeviceManager CurrentDevice
  50. 50 {
  51. 51 get
  52. 52 {
  53. 53 if (_comManager == null)
  54. 54 {
  55. 55 lock (_instAsync)
  56. 56 {
  57. 57 if (_comManager == null)
  58. 58 {
  59. 59 return _comManager = new ComDeviceManager();
  60. 60 }
  61. 61 }
  62. 62 }
  63. 63
  64. 64 return _comManager;
  65. 65 }
  66. 66 }
  67. 67 /// <summary>
  68. 68 /// 打开端口
  69. 69 /// </summary>
  70. 70 /// <returns></returns>
  71. 71 public bool OpenDevice()
  72. 72 {
  73. 73 try
  74. 74 {
  75. 75 if (!ComDeviceComputerChip.IsOpen)
  76. 76 {
  77. 77 ComDeviceComputerChip.Open();
  78. 78 }
  79. 79 return true;
  80. 80 }
  81. 81 catch (Exception ex)
  82. 82 {
  83. 83 logger.Error("打开设备错误:"+ex);
  84. 84 }
  85. 85
  86. 86 return false;
  87. 87 }
  88. 88 /// <summary>
  89. 89 /// 发送数据
  90. 90 /// </summary>
  91. 91 /// <param name="data"></param>
  92. 92 /// <returns></returns>
  93. 93 public bool SendDzxp(byte[] data)
  94. 94 {
  95. 95 try
  96. 96 {
  97. 97 if (ComDeviceComputerChip.IsOpen)
  98. 98 {
  99. 99 Thread.Sleep(10);// 延迟发送必须做延迟发送不然发送给校牌接受不到,这个问题浪费了一上午事件才发送在发送得时候需要做延迟
  100. 100 ComDeviceComputerChip.Write(data, 0, data.Length);//发送数据给串口端口
  101. 101 Thread.Sleep(10);// 延迟发送
  102. 102 return true;
  103. 103 }
  104. 104 }
  105. 105 catch (Exception ex)
  106. 106 {
  107. 107 logger.Error(ex);
  108. 108 }
  109. 109
  110. 110 return false;
  111. 111 }
  112. 112
  113. 113
  114. 114 }
  115. 115 }

 

 

设备操作类已经编写完毕,接着就是我们收到指令主动执行操作:操作的步骤如下几点

1.)同步时间

收到同步时间指令获取当前系统时间转换为16进制字节,进行CRC校验之后带上,发送给基站,发送的格式为

引导码+发送码+卡号+响应成功码+长度+内容(当前时间)+校验码

 

2.)同步课程

收到同步课程指令先通过接口拉取数据,把拉取到json数据解析,上课的开始时间,频点,日期,星期 数据进行解析为16进制字节数组

引导码+发送码+卡号+响应成功码+长度+内容(一天课程上课时间)+校验码

拉取到的课程与校牌成功以后 把卡号,频点,同步成功最后课程的时间 提交给接口保存

 

3.)签到

收到签到指令 进行回复

引导码+发送码+卡号+响应成功码+长度+内容(校牌发送的签到指令)+校验码

把校牌卡号与课程ID 提交给接口保存

一 通讯层格式

请求/控制数据帧

引导码

数据传输方向

设备IC卡号

命令码

数据包长度

数据内容

校验码

CRC16

FA FA

D0/D1

4 bytes

0x00~0xFF

0x00~0x3F

0~N

CRC_L

CRC_H

  • 引导码:2 bytes0xFA 0xFA
  • 数据传输方向:1 byte0xD0为电子校牌上传数据给服务器,0xD1为服务器下发数据到电子校牌;
  • 设备IC卡号:4 byte,对应内嵌电子校牌的IC卡号;
  • 命令码:1 byte,取值范围为0x00 – 0xFF
  • 数据包长度:1 byte0x00 – 0x3F
  • 数据内容:传输的数据信息,长度大小与数据包长度一致;
  • 校验码:2 bytes,低字节在前,高字节在后,采用CRC16校验方式,校验数据包括从数据传输方向到数据内容;

 

响应数据帧

引导码

数据传输方向

设备IC卡号

命令码

响应标志码

数据包长度

数据内容

校验码

CRC16

FA FA

D0/D1

4 bytes

0x00~0xFF

0x80/0x81

0x00~0x3F

0~N

CRC_L

CRC_H

  • 引导码:2 bytes0xFA 0xFA
  • 数据传输方向:1 byte0xD0为终端设备上传数据给服务器,0xD1为服务器下发数据到终端设备;
  • 设备IC卡号:4 byte,对应内嵌电子校牌的IC卡号;
  • 命令码:1 byte,取值范围为0x00 – 0xFF
  • 响应标志码:1 byte0x80-----接收正确;0x81----接收有误;

    数据有误码:0x01-----数据格式有误

                    0x02-----校验码错误

                    0x03-----题型有误

  • 数据包长度:1 byte0x00 – 0x3F
  • 数据内容:传输的数据信息,长度大小与数据包长度一致;
  • 校验码:2 bytes,低字节在前,高字节在后,采用CRC16校验方式,校验数据包括从数据传输方向到数据内容;

 

二 详细命令解析

(以设备IC卡号为0xA0 0xA1 0xA2 0xA3为例)

  1. 电子校牌连接基站服务器 0x00

    命令码: 0x00

    数据内容:年/月/日/星期/时/分/秒 7 bytes

    举例:

    Send: FA FA D0 A0 A1 A2 A3 00 00 CRC16

    Recv: FA FA D1 A0 A1 A2 A3 00 80 07 YY MM DD WW hh mm ss CRC16 // 连接成功

     

  2. 电子校牌请求服务器同步课程表 0x01

    命令码: 0x01

    数据内容:ID号:A0 A1 A2 A3

    FF FF FF FF 表示对所有电子校牌统一下发

    N=2n+1:课程表(时间、频点) 星期几+(时间(小时/分钟)+频点)* n(课节数,最大10)

    Weekday:星期一 ~ 星期六(1~6), 星期日: 0

                 时间(H/M):((H-6)<< 4) | (M/5) 分钟为5的倍数

    举例:

    Send: FA FA D0 A0 A1 A2 A3 01 00 CRC16 // 校牌请求下发课程表

    Recv: FA FA D1 A0 A1 A2 A3 01 80 N weekday 1...2n CRC16 // 服务器下发课程表

    Send: FA FA D0 A0 A1 A2 A3 01 80 01 weekday CRC16 //校牌回复设置课程表成功

     

  3. 电子校牌完成签到功能 0x02

    命令码: 0x02

    数据内容: 年/月/日/时/分/秒 6 bytes

    举例:

    Send: FA FA D0 A0 A1 A2 A3 02 06 YY MM DD hh mm ss CRC16

    Recv: FA FA D1 A0 A1 A2 A3 02 80 06 YY MM DD hh mm ss CRC16 // 签到成功

     

    处理相关业务逻辑使用工厂模式

     
    1. 1 using System;
    2. 2 using System.Collections.Generic;
    3. 3 using System.Linq;
    4. 4 using System.Text;
    5. 5 using System.Threading.Tasks;
    6. 6
    7. 7 namespace ZPZSerialPort.Factory
    8. 8 {
    9. 9 public interface ICommunication
    10. 10 {
    11. 11 bool Send(object data);
    12. 12 }
    13. 13 /// <summary>
    14. 14 /// 同步时间
    15. 15 /// </summary>
    16. 16 public class SyncTime : ICommunication//
    17. 17 {
    18. 18 public bool Send(object data)
    19. 19 {
    20. 20 Console.WriteLine("同步时间接受的数据");
    21. 21 return true;
    22. 22 }
    23. 23 }
    24. 24 /// <summary>
    25. 25 /// 同步课程
    26. 26 /// </summary>
    27. 27 public class SyncCourse : ICommunication
    28. 28 {
    29. 29 public bool Send(object data)
    30. 30 {
    31. 31 Console.WriteLine("同步课程接受的数据");
    32. 32 return true;
    33. 33 }
    34. 34 }
    35. 35 /// <summary>
    36. 36 /// 签到
    37. 37 /// </summary>
    38. 38 public class Sign : ICommunication
    39. 39 {
    40. 40 public bool Send(object data)
    41. 41 {
    42. 42 Console.WriteLine("同步课程接受的数据");
    43. 43 return true;
    44. 44 }
    45. 45
    46. 46 }
    47. 47 /// <summary>
    48. 48 /// 答题
    49. 49 /// </summary>
    50. 50 public class Answer : ICommunication
    51. 51 {
    52. 52 public bool Send(object data)
    53. 53 {
    54. 54 Console.WriteLine("答题接受的数据");
    55. 55 return true;
    56. 56 }
    57. 57 }
    58. 58
    59. 59
    60. 60 }

     

     

    1. 1 using System;
    2. 2 using System.Collections.Generic;
    3. 3 using System.Linq;
    4. 4 using System.Text;
    5. 5 using System.Threading.Tasks;
    6. 6
    7. 7 namespace ZPZSerialPort.Factory
    8. 8 {
    9. 9 /// <summary>
    10. 10 /// 通讯工厂
    11. 11 /// </summary>
    12. 12 public class CommunicationFactory
    13. 13 {
    14. 14 public ICommunication CreateCommunicationFactory(string style)
    15. 15 {
    16. 16 switch (style)
    17. 17 {
    18. 18 case "SyncTime"://同步时间
    19. 19 return new SyncTime();
    20. 20 case "SyncCourse"://同步课程
    21. 21 return new SyncCourse();
    22. 22 case "Sign"://签到
    23. 23 return new Sign();
    24. 24 case "Answer"://答题
    25. 25 return new Answer();
    26. 26 }
    27. 27 return null;
    28. 28 }
    29. 29 }
    30. 30 }

     

     

     

    处理接受得数据实体

 

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace ZPZSerialPort.COM_USB
  8. 8 {
  9. 9 /// <summary>
  10. 10 /// 响应数据帧
  11. 11 /// </summary>
  12. 12 public class USBComReceiveEntity
  13. 13 {
  14. 14 //引导码 2 bytes,0xFA 0xFA
  15. 15 public string header { get; set; }
  16. 16
  17. 17 //数据传输方向 1 byte,0xD0为电子校牌上传数据给服务器,0xD1为服务器下发数据到电子校牌
  18. 18 public string direction { get; set; }
  19. 19
  20. 20 //设备IC卡号 4 byte,对应内嵌电子校牌的IC卡号
  21. 21 public string icCard { get; set; }
  22. 22
  23. 23 //命令码 1 byte,取值范围为0x00 – 0xFF
  24. 24 public string code { get; set; }
  25. 25
  26. 26 //响应标志码:1 byte,0x80-----接收正确;0x81----接收有误
  27. 27 public string response { get; set; }
  28. 28
  29. 29 //数据包长度 1 byte,0x00 – 0x3F
  30. 30 public string length { get; set; }
  31. 31
  32. 32 //数据内容 传输的数据信息,长度大小与数据包长度一致
  33. 33 public string content { get; set; }
  34. 34
  35. 35 //校验码CRC16 2 bytes,低字节在前,高字节在后,采用CRC16校验方式,校验数据包括从数据传输方向到数据内容
  36. 36 public string check { get; set; }
  37. 37
  38. 38 /// <summary>
  39. 39 /// set 实体
  40. 40 /// </summary>
  41. 41 /// <param name="str"></param>
  42. 42 /// <returns></returns>
  43. 43 public static USBComReceiveEntity SetReceiveEntity(string str)
  44. 44 {
  45. 45 if (str == null || str.Length == 0) return null;
  46. 46 USBComReceiveEntity entity = new USBComReceiveEntity();
  47. 47 str = str.Replace(" ", "");
  48. 48 if (str.Length >= 4) entity.header = str.Substring(0, 4);
  49. 49 if (str.Length >= 6) entity.direction = str.Substring(4, 2);
  50. 50 if (str.Length >= 14) entity.icCard = str.Substring(6, 8);
  51. 51 if (str.Length >= 16) entity.code = str.Substring(14, 2);
  52. 52 if (str.Length >= 18) entity.response = str.Substring(16, 2);
  53. 53 if (str.Length >= 20) entity.length = str.Substring(18, 2);
  54. 54 int count = 0;
  55. 55 if (entity.length != null && entity.length.Length > 0) count = int.Parse(entity.length) * 2;
  56. 56 if (count > 0 && str.Length >= 20 + count) entity.content = str.Substring(20, count);
  57. 57 if (str.Length >= count + 20 + 4) entity.check = str.Substring(20 + count, 4);
  58. 58 return entity;
  59. 59 }
  60. 60
  61. 61 /// <summary>
  62. 62 /// 校验码CRC16
  63. 63 /// </summary>
  64. 64 /// <param name="sendEntity"></param>
  65. 65 /// <returns></returns>
  66. 66 public static string getCheckString(USBComReceiveEntity sendEntity)
  67. 67 {
  68. 68 string str = "";
  69. 69 if (sendEntity.direction == null || sendEntity.direction.Length == 0) str = str + USBComUtil.Com_Send;
  70. 70 else str = str + sendEntity.direction;
  71. 71 if (sendEntity.icCard == null || sendEntity.icCard.Length == 0) str = str + "";
  72. 72 else str = str + sendEntity.icCard;
  73. 73 if (sendEntity.code == null || sendEntity.code.Length == 0) str = str + "";
  74. 74 else str = str + sendEntity.code;
  75. 75 if (sendEntity.response == null || sendEntity.response.Length == 0) str = str + "";
  76. 76 else str = str + sendEntity.response;
  77. 77 if (sendEntity.length == null || sendEntity.length.Length == 0) str = str + "";
  78. 78 else str = str + sendEntity.length;
  79. 79 if (sendEntity.content == null || sendEntity.content.Length == 0) str = str + "";
  80. 80 else str = str + sendEntity.content;
  81. 81 return CRCUtil.ToModbusCRC16(str);
  82. 82 }
  83. 83
  84. 84 /// <summary>
  85. 85 /// 返回实体字符串
  86. 86 /// </summary>
  87. 87 /// <param name="sendEntity"></param>
  88. 88 /// <returns></returns>
  89. 89 public static string getEntityToString(USBComReceiveEntity sendEntity)
  90. 90 {
  91. 91 string str = "";
  92. 92 if (sendEntity.header == null || sendEntity.header.Length == 0) str = USBComUtil.Com_Header;
  93. 93 else str = sendEntity.header;
  94. 94 if (sendEntity.direction == null || sendEntity.direction.Length == 0) str = str + USBComUtil.Com_Send;
  95. 95 else str = str + sendEntity.direction;
  96. 96 if (sendEntity.icCard == null || sendEntity.icCard.Length == 0) str = str + "";
  97. 97 else str = str + sendEntity.icCard;
  98. 98 if (sendEntity.code == null || sendEntity.code.Length == 0) str = str + "";
  99. 99 else str = str + sendEntity.code;
  100. 100 if (sendEntity.response == null || sendEntity.response.Length == 0) str = str + "";
  101. 101 else str = str + sendEntity.response;
  102. 102 if (sendEntity.length == null || sendEntity.length.Length == 0) str = str + "";
  103. 103 else str = str + sendEntity.length;
  104. 104 if (sendEntity.content == null || sendEntity.content.Length == 0) str = str + "";
  105. 105 else str = str + sendEntity.content;
  106. 106 if (sendEntity.check == null || sendEntity.check.Length == 0) str = str + "";
  107. 107 else str = str + sendEntity.check;
  108. 108 return str;
  109. 109 }
  110. 110 }
  111. 111 }

 

 

 

 

CRC16校验 算法类

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace ZPZSerialPort.COM_USB
  8. 8 {
  9. 9 public class CRCUtil
  10. 10 {
  11. 11 #region CRC16
  12. 12 public static byte[] CRC16(byte[] data)
  13. 13 {
  14. 14 int len = data.Length;
  15. 15 if (len > 0)
  16. 16 {
  17. 17 ushort crc = 0xFFFF;
  18. 18
  19. 19 for (int i = 0; i < len; i++)
  20. 20 {
  21. 21 crc = (ushort)(crc ^ (data[i]));
  22. 22 for (int j = 0; j < 8; j++)
  23. 23 {
  24. 24 crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
  25. 25 }
  26. 26 }
  27. 27 byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
  28. 28 byte lo = (byte)(crc & 0x00FF); //低位置
  29. 29
  30. 30 return new byte[] { lo, hi };
  31. 31 }
  32. 32 return new byte[] { 0, 0 };
  33. 33 }
  34. 34 #endregion
  35. 35
  36. 36 #region ToCRC16
  37. 37 public static string ToCRC16(string content)
  38. 38 {
  39. 39 return ToCRC16(content, Encoding.UTF8);
  40. 40 }
  41. 41
  42. 42 public static string ToCRC16(string content, bool isReverse)
  43. 43 {
  44. 44 return ToCRC16(content, Encoding.UTF8, isReverse);
  45. 45 }
  46. 46
  47. 47 public static string ToCRC16(string content, Encoding encoding)
  48. 48 {
  49. 49 return ByteToString(CRC16(encoding.GetBytes(content)), true);
  50. 50 }
  51. 51
  52. 52 public static string ToCRC16(string content, Encoding encoding, bool isReverse)
  53. 53 {
  54. 54 return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
  55. 55 }
  56. 56
  57. 57 public static string ToCRC16(byte[] data)
  58. 58 {
  59. 59 return ByteToString(CRC16(data), true);
  60. 60 }
  61. 61
  62. 62 public static string ToCRC16(byte[] data, bool isReverse)
  63. 63 {
  64. 64 return ByteToString(CRC16(data), isReverse);
  65. 65 }
  66. 66 #endregion
  67. 67
  68. 68 #region ToModbusCRC16
  69. 69 public static string ToModbusCRC16(string s)
  70. 70 {
  71. 71 return ToModbusCRC16(s, true);
  72. 72 }
  73. 73
  74. 74 public static string ToModbusCRC16(string s, bool isReverse)
  75. 75 {
  76. 76 return ByteToString(CRC16(StringToHexByte(s)), isReverse);
  77. 77 }
  78. 78
  79. 79 public static string ToModbusCRC16(byte[] data)
  80. 80 {
  81. 81 return ToModbusCRC16(data, true);
  82. 82 }
  83. 83
  84. 84 public static string ToModbusCRC16(byte[] data, bool isReverse)
  85. 85 {
  86. 86 return ByteToString(CRC16(data), isReverse);
  87. 87 }
  88. 88 #endregion
  89. 89
  90. 90 #region ByteToString
  91. 91 public static string ByteToString(byte[] arr, bool isReverse)
  92. 92 {
  93. 93 try
  94. 94 {
  95. 95 byte hi = arr[0], lo = arr[1];
  96. 96 return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
  97. 97 }
  98. 98 catch (Exception ex) { throw (ex); }
  99. 99 }
  100. 100
  101. 101 public static string ByteToString(byte[] arr)
  102. 102 {
  103. 103 try
  104. 104 {
  105. 105 return ByteToString(arr, true);
  106. 106 }
  107. 107 catch (Exception ex) { throw (ex); }
  108. 108 }
  109. 109 #endregion
  110. 110
  111. 111 #region StringToHexString
  112. 112 public static string StringToHexString(string str)
  113. 113 {
  114. 114 StringBuilder s = new StringBuilder();
  115. 115 foreach (short c in str.ToCharArray())
  116. 116 {
  117. 117 s.Append(c.ToString("X4"));
  118. 118 }
  119. 119 return s.ToString();
  120. 120 }
  121. 121 #endregion
  122. 122
  123. 123 #region StringToHexByte
  124. 124 private static string ConvertChinese(string str)
  125. 125 {
  126. 126 StringBuilder s = new StringBuilder();
  127. 127 foreach (short c in str.ToCharArray())
  128. 128 {
  129. 129 if (c <= 0 || c >= 127)
  130. 130 {
  131. 131 s.Append(c.ToString("X4"));
  132. 132 }
  133. 133 else
  134. 134 {
  135. 135 s.Append((char)c);
  136. 136 }
  137. 137 }
  138. 138 return s.ToString();
  139. 139 }
  140. 140
  141. 141 private static string FilterChinese(string str)
  142. 142 {
  143. 143 StringBuilder s = new StringBuilder();
  144. 144 foreach (short c in str.ToCharArray())
  145. 145 {
  146. 146 if (c > 0 && c < 127)
  147. 147 {
  148. 148 s.Append((char)c);
  149. 149 }
  150. 150 }
  151. 151 return s.ToString();
  152. 152 }
  153. 153
  154. 154 /// <summary>
  155. 155 /// 字符串转16进制字符数组
  156. 156 /// </summary>
  157. 157 /// <param name="hex"></param>
  158. 158 /// <returns></returns>
  159. 159 public static byte[] StringToHexByte(string str)
  160. 160 {
  161. 161 return StringToHexByte(str, false);
  162. 162 }
  163. 163
  164. 164 /// <summary>
  165. 165 /// 字符串转16进制字符数组
  166. 166 /// </summary>
  167. 167 /// <param name="str"></param>
  168. 168 /// <param name="isFilterChinese">是否过滤掉中文字符</param>
  169. 169 /// <returns></returns>
  170. 170 public static byte[] StringToHexByte(string str, bool isFilterChinese)
  171. 171 {
  172. 172 string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
  173. 173
  174. 174 //清除所有空格
  175. 175 hex = hex.Replace(" ", "");
  176. 176 //若字符个数为奇数,补一个0
  177. 177 hex += hex.Length % 2 != 0 ? "0" : "";
  178. 178
  179. 179 byte[] result = new byte[hex.Length / 2];
  180. 180 for (int i = 0, c = result.Length; i < c; i++)
  181. 181 {
  182. 182 result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
  183. 183 }
  184. 184 return result;
  185. 185 }
  186. 186 #endregion
  187. 187 }
  188. 188 }

 

具体得业务代码就不贴出来了,由于是公司产品项目,大家都明白我也不多说。

代码下载:ZPZSerialPort.rar

不足之处,还望见谅!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号