我正在尝试从串行端口读取 [英] i am trying to read from a serial port

查看:81
本文介绍了我正在尝试从串行端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从串行端口读取.
我正在使用此代码:

i am trying to read from a serial port.
i am using this code :

Char[] f=new Char[1000];
SerialPort Port = new SerialPort("COM1", 9600, Parity.None, 8,StopBits.One);
Port.Open();
Port.Read(f, 0, 1000);
MessageBox.Show( f.ToString());
Port.Close()



但结果是一个错误:
拒绝访问端口"COM1".

问题出在哪里?

当我将COM1更改为COM2时,错误消失但没有任何动作.

我正在form_Keypress事件中尝试此代码.

我在等你的帮助
谢谢



but the result is an error :
Access to the port ''COM1'' is denied.

where is the problem?

when i change COM1 to COM2 error go away but no action occur.

i am trying this code in form_Keypress event.

i am waiting for your help
thank you

推荐答案

COM1可能正在被另一个进程使用,因此您的进程已被锁定.
It is likely that COM1 is in use by another process, so your process is locked out.


如果您可以查看 SerialPort.Open [ ^ ],您会看到当其他尝试使用您要打开的端口时返回此错误.

所以这是第一个问题.使用该端口的机器上还有其他东西吗?

您代码的其余部分似乎正在尝试从应用程序打开的端口读取,但是没有任何反应.有什么东西在向那个端口发送数据吗?可能不是,这也许可以解释为什么什么也没发生.

从MSDN尝试示例程序 [
If you look at the documentation for SerialPort.Open[^], you will see that this error is returned when something else is using the port you are trying to open.

So that is the first question. Is something else on your machine using that port?

The rest of your code seems to be trying to read from the port that is opened by your application but nothing happens. Is anything sending data to that port? Probably not, which might explain why nothing happens.

Try the example program from MSDN[^] (scroll down a little and you will find it.).


这篇关于我正在尝试从串行端口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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