C#中serialPort 接收多个串口数据 C# 中接收多个Com口的数据.只要求接收不发送.用serialPort来如何实现.还是要用多线程进行读取希望有这方面经验的高手们给点意见...拜谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哥们,我也遇到这方面的问题啊,现在也是要开发从串口读数据的问题。就是用serialPort类来通讯的。设置好参数后打开串口,读写数据,关闭串口。你要是同时从多个com口读取的话肯定得用多线程的。不知道你的不发送数据什么意思?你不可能不向串口发送信号吧。这样也是发送啊,只有发送信号了才能相应接受数据啊! 多个串口,最好用多线程,这样比较方便也可以在一个线程中,一个一个读串口数据!/****************************************************************** * Copyright(c) : Tangxu * Description : * CreateDate : 2006-9-01 04:53:08 * Creater : Tang.xu * LastChangeDate: * LastChanger : * Version Info : 1.0.0 * ******************************************************************/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 /// <summary> /// free opw /// </summary> public void FreeDrv() { try { if (ss_port != null) { ss_port.Close(); } } catch { } } #endregion #region Write command to OPW /// <summary> /// 发操作命令给OPW设备 /// 并返回状态 /// </summary> /// <param name="sCommand"></param> /// <returns></returns> 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 <= 0) { 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 <= 0) { 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 }} 兄弟你这个就别发啦!总是提示说ReadConfigure 没有引入适合的命名空间啊!这么回事???? 兄弟你这个就别发啦!总是提示说ReadConfigure 没有引入适合的命名空间啊!这么回事???? ReadConfigure 我感觉这个是 自定义方法~~~你仔细看看 有没有C#实现7zip接口的范例 c# 中怎么解决图片被两一个进程占用 C# 字符处理的问题 关于Arraylist与控件 .NET中打印控制 installshield问题,急 vs2008自带报表的line不显示 ASP与SQL链接,Update语句无效 saucer(思归, MS .NET MVP)和各位GGJJDDMM请进,希望开个处方:优秀的邮件列单或好的国外学习站点。 请问如何将int 型 和char型 转换成 string 型 C#调用DLL返回值是乱码!! 我数据库里存放的FCK编辑器编辑的word文档,现在不知道能不能直接从数据库里读word然后显示到aspx页面中
也可以在一个线程中,一个一个读串口数据!
/******************************************************************
* Copyright(c) : Tangxu
* Description :
* CreateDate : 2006-9-01 04:53:08
* Creater : Tang.xu
* LastChangeDate:
* LastChanger :
* Version Info : 1.0.0
* ******************************************************************/
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
/// <summary>
/// free opw
/// </summary>
public void FreeDrv()
{
try
{
if (ss_port != null)
{
ss_port.Close();
}
}
catch
{ }
}
#endregion #region Write command to OPW
/// <summary>
/// 发操作命令给OPW设备
/// 并返回状态
/// </summary>
/// <param name="sCommand"></param>
/// <returns></returns>
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 <= 0)
{
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 <= 0)
{
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
}}