如何做到稳健的SerialPort编程.NET / C#? [英] How to do robust SerialPort programming with .NET / C#?

查看:411
本文介绍了如何做到稳健的SerialPort编程.NET / C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Windows服务用于与串行磁条读取器和中继电路板(访问控制系统)。

I'm writing a Windows Service for communication with a Serial Mag-stripe reader and a relay board (access control system).

我碰到的问题,其中的code停止工作(我得到的IOExceptions)陆续程序中断的过程中打开同一串行端口作为我的服务。

I run into problems where the code stops working (i get IOExceptions) after another program has "interrupted" the process by opening the same serial port as my service.

的code部分如下:

public partial class Service : ServiceBase
{
	Thread threadDoorOpener;
	public Service()
	{
		threadDoorOpener = new Thread(DoorOpener);
	}
	public void DoorOpener()
	{
		while (true)
		{
			SerialPort serialPort = new SerialPort();
			Thread.Sleep(1000);
			string[] ports = SerialPort.GetPortNames();
			serialPort.PortName = "COM1";
			serialPort.BaudRate = 9600;
			serialPort.DataBits = 8;
			serialPort.StopBits = StopBits.One;
			serialPort.Parity = Parity.None;
			if (serialPort.IsOpen) serialPort.Close();
			serialPort.Open();
			serialPort.DtrEnable = true;
			Thread.Sleep(1000);
			serialPort.Close();
		}
	}
	public void DoStart()
	{
		threadDoorOpener.Start();
	}
	public void DoStop()
	{
		threadDoorOpener.Abort();
	}
	protected override void OnStart(string[] args)
	{
		DoStart();
	}
	protected override void OnStop()
	{
		DoStop();
	}
}

我的样本程序成功启动工作线程,和DTR的开/关和提高导致我的磁条读写器上电(等待1秒),关闭(等待1秒)等。

My sample program successfully starts the work-thread, and the opening/closing and raising of DTR causes my Mag-stripe reader to power up (wait 1sec), shut down (wait 1 sec) and so on.

如果我启动超级终端,并连接到同一个COM端口,超级终端告诉我的端口正在使用中。如果我反复美元的超级p $ PSS进入,尝试重新打开 该端口将经过几次重试成功。

If I launch HyperTerminal and connects to the same COM port, HyperTerminal tells me the port is currently in use. If i repeatedly press ENTER in HyperTerminal, to try to reopen the port it will succeed after a few retries.

这已经造成的IOExceptions我的工作线程,预期的效果。然而,即使我关闭超级终端,我仍然会得到相同的IOException的我的工作线程。唯一的治疗实际上是重新启动计算机。

This has the effect of causing IOExceptions in my work-thread, which is expected. However, even if I close down HyperTerminal, i still get the same IOException in my work-thread. The only cure is actually to restart the computer.

其他方案(未使用.NET库端口的访问)似乎在这一点上,通常工作

Other programs (which is not using .NET libraries for port-access) seem to work normally at this point.

任何想法是什么原因造成的?

Any ideas as to what is causing this?

推荐答案

@thomask

是的,超级终端将实际上在SetCommState的DCB,这可以解释为大多数抛出的SerialPort对象的IOExceptions的启用fAbortOnError。某些电脑/手持设备也有对错误标志中止默认打开的UART - 所以它的当务之急是一个串行口的初始化程序中清除(微软忽视了这样做)。我写了一篇长文最近来解释这个更详细(见这个,如果​​你'再有兴趣)。

Yes, Hyperterminal does in fact enable fAbortOnError in SetCommState's DCB, which explains for most of the IOExceptions thrown by the SerialPort object. Some PCs / handhelds also have UARTs that have the abort on error flag turned on by default - so it's imperative that a serial port's init routine clears it (which Microsoft neglected to do). I wrote a long article recently to explain this in greater detail (see this if you're interested).

这篇关于如何做到稳健的SerialPort编程.NET / C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆