You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.1 KiB
C#
119 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace SocketProcess.SerialPort
|
|
{
|
|
public class BarCodeGunManager
|
|
{
|
|
|
|
public BarCodeGunManager()
|
|
{
|
|
|
|
}
|
|
|
|
//#region 单例模式
|
|
//private readonly static object syac=new object();
|
|
|
|
//private static BarCodeGunManager _instance;
|
|
|
|
//public static BarCodeGunManager Instance
|
|
//{
|
|
// get
|
|
// {
|
|
// if (_instance == null)
|
|
// {
|
|
// lock (syac)
|
|
// {
|
|
// _instance = new BarCodeGunManager();
|
|
// }
|
|
// }
|
|
// return _instance;
|
|
// }
|
|
//}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
public List<SerialEquip> EquipLsit = new List<SerialEquip>();
|
|
|
|
public void OpenAllEquip()
|
|
{
|
|
foreach (SerialEquip equip in EquipLsit)
|
|
{
|
|
equip.Comm.Open();
|
|
equip.Comm.DataReceived +=equip.Serial_DataReceived;
|
|
}
|
|
}
|
|
|
|
//private void Serial_DataReceived(byte[] readBuffer)
|
|
//{
|
|
|
|
// string Data = System.Text.Encoding.Default.GetString(readBuffer);
|
|
// //是否需要判断条码的完整性
|
|
// //绑定内部条码和当前条码
|
|
//}
|
|
}
|
|
|
|
public class SerialEquip
|
|
{
|
|
public SerialHelper Comm;
|
|
public delegate void Scandeleget(string code, SerialEquip equip);
|
|
public Scandeleget SerialData;
|
|
public SerialEquip(string Com,string Type)
|
|
{
|
|
this.EquipType = Type;
|
|
this.EquipCom = Com;
|
|
Comm.DataReceived += Serial_DataReceived;
|
|
Comm = new SerialHelper();
|
|
}
|
|
|
|
public void OpenCom()
|
|
{
|
|
if (Comm.IsOpen)
|
|
{
|
|
Comm.Close();
|
|
}
|
|
Comm.serialPort.PortName = EquipCom;
|
|
//波特率
|
|
Comm.serialPort.BaudRate = 9600;
|
|
//数据位
|
|
Comm.serialPort.DataBits = 8;
|
|
//两个停止位
|
|
Comm.serialPort.StopBits = System.IO.Ports.StopBits.One;
|
|
//无奇偶校验位
|
|
Comm.serialPort.Parity = System.IO.Ports.Parity.None;
|
|
Comm.serialPort.ReadTimeout = 200;
|
|
Comm.serialPort.WriteTimeout = -1;
|
|
Comm.Open();
|
|
}
|
|
public void Serial_DataReceived(byte[] readBuffer)
|
|
{
|
|
|
|
string Data = System.Text.Encoding.Default.GetString(readBuffer);
|
|
if (SerialData!=null)
|
|
{
|
|
SerialData(Data,this);
|
|
}
|
|
//是否需要判断条码的完整性
|
|
//绑定内部条码和当前条码
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设备名称
|
|
/// </summary>
|
|
public string EquipName { get; set; }
|
|
/// <summary>
|
|
/// 设备类型
|
|
/// </summary>
|
|
public string EquipType { get; set; }
|
|
/// <summary>
|
|
/// 串口名称
|
|
/// </summary>
|
|
public string EquipCom { get; set; }
|
|
|
|
}
|
|
}
|