上位机学习记录(10) 扫码枪
上位机学习记录(10) 扫码枪
设计思路
扫码枪本质上是个串口设备,连接串口以后,给缓冲区编写函数处理数据,然后抛出数据
- 编写串口连接,连接扫码枪。然后绑定DataReceived事件(按照扫描枪传入的数据多少,创建多大的缓冲区,然后根据编码格式,把数据传递给一个委托对象)。
- 编写事件处理信息(自定义事件类)
代码:ScannerSerial.cs
public class ScannerSerial
{
//创建一个事件
public event Action<object, TEventArgs> ScannerReceived;
private SerialPort scanner;
/// <summary>
/// 连接串口
/// </summary>
/// <param name="baudRate">波特率</param>
/// <param name="portName">串口号</param>
/// <param name="parity">校验位</param>
/// <param name="dataBits">数据位</param>
/// <param name="stopBits">停止位</param>
public void Connect(int baudRate, string portName, Parity parity, int dataBits, StopBits stopBits)
{
if (scanner == null)
{
scanner = new SerialPort();
}
if (scanner.IsOpen)
{
scanner.Close();
}
scanner.BaudRate = baudRate;
scanner.PortName = portName;
scanner.Parity = parity;
scanner.DataBits = dataBits;
scanner.StopBits = stopBits;
scanner.ReceivedBytesThreshold = 1;
scanner.DataReceived += Scanner_DataReceived;
scanner.Open();
}
private void Scanner_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] rcv = new byte[scanner.BytesToRead];
scanner.Read(rcv, 0, rcv.Length);
string message = Encoding.Default.GetString(rcv);
message = message.Replace("\r", "");
ScannerReceived?.Invoke(this, new TEventArgs(message));
}
public void DisConnect()
{
if (scanner != null)
{
scanner.Close();
}
}
}
public class TEventArgs : EventArgs
{
public TEventArgs(string message)
{
this.Message = message;
}
public string Message { get; set; }
}
第二步:对于扫码枪功能进行封装
Scanner.cs。外部函数只需要调用Connect方法就能完成扫码枪的初始化
public class Scanner
{
//创建一个事件
public event Action<object, TEventArgs> ScannerReceived;
private ScannerSerial scanner = new ScannerSerial();
public OperationResult Connect(BasicParam basicParam)
{
Parity parity = Parity.None;
int dataBits = 8;
StopBits stopBits = StopBits.One;
GetPortParam(basicParam.ScannerParam, out parity, out dataBits, out stopBits);
OperationResult result = new OperationResult();
try
{
scanner.ScannerReceived += Scanner_ScannerReceived;
scanner.Connect(basicParam.ScannerBaud, basicParam.ScannerPort, parity, dataBits, stopBits);
}
catch (Exception ex)
{
result.IsSuccess = false;
result.ErrorMsg = ex.Message;
return result;
}
return OperationResult.CreateSuccessResult();
}
private void Scanner_ScannerReceived(object sender, TEventArgs e)
{
ScannerReceived?.Invoke(sender, e);
}
public void DisConnect()
{
scanner.DisConnect();
}
///
private void GetPortParam(string param, out Parity parity, out int dataBits, out StopBits stopBits)
{
parity = Parity.None;
dataBits = 8;
stopBits = StopBits.One;
if (param.Length == 3)
{
switch (param.Substring(0, 1))
{
case "N": parity = Parity.None; break;
case "E": parity = Parity.Even; break;
case "O": parity = Parity.Odd; break;
default:
break;
}
switch (param.Substring(1, 1))
{
case "8": dataBits = 8; break;
case "7": dataBits = 7; break;
default:
break;
}
switch (param.Substring(2, 1))
{
case "1": stopBits = StopBits.One; break;
case "2": stopBits = StopBits.Two; break;
default:
break;
}
}
}
}
设计思想:
把底层功能进行封装。外部就需要调用封装后的结果就好
第三步:外部调用代码(初始化)
///创建扫码枪通信对象
private Scanner scanner = new Scanner();
void Fun()
{
result = scanner.Connect(motionEx.basicParam);
SetScanner(result.IsSuccess);
if (!result.IsSuccess)
{
AddLog(1, "扫码枪连接失败:" + result.ErrorMsg);
}
else
{
scanner.ScannerReceived += Scanner_ScannerReceived;
AddLog(0, "扫码枪连接成功");
}
}
private void Scanner_ScannerReceived(object sender, TEventArgs e)
{
if (ScannerRcv != null)
{
ScannerRcv(e.Message);
}
}
//获取扫码
private Action<string> ScannerRcv;