开发者

Reading from a serial port manually using VB.net

I need to connect to an AND GF 3000 e-scale using RS232. I've tested the connection using HyperT and AND's own program and it's working. Now I'm creating a VB app to read the thing and so far it's actually working. However it's quite buggy in several parts so I want to optimize the thing.

My previous read command uses:

Private Sub mscport_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles mscport.DataReceived
    Dim tmpBuf As String
    tmpBuf = mscport.ReadLine
End Sub

which is not the full version of what I'm using but otherwise works. However, exiting and reconnecting (I'm using Windows Form Controls) turns up that "The I/O operation has been aborted because of either a thread exit or an application request" exception. Looking around the 'net I found the reason so from Dick Grier over here: http://www.pcreview.co.uk/forums/serial-port-error-o-operation-has-been-abortedbecause-either-thread-exit-application-request-t2781073.html:

What this means, almost certainly, is that the SerialPort object attempted to complete the call to ReadLine after the port has been closed. This can happen because of the lack of synchronization between UI events which may cause the port to close, and the background thread in the SerialPort object that is performing the actual ReadFile operation (this executes as a result of ReadLine in your delegate).

The problem with ReadLine, and the reason that I DO NOT use it, is that it blocks until the the line terminating condition occurs -- this may be well AFTER you have closed the port. Thus the exception.

I prefer simply buffering my own data in a Static or class-level variable (all ReadExisting and append new data to the buffer) , and testing that buffer for the vbCrLf terminating characters. If the vbCrLf is found (InStr or Substring, your choice), then call a delegate to process and display the data in the buffer. Remember to clear this buffer AFTER you have processed and displayed its content. If you do this, the exception should be resolved.

Dick

Previously my app uses ReadExisting instead of ReadLine for serial-serial connection. Later, when using USB-serial cable, ReadExisting didn't work so I used ReadLine instead. I want to use the USB cable so I need to find a way to replace ReadLine. Now I'm no great shakes at serial ports but I've managed to make an almost working code replacing the ReadLine using ReadChar which is here:

Private Sub mscport_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles mscport.DataReceived

    Dim tmpbuf As String
    Dim bytebuffer(17) As Byte
    Dim count As Integer = 17
    Try
        While count > 0
            bytebuffer(17 - count) = mscport.ReadChar
            'If bytebuffer(17 - count) = vbCrLf Then

            'End If
            'tmpBuf = tmpBuf & mscport.ReadExisting
         开发者_JS百科   count = count - 1
        End While

    Catch ex As InvalidOperationException
        MessageBox.Show(ex.Message)
    Catch ex As UnauthorizedAccessException
        MessageBox.Show(ex.Message)
    Catch ex As System.IO.IOException
        MessageBox.Show(ex.Message)

    End Try
    tmpbuf = tmpbuf & System.Text.Encoding.ASCII.GetString(bytebuffer, 0, 17)
    'tmpBuf = bytebuffer.ToString()
    ReceiveData(tmpbuf)
End Sub

The problem with the new code:

  1. The IO exception is still there. Sometimes it triggers when opening the app. Even with all that exceptions it still didn't catch.

  2. The Data sometimes get received all jumbled up.For example, ST 0009.80 g is displayed as .80 gST 0009. The data ends with a CrLf so I'm still thinkking on how to rearrange it before displaying.

I know there's a better way to do this, I just couldn't think of one, or maybe I'm not searching enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜