如何关闭其他程序使用的SerialPort [英] how to close a serialport used by another program

查看:350
本文介绍了如何关闭其他程序使用的SerialPort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个书面方式程序的SerialPort。有时,当我要打开的SerialPort,一个UnauthorizedAccessException被抛出:访问COM1被拒绝。这是beause另一个程序(这是别人写的,我没有它soucecode)正在使用COM1,它退出时该程序不会关闭COM1。
我想问两个问题:

I'm writting a SerialPort program. Sometimes when I want to open the SerialPort, an UnauthorizedAccessException is thrown :access to COM1 is denied. This is beause another program( which is written by someone else and I don't have its soucecode ) is using COM1, and that program doesn't close COM1 when it exits. I want to ask 2 questions:


  1. 如何使用我的代码(特别是C#代码)关闭时,COM1它是由另一程序使用

  2. 如果我无法靠近COM1与C#代码,我怎么能关闭或免费COM1在Windows操作系统?

感谢。

推荐答案

此堆栈溢出问题的第一步:我如何获取打开的文件列表处理由过程在C#中?

This Stack Overflow question is the first step: How do I get the list of open file handles by process in C#?

一旦你有,你可以修改代码有关闭句柄,或者干脆杀具有手柄打开的进程。

Once you have that, you can modify the code there to close the handle, or simply kill the process that has the handle open.

然而,这是非常危险的*,容易出现问题,并需要根据具体的部署(X32 VS 64 VS ARM)加什么'被重新设计。COM1'映射到该特定计算机上

This, however, is very risky*, prone to problems, and will need to be re-engineered based on specific deployments (x32 vs x64 vs ARM) plus what 'COM1' maps to on that particular machine.

在风险:如果串行端口被用来,例如,安装在手机或类似的新固件 - 关闭口可能会导致砌体设备。如果端口被用来传送数据,然后关闭端口可能导致数据丢失。

On Risk: If the serial port is being used to, for instance, install new firmware on a phone or similar - closing the port may result in bricking the device. If the port is being used to transfer data, then closing the port may result in losing data.

祝你好运。但请,如果你破坏你的系统或者数据丢失不要怪我。

Good luck. But please do NOT blame me if you destroy your system or lose data.

这篇关于如何关闭其他程序使用的SerialPort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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