저는 벽돌 벽에 있어요!VB.net 2008의 직렬 포트 정보 읽기
나는 직렬 포트를 통해 온도를보고하는 온도 PCB를 가지고있다.
하이퍼 터미널을 열고 원하는 모든 데이터를 수신 할 수 있습니다. 따라서 장치가 작동 중임을 알고 있습니다 ... 그러나받은 데이터를 사용할 수 있도록 VB 응용 프로그램을 만들고 싶습니다. 나는이 프로그램을 실행하면
는이 오류를 얻을 :
System.TimeoutException: The operation has timed out.
at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Ports.SerialPort.InternalRead(Char[] buffer, Int32 offset, Int32 count, Int32 timeout, Boolean countMultiByteCharsAsOne)
at System.IO.Ports.SerialPort.ReadTo(String value)
at System.IO.Ports.SerialPort.ReadLine()
at Temperature.Form1.ReadFromCom() in C:\Documents and Settings\asamuel\Desktop\VB Project Sollutions\Temperature2\Temperature\Form1.vb:line 43
누군가가 나를 도와 주시겠습니까! 나는 미쳐 갈거야! 하이퍼 터미널에서
데이터는 다음과 같이 통해 제공 :
R V1.0 2002-01-06 20:37:37 C
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.00
3 0027.06
4 0027.18
1 0027.00
2 0027.06
내 VB 응용 프로그램 코드는 다음과 같습니다
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim SerialPort1 As New SerialPort
Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
Dim abortThread As Boolean
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text Is "Start Capture" Then
Try
abortThread = False
SerialPort1.Open()
readThread.Start()
Button1.Text = "Stop Capture"
Catch ex As Exception
MsgBox("Another program is already using COM1." & vbCrLf & vbCrLf & _
"Please try again later", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "COM1 Not Available")
End Try
ElseIf Button1.Text Is "Stop Capture" Then
abortThread = True
Button1.Text = "Start Capture"
End If
End Sub
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialPort1
.PortName = "COM1"
.BaudRate = 2400
.Parity = Parity.None
.DataBits = 8
.StopBits = 1
.ReadTimeout = 500
End With
End Sub
Public Sub ReadFromCom()
While abortThread = False
Try
Dim message As String = SerialPort1.ReadLine
updateStatus("Received: " & message)
Catch ex As TimeoutException
updateStatus(ex.ToString)
End Try
End While
End Sub
Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
Public Sub updateStatus(ByVal newStatus As String)
If Me.InvokeRequired Then
Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
Me.Invoke(upbd, New Object() {newStatus})
Else
TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
End If
End Sub
End Class
무엇이 문제입니까? 귀하의 질문에 따르면 모든 것이 잘 진행됩니다. –
Konrad와 동의 하시겠습니까? VB에서 무엇이 발생했는지 또는 일어나지 않았는지 알려주세요. –
Ah - 죄송합니다! 기본적으로 프로그램을 실행하면 시간 초과 오류가 발생합니다. 마치 COM1에서 아무런 정보도 나오지 않는 것처럼 보이지만 ... 나는 그것이라는 것을 압니다! –