I'm migrating a program from VB 6 to VS 2008. The program communicates with a microcontroller through a serial port... or at least, it's SUPPOSED to. DUN DUN DUN. Though I tried to keep the communication as similar to the VB6 version (which I didn't write) as possible, the microcontroller is not replying as it should.
This is the old code that works:
This is the new code:Code:Function SendInstruction(w As String) As String buffer$ = "" buffera$ = "" MSComm1.InputLen = 0 MSComm1.Output = w & Chr$(13) Text3 = CStr(w) x = timeGetTime If Port > 2 Then delay (1) Timeout = False Do DoEvents signal$ = MSComm1.Input: buffer$ = buffer$ + signal$ If signal$ = "" Then signal$ = Chr$(0) If (timeGetTime > (x + 200)) Then Timeout = True Loop Until Timeout Or (Asc(Right$(signal$, 1)) = 13) Debug.Print timeGetTime - x Text1 = buffer$ If Left$(buffer$, 1) = "?" Then: End Function
The reply goes into buffer. I send the same argument w in both cases, but the new code just times out a the SerialPort1.ReadLine. I'm pretty sure the port settings (port name, parity, baud rate, data bits, and stopbits) are the same too. Anything I might have missed? Also, what is the InputLen property and where did it go in VB 2008? Though I don't think that's the problem since it should just default to 0 in the new code.Code:Sub SendInstruction(ByVal w As String) My.Application.DoEvents() If SerialPort1.IsOpen Then Try 'Send a command to the current COM port Dim reply As String buffer = "" 'clear buffer for more reply SerialPort1.WriteLine(w) 'Send instruction. TextBoxInstruction.Text = w 'CStr(w) 'display instruction in the instruction textbox 'Read answer from the serial connection. reply = SerialPort1.ReadLine buffer = buffer & reply 'store reply TextBoxReply.Text = buffer 'display reply in the reply textbox Catch e As System.InvalidOperationException If e.Message = "The port is closed." Then buffer = "<Disconnected.>" Else Throw e End If Catch e As TimeoutException 'A timeout will cause the command to be ignored buffer = "<Timed out.>" TextBoxReply.Text = buffer End Try Else buffer = "<Disconnected.>" End If End Sub
Halp!




Reply With Quote