如何关闭另一个程序使用的串口

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:

  1. 使用时如何用我的代码(尤其是C#代码)关闭COM1由另一个程序?
  2. 如果无法使用 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.

这篇关于如何关闭另一个程序使用的串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)