2013-08-09 2 views
2

를 형성하기 위해 움직일 때 나는 distance tracking laser 내 COM1 포트에 매여 내가 연결을 초기화하기 위해 이러한 설정을 사용하고이 변경 (읽기 유형 두 배의 전역 변수이며, ErrorMessage가 내가 내 폼에 Reading 값을 얻기 위해 원하는 문자열 형식의 전역 변수) 여기에서COM 포트 값은

Private Sub LaserPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles LaserPort.DataReceived 
    ComRecv = True 
    Dim TempRead As String 

    TempRead = LaserPort.ReadExisting() 

    If Not IsNumeric(TempRead) Then 
     If Asc(TempRead) = 13 Then 
      TempRead = "*No Data*" 
     End If 
     ErrorMessage = "Laser Error " & TempRead & "... Please restart application, then turn laser off and back on." 
    Else 
     Reading = ErrorMessage 
    End If 
End Sub 

입니다. 스레드로부터 안전하지 않기 때문에 메서드에서 직접 처리 할 수 ​​없습니다. 그래서 현재 시도한 해결책은 타이머가 Reading의 값을 1 초마다 확인하고 양식에 추가하는 것입니다. 나는이 진드기 방법으로 그렇게한다 :

Private Sub tmrMonitor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMonitor.Tick 
    Dim MeasuredDistance As New clsDimension 
    Dim DesiredDistance As New clsDimension 

    'Check to see if we've got com with the laser so we can alert the user if not 
    If Not ServoCalibrater.ComRecv Then 
     LaserError.Text = "No communication received from the laser. Please check to make sure it's turned on." 
    Else 
     CurrentPosText.Text = Reading 
     Refresh() 
    End If 
End Sub 

위의 코드는 디버거를 단계별로 실행할 때 완벽하게 작동하는 것 같다. 그러나 폼이 디버거없이 표시 될 때 CurrentPosText.Text에 표시된 숫자는 레이저의 예상 값과 완전히 다릅니다.

Putty.exe을 통해 동일한 명령을 실행하여 레이저 값이 올바른지 확인했습니다.

Here은 퍼티의 일관된 결과 및 설정입니다.

TLDR 시계 this video!

enter image description here를 (비디오 링크를 따라 시계)

어떻게 그리고 왜합니까 디버거없이 폼에 표시 할 때 나는 COM 포트 변화로받을 수 ?

+1

사용자가하는 것처럼 ReadExisting()을 사용할 수 없습니다. 이것은 전체 응답을 읽도록 프로그램을 충분히 느리게 할 때만 작동합니다. 디버거를 사용하는 것처럼. 데이터 형식에 관한 세부 사항이 너무 적 으면, 다음에 ReadLine()을 시도하십시오. –

답변

0

한스가 맞습니다. com 포트에서 단일 읽기를 되 돌리는 데 Readline()을 사용해야합니다. ReadExisting은 com 포트 버퍼에있는 모든 것을 제공합니다.