how to close a serialport used by another program(如何关闭另一个程序使用的串口)
问题描述
我正在编写一个 SerialPort 程序.有时当我想打开 SerialPort 时,会抛出 UnauthorizedAccessException :拒绝访问 COM1.这是因为另一个程序(由其他人编写,我没有它的源代码)正在使用 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:
- 使用时如何用我的代码(尤其是C#代码)关闭COM1由另一个程序?
- 如果无法使用 C# 代码关闭 COM1,如何在 Windows 操作系统中关闭或释放 COM1?
谢谢.
推荐答案
这个 Stack Overflow 问题是第一步:如何在 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 与 x64 与 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.
这篇关于如何关闭另一个程序使用的串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何关闭另一个程序使用的串口
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01