分享

C#源码多线程多串口

 laoluo2017 2017-04-20
多个串口,最好用多线程,这样比较方便 ,
也可以在一个线程中,一个一个读串口数据!本源码是从网上收集的,用于保留自己以后使用,同时希望对需要的朋友提供一点学习的资料。
using System;
using System.IO.Ports;
using System.Threading;
using System.Text;
namespace Tangxu.Common
{
public class ReadCom
{
public ReadCom()
{
_ReadConfig = new ReadConfigure(System.Environment.CurrentDirectory+ '\\Com_Info.xml');
}
public ReadCom(string sCom,int nBaud):this()
{
}
private byte[] _ReadBuffer;
private SerialPort ss_port = new SerialPort();
private static int nReadCount = 0;
private ReadConfigure _ReadConfig;
#region Initialize com port
public bool InitCom()//初始化建串口类实例
{
// return true;
try
{
ss_port.PortName = _ReadConfig.GetNodeValue('
PORT');// _sComPort;
ss_port.BaudRate =int.Parse(_ReadConfig.GetNodeValue('BAUD'));//_nBaud;
ss_port.ReadBufferSize = 10240;
ss_port.DataBits =int.Parse(_ReadConfig.GetNodeValue('DATA'));//8;
switch (_ReadConfig.GetNodeValue('PARITY'))
{
case 'None':
ss_port.Parity = Parity.None;
break;
case 'Even':
ss_port.Parity = Parity.Even;
break;
case 'Mark':
ss_port.Parity = Parity.Mark;
break;
case 'Odd':
ss_port.Parity = Parity.Odd;
break;
case 'Space':
ss_port.Parity = Parity.Mark;
break;
}
switch (_ReadConfig.GetNodeValue('STOP'))
{
case '1':
ss_port.StopBits = StopBits.One;
break;
case '1.5':
ss_port.StopBits = StopBits.OnePointFive;
break;
case '2':
ss_port.StopBits = StopBits.Two;
break;
}
ss_port.ReadTimeout = 600;
ss_port.WriteTimeout = 700;
ss_port.Open();//打开串口
return true;
}
catch (Exception ex)
{
throw new Exception('打开串口失败!\r\n错误信息:' + ex.Message);
}
}
#endregion
#region FreeDrv
///
/// free opw
///

public void FreeDrv()
{
try
{
if (ss_port != null)
{
ss_port.Close();
}
}
catch
{ }
}
#endregion
#region Write command to OPW
///
/// 发操作命令给OPW设备
/// 并返回状态
///

///
///
public string WriteCommand(string sCommand)
{
StringBuilder sb = new StringBuilder();
bool bRead = true;
try
{
ss_port.DiscardInBuffer();
ss_port.Write(sCommand);
Thread.Sleep(1500);
while (bRead)
{
_ReadBuffer = new byte[ss_port.BytesToRead];
ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
Thread.Sleep(500);
if (ss_port.BytesToRead <=>
{
bRead= false;
}
}
if (sb.ToString().Length== 0)
{
nReadCount++;
}
if (nReadCount == 3)
{
nReadCount = 0;
throw new Exception('设置不正确或没有联接设备!');
}
}
catch (Exception ex)
{
throw new Exception('从设备获取数据失败!\r\n错误信息:' + ex.Message);
}
return sb.ToString(); ;
}
public string WriteCommand(byte[] bCommand)
{
StringBuilder sb = new StringBuilder();
bool bRead = true;
try
{
ss_port.DiscardInBuffer();
ss_port.Write(bCommand,0,bCommand.Length);
Thread.Sleep(1500);
while (bRead)
{
_ReadBuffer = new byte[ss_port.BytesToRead];
ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
Thread.Sleep(500);
if (ss_port.BytesToRead <=>
{
bRead = false;
}
}
if (sb.ToString().Length == 0)
{
nReadCount++;
}
if (nReadCount == 3)
{
nReadCount = 0;
throw new Exception('设置不正确或没有联接设备!');
}
}
catch (Exception ex)
{
throw new Exception('从设备获取数据失败!\r\n错误信息:' + ex.Message);
}
return sb.ToString();
}
#endregion
#region Get All COM Port
public string[] GetAllComPort()
{
string[] sAllPort = null;
try
{
sAllPort = SerialPort.GetPortNames();
}
catch (Exception ex)
{
throw new Exception('获取计算机COM口列表失败!\r\n错误信息:' + ex.Message);
}
return sAllPort;
}
#endregion
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多