C#设备处理类操作

时间:2022-03-01 03:06:10

C#对于处理window操作系统下的设备有天然的优势,对于大多数设备读写等操作来说基本上够了,这里只讨论通过普通的大多数的设备的操作。涉及到两大类SerialPort类,Socket的一些操作。不一定好,但希望分享出去,让更多的人受益。。

由于设备的读写方式不同,串口,网口,usb,等各种各样不同的方式,所以对外的操作,可能就达不到统一,没法集中处理,造成很大程度代码冗余,会给维护带来很大不便。需要一个父类来对不同操作进行统一的一个约束,同时可以对外有一个统一的j接口,方便业务上边的一些处理。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace EquipmentOption
{
public abstract class Equipment
{ /// <summary>
/// 读取到的Code
/// </summary>
public string Code;
/// <summary>
/// 错误消息
/// </summary>
public string Error = string.Empty;
public BaseEquipment EquipmentModel; public int ReadTimeOut=;
public Equipment(BaseEquipment Model)
{
this.EquipmentModel = Model;
}
/// <summary>
/// 扫描事件
/// </summary>
private int scanning;
public int Scanning
{
get
{
return this.scanning;
}
set
{
this.scanning = value;
EquipmentArgs e = new EquipmentArgs(this.Code, this.Error);
OnSetVelues(e);
}
}
public event SetEventHandler SetEvent;
public delegate void SetEventHandler(object sender, EquipmentArgs e);
public class EquipmentArgs : EventArgs
{
public string Code;
public string Error;
public EquipmentArgs(string SnCode,string error)
{
this.Code = SnCode;
this.Error = error;
}
}
public void OnSetVelues(EquipmentArgs e)
{
if (this.SetEvent != null)
{
this.SetEvent(this, e);
}
}
/// <summary>
/// 检测设备
/// </summary>
/// <param name="message">错误消息返回值,仅当返回false才有值</param>
/// <returns></returns>
public abstract bool test(out string message);
/// <summary>
/// 给设备发送指令
/// </summary>
/// <param name="command">指令内容</param>
/// <param name="type">指令类型</param>
/// <returns></returns>
public abstract string SendMessage(String command, CommandType type);
} }

父类里边主要定义了一些公用的属性,以及一个简单的事件转发。这个事件转发用于统一网口设备和串口设备的获取数据方式

调用方式如下:

        private void Form1_Load(object sender, EventArgs e)
{
Equipment Comquip = new ComEquipment(new BaseEquipment());
Comquip.SetEvent += Equipment_GetCodeEvent;
Equipment IPEquip = new IPEquipment(new BaseEquipment());
IPEquip.SetEvent += Equipment_GetCodeEvent;
}
void Equipment_GetCodeEvent(object sender, Equipment.EquipmentArgs e)
{
string code = e.Code;//这里的Code就是从设备中读取到的值
}

可以把需要用到的基础消息丢到baseEquipment中用来初始化对应的设备,然后,把对于设备读取到的信息就是这里的e.code。不管网口串口都是一样的返回.

设备的操作无非读写

对于用串口连接的设备:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace EquipmentOption
{
public class ComEquipment : Equipment
{
private SerialPort Port;
public ComEquipment(BaseEquipment baseModel) :
base(baseModel)
{
this.Port = new SerialPort(baseModel.Port);
Port.BaudRate = baseModel.BaudRate;
Port.StopBits = (StopBits)baseModel.StopBit;
Port.Parity = (Parity)baseModel.ParityCheck;
this.Port.DataReceived += Port_DataReceived;
}
//事件转发
void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(this.EquipmentModel.SleepTime);
this.Error = string.Empty;
this.Code = string.Empty;
try
{
switch (this.EquipmentModel.ReadType)
{
case :
this.Code = (sender as SerialPort).ReadLine();
break;
case :
this.Code = (sender as SerialPort).ReadExisting();
break;
default:
Error = string.Concat(this.EquipmentModel.EquipmentName, "读取有误");
break;
}
++this.Scanning;//这里切记是属性值变化才会触发事件 }
catch
{
Error = string.Concat(this.EquipmentModel.EquipmentName, "配置错误,请调整");
}
} /// <summary>
/// 检测
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public override bool test(out string message)
{
message = "";
bool result = false;
if (this.Port != null)
{
try
{
Port.Open();
result = true;
}
catch
{
message = string.Concat("设备", this.EquipmentModel.EquipmentName, "异常");
result = false;
}
finally
{
if (Port.IsOpen)
{
Port.Close();
}
}
}
else
{
message = string.Concat("设备", this.EquipmentModel.EquipmentName, "初始化失败");
result = false;
}
return result;
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="command">命令</param>
public override string SendMessage(String command, CommandType type)
{
if (!String.IsNullOrEmpty(command) && this.Port.IsOpen)
{
switch (type)
{
case CommandType.GetBytes:
Byte[] commandsGetBytes = CommandConvert.CommandByGetBytes(command);
this.Port.Write(commandsGetBytes, , commandsGetBytes.Length);
break;
case CommandType.Command16:
Byte[] commands16 = CommandConvert.CommandFrom16(command);
this.Port.Write(commands16, , commands16.Length);
break;
case CommandType.Command10:
Byte[] commands10 = CommandConvert.CommandFrom10(command);
this.Port.Write(commands10, , commands10.Length);
break;
case CommandType.CommandText:
this.Port.Write(command);
break;
}
return this.Port.PortName + "发送数据:" + command;
}
else
{
return "串口" + this.Port.PortName + "未打开";
}
}
}
}

对于用网口连接的设备:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace EquipmentOption
{
public class IPEquipment : Equipment
{
private IPEndPoint IPPort;
private IPAddress IP;
public IPEquipment(BaseEquipment baseModel) :
base(baseModel)
{
int Port = ;
if (int.TryParse(baseModel.Port, out Port))
{
this.IP = IPAddress.Parse(baseModel.IPAddress);
this.IPPort = new IPEndPoint(IP, Port);
Thread t = new Thread(new ParameterizedThreadStart(ScanEvent));
t.Start(IPPort);
}
} public override bool test(out string message)
{
bool result = false; message = "";
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 创建Socket
try
{
c.SendTimeout = ;
c.Connect(IPPort); //连接到服务器
result = true;
}
catch
{
message =string.Concat("设备" , this.EquipmentModel.EquipmentName , "异常");
result = false;
}
finally
{
c.Close();
}
return result;
}
public void ScanEvent(object ipe)
{
while (true)
{
try
{
//创建Socket并连接到服务器
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 创建Socket
c.Connect((IPEndPoint)ipe); //连接到服务器
//接受从服务器返回的信息
byte[] recvBytes = new byte[];
int bytes;
//bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
bytes = c.Receive(recvBytes);
string Code = Encoding.Default.GetString(recvBytes, , bytes);
Code = Code.Replace(EquipmentModel.Suffix, "");
this.Code = Code;
c.Close();
++this.Scanning;
}
catch(Exception ex)
{
Error = ex.ToString();
continue;
}
}
} public override string SendMessage(string command, CommandType type)
{
//new mothed to SendMessage;
return "";
}
}
}

对于扩展而言,需要做的仅仅是不同类别的设备再增加不同的子类去继承抽象类.