串口线程 [英] serial port threading

查看:58
本文介绍了串口线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在制作一个与pic微控制器配合使用的计数器。我已经做了计数器,它的工作原理,但我通过串口与pic微控制器给出的脉冲是非常快的计数器不计...我想在计数器的帮助不想错过任何脉冲.. plz帮助。



我的代码如下: -



hello
i am making a counter which works with pic micro controller. i have made counter and it works but the pulses i gave to counter through serial port with pic micro controller is very fast the counter doesn't count... i want help in counter do not want to miss any of the pulse.. plz help.

my code is following:-

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class Form1
    Delegate Sub SetTextCallback(ByVal [text] As Char)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "COM50"
        SerialPort1.BaudRate = "2600"
        SerialPort1.Open()
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As Char)
        If Me.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)

            Me.Invoke(x, New Object() {(Text)})
        Else

            If Text = "A" Then
                Label1.Text = Label1.Text + 1
                Label1.Refresh()
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label1.Text = "0000"
    End Sub
End Class





请帮助

谢谢



please help
Thanks

推荐答案

First of all,在单独的线程中执行所有串口活动。另外,使用 BeginInvoke ,而不是调用。为了解一些,请看我过去的答案:

(搞砸了,请看下面),

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



(对不起,我们遇到了CodeProject链接帖子的问题。我的第一个链接搞砸了。请使用:

http://www.codeproject.com/Answers/159125/Control-Invoke-vs- Control-BeginInvoke#answer1。

很抱歉给您带来不便。



我不知道所有细节。功能始终取决于RS-232电缆另一端的功能。在某些设备中,控制没有很好地实现,并且数据可能在另一方丢失:如果您没有及时发送确认,延迟从端口读取或类似的事情,则消息可能会丢失。如果发生这种情况,请尝试使用非常快速的应用程序,无需UI。您甚至可以提升线程/进程优先级并使用线程关联将您的串行通信线程专用于固定的CPU代码。如果你这样做并发现丢失信息率的任何差异,我的猜测是正确的。如果没有,那么在某些逻辑中存在一个缺陷,无论是在您身边,还是在连接到串行端口的设备侧面。



- SA
First of all, do all the serial port activity in a separate thread. Also, use BeginInvoke, instead Invoke. For some understanding, please see my past answers:
(screwed up, please see below),
Problem with Treeview Scanner And MD5[^].

(Sorry, we are experiencing a problem with CodeProject link postings. My first link was screwed up. Please use:
"http://www.codeproject.com/Answers/159125/Control-Invoke-vs-Control-BeginInvoke#answer1".
Sorry for the inconvenience.)

I don't know all the detail. Functionality always depends on what you have on the other end of your RS-232 cable. In some devices, the control is not well implemented, and the data can be lost on the other side: the messages could be lost if you don't send timely confirmation, delay reading from the port or something like that. If that happens, experiment with a very fast application, without UI. You can even boost thread/process priority and use thread affinity to dedicate your serial communication thread to a fixed CPU code. If you do that and spot any difference in the rate of lost messages, my guess is correct. If not, there is a flaw in some logic, again, either on your side, or the side of the device connected to a serial port.

—SA


作为Sergey回答的附录,你通常不会在PC端实现计数器:如果脉冲率很高,那么PC应用程序就无法使用跟进。

解决方案是对微控制器端的脉冲进行计数,并以合理的速率将当前计数发送到PC应用程序。
As an addendum to Sergey answer, you usually don't implement the counter on the PC side: if the pulse rate is high there's no way for the PC application to keep up.
The solution is counting pulses on the microcontroller side and send at 'a reasonable rate' the current count to the PC application.


这篇关于串口线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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