与线程作战...... [英] Battling with Threads...

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

问题描述

大家好,



我通过串口向仪器发送命令然后取回数据

当我跑发送命令的代码并获得回复(到富文本框)。但是,当串口通过invoke命令传送到rtb时,如果我通过调试器运行命令工作,并不是所有细节都存在,那么数据就会得到,但是如果我从桌面运行.exe而不是所有的数据都在那里,它就会崩溃。我认为这可能是由于系统运行得更快而没有调试器附件。我需要一种减慢线程的方法,不使用线程休眠(将线程取出!),我尝试使用线程休眠,它在调试器中一直有效,在exe中只有一些时间。



对不起,在正确阅读之前点击其他内容丢了



Hi All,

I am sending commands to an instrument via a serial port and getting back the data however
when I run the code the commands are sent and the reply is got (to a rich text box). How ever as the serial port is piped to the rtb via an invoke command not all the details are there every time if I run it through the debugger the commands work, the data is got, however if I run the .exe from the desktop not all the data is there and it falls over. I think it might be due to the system running quicker with out the debugger attachment. I need a way of slowing the threads not using a thread sleep (which takes the thread out!), I have tried using a Thread Sleep which works all the time in the debugger and only some times in the exe.

Sorry got lost in something else clicked post before reading properly

'MsgBox(rtbIncoming.Text)
     Data_Read = rtbIncoming.Text
     MsgBox("Data :" + Data_Read)



执行读数的实际例程如下:


The actual routine that does the reading is here:

Private Sub port_DataReceived_1(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

       NoDataAtPort.Enabled = False
       InputData = myComPort.ReadExisting
       Reply_Status = REPLY.YES_REPLY

       If InputData <> [String].Empty Then
           Me.Invoke(New SetTextCallback(AddressOf SetText), New Object() {InputData})
           '    MsgBox("here!")
           DataReceived = True
       Else
           MsgBox("null")
       End If

       TmrNoDataAtPort.Enabled = False
       If (Reply_Status = REPLY.TIMEOUT_REPLY) Then
           Data_Back = "TIMEOUT"
       ElseIf (Reply_Status = REPLY.YES_REPLY) Then
           NoDataAtPort.Enabled = False
       End If

       If (DataReceived = True) Then Me.Invoke(New PutInRightTextBox(AddressOf DecphierCommand), New Object() {InputData})

   End Sub





DecphierCommand()只是一个案例陈述来拆分rtb输入数据。





很抱歉被抓到的东西必须要做...



格伦



DecphierCommand() is just a case statement to split the rtbIncoming data up.


Sorry about that got caught with something had to do it...

Glenn

推荐答案

我这样做的方法是有一个单独的处理线程,它将原始串行数据转换成完整的消息,然后将它们传递给主代码进行实际分析。

因此,如果数据是STX数据EXT然后是校验和,处理线程将保持数据到目前为止从STX开始,直到找到ETX,抓住下一个作为校验和,验证它,并丢弃或传递整个消息开启。



添加睡眠等等通常是一个错误 - 它很难确定它在所有情况下都能正常工作并且很容易让它变得容易在外部发生变化的六个月时间内出现问题 - 可能是在运行您的应用程序的PC上添加或删除了繁重的处理任务,或者PC升级了更多内存。



我不知道你的数据是什么看起来像,但大多数都有某种开始和结束的消息序列,即使它只是它以换行符结束。
The way I do it is to have a separate "processing" thread which converts "raw" serial data into complete "messages" and then passes them to the main code for actual analysis.
So if the data is STX data EXT then a checksum, the processing thread will keep the "data so far" starting at the STX until it finds the ETX, grab the next as the checksum, validate it, and either discard or pass the whole message on up.

Adding Sleep and so forth is generally a mistake - it makes it very hard to be sure it works under all circumstances and it's far too easy to have a problem appear in six months time when something external has changed - perhaps a heavy processing task was added or removed from the PC running your app as well, or the PC was upgraded with more RAM.

I don't know what your data looks like, but mostly there is some kind of start and or end message sequence, even if it's only "it ends with newline".


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

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