在 C# 或 python 上写入 COM 端口

发布于 2024-09-18 05:11:34 字数 692 浏览 3 评论 0原文

我尝试使用 Visual Studio C# 将字符串写入 COM4(这是一个 USB 串行适配器),并收到以下错误:

对端口“COM4”的访问被拒绝。

我的程序非常简单:

serialport.Open();
serialport.WriteLine("test");
serialport.Close();

我还尝试将 pyserial 与 python 一起使用,并得到:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ser.write("string")
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 255, in write
    raise SerialException("WriteFile failed (%s)" % ctypes.WinError())
SerialException: WriteFile failed ([Error 6] The handle is invalid.)

我知道这个问题与编程无关,可能是 Windows 7 的问题,但我希望有人已经遇到了类似的问题。

im trying to write a string to a COM4 which is a usb-serial adapter using Visual Studio C# and got the following error:

Access to the port 'COM4' is denied.

My program is extremely simple:

serialport.Open();
serialport.WriteLine("test");
serialport.Close();

I also tried using pyserial with python and got:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ser.write("string")
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 255, in write
    raise SerialException("WriteFile failed (%s)" % ctypes.WinError())
SerialException: WriteFile failed ([Error 6] The handle is invalid.)

I know this question isn't really related to programming and is probably a Windows 7 thing but I was hoping someone has already had a similar problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我闹别瞎闹 2024-09-25 05:11:37

它与权限无关,也与正在使用的端口无关。我发现了相关问题 上面说是因为我安装的版本(64位Python和32位pyserial)我觉得很傻。

我仍然不明白为什么它在 C# 上也不起作用,但没关系。我只会使用Python。

It had nothing to do with permissions neither with the port being in use. I found a related question which said it was because of the version I had installed (64 bit Python and 32 bit pyserial) I feel so silly.

What I still can't figure out is why it also didn't work on C# but that's OK. I'll just use python.

浊酒尽余欢 2024-09-25 05:11:36

一个串行端口不能同时有多个连接。该端口可能正在使用中。

You cannot have multiple simultaneous connections to a serial port. The port is likely in use.

情深如许 2024-09-25 05:11:35

试试这个:

右键单击您的可执行文件并选择“以管理员身份运行”。 Windows 7 默认情况下运行没有管理员权限的程序。我敢打赌,写入 COM 端口的能力需要以管理员身份运行程序。

Try this:

Right click on your executable and choose "run as administrator". Windows 7 by default runs programs without admin privileges. I'm betting that the ability to write to a COM port requires running a program as an Administrator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文