急需的示例代码 [英] sample code needed very urgently

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

问题描述

我想要一个实现通过串行端口从硬件接收十六进制数据的代码,这里是我编写的代码

i want a code which implements receiving of hex data from the hardware through serial port here is a code which i have writen

Dim strData As String
       Try
           strData = serialport.ReadTo(Chr(&HA))
           If (strData.Length > 0) Then
               MsgBox("Drive returned data before initial LF:" & _
                   vbNewLine & strData) 'This should be empty. always.
           End If
       Catch ex As Exception
           MsgBox("Coud not read to initial LF: " & vbNewLine & ex.Message)
       End Try

       ' Now read and trim the CR LF from the remaining data.
       strData = serialport.ReadExisting()
       If (strData.Length > 0) Then
           Try
               strData = strData.Substring(0, strData.Length - 2)
           Catch ex As Exception
               MsgBox("Serial data out of sync." & vbNewLine & vbNewLine _
                   & strData)
           End Try
       End If

       Return strData



一旦调试了readline()或read(),整个程序就会被挂起并直接终止.
请帮助解决代码中的问题.如果要更改此代码,我应该执行什么操作以从硬件接收数据.



as soon as readline() or read() is being debuged the whole program is getng hanged and am terminating it directly.
plz help whats the problem in code.if this is to be changed what shud i implement to receive data from hardware

推荐答案

您在做什么? MessageBox是一个阻止呼叫,如何将其与通讯混在一起?通常,通信API也基于某些阻塞调用,因此在实际代码中,它们可以在单独的线程中执行.要开始开发,请从一个简单的控制台应用程序开始.

第二件事是:没有任何东西真正挂起.您的读取操作是一个阻止呼叫.完成后,ReadTo取决于RS-232电缆另一端的情况.谁知道你在那里有什么?

—SA
What are you doing? MessageBox is a blocking call, how can you mix it with communications? Generally, the communication APIs are also based on some blocking calls, so in a real-life code they can be executed in a separate thread. To start development, start with a simple console application.

The second thing is: nothing really hangs. Your read operation is a blocking call. When you finish your ReadTo depends on what happens on the opposite end of your RS-232 cable. Who knows what do you have there?

—SA


这篇关于急需的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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