调用函数时如何解决错误端口是关闭的

编程入门 行业动态 更新时间:2024-10-23 11:33:13
本文介绍了调用函数时如何解决错误端口是关闭的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

HI .... 我有2个表格form1& form2。 Form1包含一个串行端口。此端口以表单加载方式打开。

HI.... I have 2 forms form1&form2. Form1 contains a serial port. This port is opened in form load.

private void Form1_Load(object sender, EventArgs e) { serialPort1.Open(); }

我想通过form2写入串口。为此,我在form1中创建了一个用于写入串口的函数。

I want to write to serial port through form2. For that I created a function in form1 for writing to serial port.

public void SerialPortValueUpdated() { byte[] head = new byte[1] { 0xAA }; byte[] trail = new byte[1] { 0x55 }; byte[] llen = new byte[1] { length }; // head = Convert.ToByte(0xAA); //serialPort1.Open(); serialPort1.Write(head, 0, 1); serialPort1.Write(llen, 0, 1); serialPort1.Write(trail, 0, 1); }

并从form2这样调用这个函数就像这样

and called this function from form2 like this

private void button1_Click(object sender, EventArgs e) { Form1 F = new Form1(); F.SerialPortValueUpdated(); }

但是当我调用这个函数时,我收到一个错误,即'Port is closed'。我怎么能解决这个问题。 请帮助我。

But when I calling this function I get an error that 'Port is closed'. How can I solve this. Please help me.

推荐答案

在你的代码中添加一个try-catch子句。 Form1_Load中可能有隐藏错误。 Add a try-catch clause in your code. You might have a hidden error in Form1_Load. private void Form1_Load(object sender, EventArgs e) { try { serialPort1.Open(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }

[更新] 你需要出示表格激活Form_Load。

[Update] You need to show the form to activate Form_Load.

private void button1_Click(object sender, EventArgs e) { Form1 F = new Form1(); F.Show(); // Will show a modless dialog. F.SerialPortValueUpdated(); }

这不是在表单中创建表单的最佳方式。 改为使用后台工作者。 BackgroundWorker类 [ ^ ] 但这取决于你想做什么。 [更新] 对于日志和串行通信的状态,我建议使用 Portmon 来自Sysinternals的[ ^ ] 附加阅读: 使用Microsoft Visual C#Express进行简单串行通信 [ ^ ] 如何使用C#串口通信 [ ^ ] C#中的串口通信 [ ^ ]

This is not the best way to start a form with in a form. Look into using a background worker instead. BackgroundWorker Class[^] But it depends what you want to do. [Update] For logging and status of your serial communication, I recommend to use Portmon[^] from Sysinternals Additional reading: Simple Serial Communication with Microsoft Visual C# Express[^] How To Work With C# Serial Port Communication[^] Serial Port Communication In C#[^]

在 button1_Click 方法中,您创建的是新的 Form1 对象,并在该新对象上调用串行端口方法。但由于该对象未经过表单加载,因此不会包含打开的串行端口。您需要在程序开头创建的原始 Form1 对象上调用该方法。 In your button1_Click method you are creating a new Form1 object, and calling the serial port method on that new object. But since that object has not gone through form loading it will not contain an open serial port. You need to call the method on the original Form1 object created at the start of the program.

更多推荐

调用函数时如何解决错误端口是关闭的

本文发布于:2023-08-07 14:16:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   端口   函数   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!