C#中关于串口的监听

isreal0 2009-03-19 04:36:53
我希望在c#代码中实现对一个串口的监听,下载了codeproject一段程序,他利用了port控件,设置了portName,然后openPort以后就可以实现收发串口数据。
但是我现在希望对计算机中已经使用的一个串口进行监听,这个com口上已经连接了一个外部设备,并实时传输数据到本计算机中。请问在监听是是否需要再调用port的open方法将其打开?或者只要在设置好portName然后在port的dataReceived中写入内容就会被自动相应到?
期待各位的解答,非常感谢。
...全文
1442 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
串口发送16进制的数据?
zggxyxlft2004 2010-07-22
  • 打赏
  • 举报
回复
只是为了实现监听而已?!?!
很简单的一个问题。
有需要来问我。
哈哈潜伏哥 2009-03-28
  • 打赏
  • 举报
回复
这个是用于wince下的串口驱动源码,可以实现你要求,windows上可能对你有帮助。
你去看看。

http://www.codeproject.com/KB/mobile/DemoDriver.aspx?fid=276824&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51
isreal0 2009-03-26
  • 打赏
  • 举报
回复
再顶下
nkthinker 2009-03-22
  • 打赏
  • 举报
回复
没有完美的答案。。涉及底层的东西。想监听用一些监听的软件能实现。
isreal0 2009-03-22
  • 打赏
  • 举报
回复
- -
isreal0 2009-03-22
  • 打赏
  • 举报
回复
期待一个完美的答案哈。。
cstester 2009-03-21
  • 打赏
  • 举报
回复
using System ;
namespace Jackson.Life
{
public class YoJacksonAge
{
public static void Go()
{
double myAge =0;
double now=18.7;
double mylifespan = 80;
if(MyAge==now){
Console.WriteLine("I went the 1/4 for MyLife already!");
Console.WriteLine("I have no money and no house");
Console.WriteLine("I have no wife and no son");
}else if(MyAge==25){
Console.WriteLine("I went the about 1/5 for MyLife already!");
Console.WriteLine("I have a little money and a little house");
Console.WriteLine("I have wife and get Jack this year");
}}else if(MyAge==40){
Console.WriteLine("I went the 3/8 for MyLife already!");
Console.WriteLine("I have 5000k money and a big house");
Console.WriteLine("I love my wife and a son,his age is 10");
}}else if(MyAge==60){
Console.WriteLine("I went the 3/4 for MyLife already!");
Console.WriteLine("I have some E money and a big house");
Console.WriteLine("I have not to much time alreadey");
Console.WriteLine("I love my home ");
}else{
Console.WriteLine("I will be dead!");
Console.WriteLine("the end!");
Application.Exit();
}
}
}
}

程序运行调用该方法后,发生这样一个中文经典异常:
-------------------------------------------------------------------
Execption:Error(909):jackson is deathless.
异常:错误代号(909):江哥是不死的.

------------------------------------------------------------
我百思不得其解。。
很多干了一辈子的死鬼程序员,
托梦的时候告诉我: 干了 一辈子 都没遇到过这异常。。
jscn123789abc 2009-03-21
  • 打赏
  • 举报
回复

不明白串口为什么不可以共享?
rl0516 2009-03-21
  • 打赏
  • 举报
回复
监视已经打开的串口,难度非常大,要基于驱动一级的编写。

要不试一下虚拟串口软件
昵称为空 2009-03-21
  • 打赏
  • 举报
回复
有个麻烦的方法,就是从你刷卡器上面接出来一个串口
用新接的串口连接到计算机的另一个串口
昵称为空 2009-03-21
  • 打赏
  • 举报
回复
如果串口已经被别的程序打开了,你就无法使用了,串口好像是独占的。
必须要做硬件底层一级的基于驱动级开发,把串口接收的数据“复制”过来
pzlin 2009-03-21
  • 打赏
  • 举报
回复
监听的方法是..port.open = true后

实现dataReceived接受事件 . 并从中读取数据

换一个串口吧.或者把那个程序关掉.

两个程序不能共用一个串口的 . 一般一台台式机上只有一个串口
isreal0 2009-03-21
  • 打赏
  • 举报
回复
多谢dunao兄弟的回复。您的代码中也有port.open方法,但是我的串口已经被其他程序使用了,没有办法再open了哦
CraxyMouse 2009-03-21
  • 打赏
  • 举报
回复
/******************************************************************
* 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
}

}
isreal0 2009-03-21
  • 打赏
  • 举报
回复
期待哪位兄弟给一些具体的代码。。。
andy_1027 2009-03-20
  • 打赏
  • 举报
回复
SerialPort这个类只能进行简单的串口读写,并且是在串口没有被占用的情况下。
所以不能用SerialPort类实现你这个应用。
可以找其它方法。
isreal0 2009-03-20
  • 打赏
  • 举报
回复
期待高手出现。。
isreal0 2009-03-19
  • 打赏
  • 举报
回复
dragonforfly兄弟请问可以给一些C#添加钩子监听串口的材料吗?
Alden 2009-03-19
  • 打赏
  • 举报
回复
应该可以添加一个钩子,进行消息的复制。
加载更多回复(6)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧