在 C# 或 python 上写入 COM 端口
我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它与权限无关,也与正在使用的端口无关。我发现了相关问题 上面说是因为我安装的版本(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.
一个串行端口不能同时有多个连接。该端口可能正在使用中。
You cannot have multiple simultaneous connections to a serial port. The port is likely in use.
试试这个:
右键单击您的可执行文件并选择“以管理员身份运行”。 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.