C#串口通信程序实现无感知签到与答题

时间:2023-03-09 20:41:07
C#串口通信程序实现无感知签到与答题

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

串口介绍:

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

正文:

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

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

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

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

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

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

4.)配置相关接口地址

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

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

C#代码实现:采用SerialPort

实例化一个SerialPort

1. private SerialPort ComDevice = new SerialPort();

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

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace ZPZSerialPort.ComSerialPort
{
public sealed class ComDeviceManager
{
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();//NLog日志记录串口信息
private static ComDeviceManager _comManager;
private static readonly object _instAsync = new object();
public SerialPort ComDeviceComputerChip { get; private set; } public Action<Byte[]> ActionComputerChip { get; set; } /// <summary>
/// 此处配置根据实际串口进行配置,也可以配置为可变的参数
/// </summary>
private ComDeviceManager()
{
ComDeviceComputerChip = new SerialPort();//实例化一个SerialPort
ComDeviceComputerChip.PortName = ConfigurationManager.AppSettings["protnamexyt"];//端口号此处端口号不固定此处配置为可变参数
ComDeviceComputerChip.BaudRate = ;// 串行波特率指定为115200
ComDeviceComputerChip.Parity = (Parity)Convert.ToInt32("");//
ComDeviceComputerChip.DataBits = Convert.ToInt32("");
ComDeviceComputerChip.StopBits = (StopBits)Convert.ToInt32("");
ComDeviceComputerChip.DataReceived += ComDevice1_DataReceived; }
/// <summary>
/// 接受端口数据事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComDevice1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffers = new byte[ComDeviceComputerChip.BytesToRead];
ComDeviceComputerChip.Read(buffers, , buffers.Length);
ActionComputerChip?.Invoke(buffers);
}
/// <summary>
/// 当前设备
/// </summary>
public static ComDeviceManager CurrentDevice
{
get
{
if (_comManager == null)
{
lock (_instAsync)
{
if (_comManager == null)
{
return _comManager = new ComDeviceManager();
}
}
} return _comManager;
}
}
/// <summary>
/// 打开端口
/// </summary>
/// <returns></returns>
public bool OpenDevice()
{
try
{
if (!ComDeviceComputerChip.IsOpen)
{
ComDeviceComputerChip.Open();
}
return true;
}
catch (Exception ex)
{
logger.Error("打开设备错误:"+ex);
} return false;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public bool SendDzxp(byte[] data)
{
try
{
if (ComDeviceComputerChip.IsOpen)
{
Thread.Sleep();// 延迟发送必须做延迟发送不然发送给校牌接受不到,这个问题浪费了一上午事件才发送在发送得时候需要做延迟
ComDeviceComputerChip.Write(data, , data.Length);//发送数据给串口端口
Thread.Sleep();// 延迟发送
return true;
}
}
catch (Exception ex)
{
logger.Error(ex);
} return false;
} }
}

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

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 bytes,0xFA 0xFA;
  • 数据传输方向:1 byte,0xD0为电子校牌上传数据给服务器,0xD1为服务器下发数据到电子校牌;
  • 设备IC卡号:4 byte,对应内嵌电子校牌的IC卡号;
  • 命令码:1 byte,取值范围为0x00 – 0xFF;
  • 数据包长度:1 byte,0x00 – 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 bytes,0xFA 0xFA;
  • 数据传输方向:1 byte,0xD0为终端设备上传数据给服务器,0xD1为服务器下发数据到终端设备;
  • 设备IC卡号:4 byte,对应内嵌电子校牌的IC卡号;
  • 命令码:1 byte,取值范围为0x00 – 0xFF;
  • 响应标志码:1 byte,0x80-----接收正确;0x81----接收有误;

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

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

0x03-----题型有误

  • 数据包长度:1 byte,0x00 – 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 // 签到成功

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

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks; namespace ZPZSerialPort.Factory
    {
    public interface ICommunication
    {
    bool Send(object data);
    }
    /// <summary>
    /// 同步时间
    /// </summary>
    public class SyncTime : ICommunication//
    {
    public bool Send(object data)
    {
    Console.WriteLine("同步时间接受的数据");
    return true;
    }
    }
    /// <summary>
    /// 同步课程
    /// </summary>
    public class SyncCourse : ICommunication
    {
    public bool Send(object data)
    {
    Console.WriteLine("同步课程接受的数据");
    return true;
    }
    }
    /// <summary>
    /// 签到
    /// </summary>
    public class Sign : ICommunication
    {
    public bool Send(object data)
    {
    Console.WriteLine("同步课程接受的数据");
    return true;
    } }
    /// <summary>
    /// 答题
    /// </summary>
    public class Answer : ICommunication
    {
    public bool Send(object data)
    {
    Console.WriteLine("答题接受的数据");
    return true;
    }
    } }
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks; namespace ZPZSerialPort.Factory
    {
    /// <summary>
    /// 通讯工厂
    /// </summary>
    public class CommunicationFactory
    {
    public ICommunication CreateCommunicationFactory(string style)
    {
    switch (style)
    {
    case "SyncTime"://同步时间
    return new SyncTime();
    case "SyncCourse"://同步课程
    return new SyncCourse();
    case "Sign"://签到
    return new Sign();
    case "Answer"://答题
    return new Answer();
    }
    return null;
    }
    }
    }

    处理接受得数据实体

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ZPZSerialPort.COM_USB
{
/// <summary>
/// 响应数据帧
/// </summary>
public class USBComReceiveEntity
{
//引导码 2 bytes,0xFA 0xFA
public string header { get; set; } //数据传输方向 1 byte,0xD0为电子校牌上传数据给服务器,0xD1为服务器下发数据到电子校牌
public string direction { get; set; } //设备IC卡号 4 byte,对应内嵌电子校牌的IC卡号
public string icCard { get; set; } //命令码 1 byte,取值范围为0x00 – 0xFF
public string code { get; set; } //响应标志码:1 byte,0x80-----接收正确;0x81----接收有误
public string response { get; set; } //数据包长度 1 byte,0x00 – 0x3F
public string length { get; set; } //数据内容 传输的数据信息,长度大小与数据包长度一致
public string content { get; set; } //校验码CRC16 2 bytes,低字节在前,高字节在后,采用CRC16校验方式,校验数据包括从数据传输方向到数据内容
public string check { get; set; } /// <summary>
/// set 实体
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static USBComReceiveEntity SetReceiveEntity(string str)
{
if (str == null || str.Length == ) return null;
USBComReceiveEntity entity = new USBComReceiveEntity();
str = str.Replace(" ", "");
if (str.Length >= ) entity.header = str.Substring(, );
if (str.Length >= ) entity.direction = str.Substring(, );
if (str.Length >= ) entity.icCard = str.Substring(, );
if (str.Length >= ) entity.code = str.Substring(, );
if (str.Length >= ) entity.response = str.Substring(, );
if (str.Length >= ) entity.length = str.Substring(, );
int count = ;
if (entity.length != null && entity.length.Length > ) count = int.Parse(entity.length) * ;
if (count > && str.Length >= + count) entity.content = str.Substring(, count);
if (str.Length >= count + + ) entity.check = str.Substring( + count, );
return entity;
} /// <summary>
/// 校验码CRC16
/// </summary>
/// <param name="sendEntity"></param>
/// <returns></returns>
public static string getCheckString(USBComReceiveEntity sendEntity)
{
string str = "";
if (sendEntity.direction == null || sendEntity.direction.Length == ) str = str + USBComUtil.Com_Send;
else str = str + sendEntity.direction;
if (sendEntity.icCard == null || sendEntity.icCard.Length == ) str = str + "";
else str = str + sendEntity.icCard;
if (sendEntity.code == null || sendEntity.code.Length == ) str = str + "";
else str = str + sendEntity.code;
if (sendEntity.response == null || sendEntity.response.Length == ) str = str + "";
else str = str + sendEntity.response;
if (sendEntity.length == null || sendEntity.length.Length == ) str = str + "";
else str = str + sendEntity.length;
if (sendEntity.content == null || sendEntity.content.Length == ) str = str + "";
else str = str + sendEntity.content;
return CRCUtil.ToModbusCRC16(str);
} /// <summary>
/// 返回实体字符串
/// </summary>
/// <param name="sendEntity"></param>
/// <returns></returns>
public static string getEntityToString(USBComReceiveEntity sendEntity)
{
string str = "";
if (sendEntity.header == null || sendEntity.header.Length == ) str = USBComUtil.Com_Header;
else str = sendEntity.header;
if (sendEntity.direction == null || sendEntity.direction.Length == ) str = str + USBComUtil.Com_Send;
else str = str + sendEntity.direction;
if (sendEntity.icCard == null || sendEntity.icCard.Length == ) str = str + "";
else str = str + sendEntity.icCard;
if (sendEntity.code == null || sendEntity.code.Length == ) str = str + "";
else str = str + sendEntity.code;
if (sendEntity.response == null || sendEntity.response.Length == ) str = str + "";
else str = str + sendEntity.response;
if (sendEntity.length == null || sendEntity.length.Length == ) str = str + "";
else str = str + sendEntity.length;
if (sendEntity.content == null || sendEntity.content.Length == ) str = str + "";
else str = str + sendEntity.content;
if (sendEntity.check == null || sendEntity.check.Length == ) str = str + "";
else str = str + sendEntity.check;
return str;
}
}
}

CRC16校验 算法类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ZPZSerialPort.COM_USB
{
public class CRCUtil
{
#region CRC16
public static byte[] CRC16(byte[] data)
{
int len = data.Length;
if (len > )
{
ushort crc = 0xFFFF; for (int i = ; i < len; i++)
{
crc = (ushort)(crc ^ (data[i]));
for (int j = ; j < ; j++)
{
crc = (crc & ) != ? (ushort)((crc >> ) ^ 0xA001) : (ushort)(crc >> );
}
}
byte hi = (byte)((crc & 0xFF00) >> ); //高位置
byte lo = (byte)(crc & 0x00FF); //低位置 return new byte[] { lo, hi };
}
return new byte[] { , };
}
#endregion #region ToCRC16
public static string ToCRC16(string content)
{
return ToCRC16(content, Encoding.UTF8);
} public static string ToCRC16(string content, bool isReverse)
{
return ToCRC16(content, Encoding.UTF8, isReverse);
} public static string ToCRC16(string content, Encoding encoding)
{
return ByteToString(CRC16(encoding.GetBytes(content)), true);
} public static string ToCRC16(string content, Encoding encoding, bool isReverse)
{
return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
} public static string ToCRC16(byte[] data)
{
return ByteToString(CRC16(data), true);
} public static string ToCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion #region ToModbusCRC16
public static string ToModbusCRC16(string s)
{
return ToModbusCRC16(s, true);
} public static string ToModbusCRC16(string s, bool isReverse)
{
return ByteToString(CRC16(StringToHexByte(s)), isReverse);
} public static string ToModbusCRC16(byte[] data)
{
return ToModbusCRC16(data, true);
} public static string ToModbusCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion #region ByteToString
public static string ByteToString(byte[] arr, bool isReverse)
{
try
{
byte hi = arr[], lo = arr[];
return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, ).ToUpper().PadLeft(, '');
}
catch (Exception ex) { throw (ex); }
} public static string ByteToString(byte[] arr)
{
try
{
return ByteToString(arr, true);
}
catch (Exception ex) { throw (ex); }
}
#endregion #region StringToHexString
public static string StringToHexString(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
s.Append(c.ToString("X4"));
}
return s.ToString();
}
#endregion #region StringToHexByte
private static string ConvertChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c <= || c >= )
{
s.Append(c.ToString("X4"));
}
else
{
s.Append((char)c);
}
}
return s.ToString();
} private static string FilterChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c > && c < )
{
s.Append((char)c);
}
}
return s.ToString();
} /// <summary>
/// 字符串转16进制字符数组
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static byte[] StringToHexByte(string str)
{
return StringToHexByte(str, false);
} /// <summary>
/// 字符串转16进制字符数组
/// </summary>
/// <param name="str"></param>
/// <param name="isFilterChinese">是否过滤掉中文字符</param>
/// <returns></returns>
public static byte[] StringToHexByte(string str, bool isFilterChinese)
{
string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str); //清除所有空格
hex = hex.Replace(" ", "");
//若字符个数为奇数,补一个0
hex += hex.Length % != ? "" : ""; byte[] result = new byte[hex.Length / ];
for (int i = , c = result.Length; i < c; i++)
{
result[i] = Convert.ToByte(hex.Substring(i * , ), );
}
return result;
}
#endregion
}
}

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

代码下载:ZPZSerialPort.rar

不足之处,还望见谅!C#串口通信程序实现无感知签到与答题C#串口通信程序实现无感知签到与答题