读取C#.net中串行端口的数据 [英] Read data in Serial Port in C#.net

查看:71
本文介绍了读取C#.net中串行端口的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



这是我基于串口的新任务。



这对我来说是新的,所以请帮助解决我的问题。



如何从串口读取并通过C#.net写入串口?



我试过了,但我没有得到正确的结果。

我给我的代码。

请验证并给我一个解决方案。



在我的表格中,我有2个按钮和1个文本框。

我的代码:

在我的命名空间中:

Hi friends,

This is my new task based on "serial Port".

It's new for me, so Please help to solve my Problem.

How do I read from Serial port and write to serial port via C#.net?

I tried it, but I didn't get proper result.
I give a my code.
Please verify and give me a solution.

In my form, I have 2 buttons and 1 text Box.
My Code:
In My Namespace:

using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Resources;
Declaration of Serial Port
  public static SerialPort sp;
in Form Load event:
sp = new SerialPort();
         sp.PortName = "COM5"
         sp.Encoding = Encoding.ASCII;
         sp.BaudRate = 9600;
         sp.DataBits = 8;
         sp.StopBits = StopBits.One;
         sp.Parity = Parity.None;
         sp.ReadBufferSize = 4096;
         sp.NewLine = "\r\n";
         sp.Handshake = Handshake.None;
         sp.ReceivedBytesThreshold = 100000;





当我点击写入按钮时:



When I click write button:

try
       {
           sp.Open();
           sp.WriteLine(txtdata.Text);
           txtdata.Text = "";
           sp.Close();
       }
       catch (Exception ex)
       {
       }



当我点击阅读按钮时:


When I click Read Button:

sp.Open();
sp.ReadTimeout = 500;
txtdata.Text ="";
String message = sp.ReadLine();
txtdata.Text = message;
sp.Close();





问题是阅读按钮。

我没有得到结果(我在文本框中写的内容。

请帮忙,如何解决这个问题?



谢谢和问候,

Lakshmi Narayanan.S



Problem is "Read Button".
I didn't get a result(What I wrote in the textbox).
Please Help, How to fix this Problem?

Thanks and Regards,
Lakshmi Narayanan.S

推荐答案

当然如果不能这样工作的话。您可以从按钮事件处理程序将数据发送到串行端口,但不能以这种方式读取。这是阻止操作。



你需要在一个单独的线程中一直读取。当数据到来时,它会读取它;你需要动态解释数据。



请参阅我过去使用串口线程的解决方案:

WPF应用程序中的串口 [ ^ ]。



这是关于WPF但仍适用于您的情况。只有UI操作的调用有点不同。通常,您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



要对UI线程以外的线程使用类似的调用机制,请参阅我的提示/技巧文章,其中包含完整的源代码和用法示例:简单的阻塞队列,用于线程通信和线程间的Invoca [ ^ ]。



-SA
Of course if won't work this way. You can send data to serial port from the button event handler, but you cannot read this way. This is blocking operation.

You need to read all the time in a separate thread. When data comes, it will read it; you need to interpret data on the fly.

Please see my past solution on using thread with the serial port:
Serial Port in WPF Application[^].

It was about WPF but still applicable to your case. Only the invocation of the UI operation is a bit different. Generally, you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

To use the similar invocation mechanism to the thread other than UI thread, see my Tips/Tricks article with full source code and usage samples: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


请参阅Microsoft



如何访问串行和并行端口 [ ^ ]



谢谢。
See at Microsoft

How to access serial and parallel ports[^]

Thanks.


请尝试以下链接:





NET 2.0中串行端口上的通信 [ ^ ]





使用C#和Whidbey的串行通信 [ ^ ]



链接 [ ^ ]



此外,当您将数据写入SerialPort时,之后不要关闭SerialPort。试试这个,我认为这会对你有帮助。
please try the link given below:


Communication on a serial port in NET 2.0[^]


Serial Communication using C# and Whidbey[^]

Link[^]

Also when you are writing data into SerialPort, after that do not close the SerialPort. Try this i think this will help you.


这篇关于读取C#.net中串行端口的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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