Results 1 to 5 of 5

Thread: [RESOLVED] [Migration] Serial port device is not responding to my VS 2008 code

  1. #1

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Resolved [RESOLVED] [Migration] Serial port device is not responding to my VS 2008 code

    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:
    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
    This is 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
    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.

    Halp!
    0xABADA55D00D

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [Migration] Serial port device is not responding to my VS 2008 code

    Your code assumes that there's going to be data in the received buffer almost immediately after you send your command. Reading the serial port has to be done in a different way. Here's a quick example of serial communication in VB.NET:

    Code:
        Private WithEvents port As New IO.Ports.SerialPort("COM1", 9600, IO.Ports.Parity.None, 8, IO.Ports.StopBits.None)
    
        Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChooseOrder.Click
            If Not port.IsOpen Then port.Open()
            'Send data.
            port.WriteLine("BLAH")
        End Sub
    
        Private Sub Received_Data(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
            'This happens when the port receives data.  Read it then.
            MsgBox(port.ReadLine)
        End Sub
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Talking Re: [Migration] Serial port device is not responding to my VS 2008 code

    Greetings, Jenner, my partner in crime . All is good and the problem has been solved. It doesn't involve the DataReceived event, but you are right there is a delay. Good thing about ReadLine is that it's a blocking call and will keep running until a newline is received. But the real question is: what IS newline? *X-files music plays*

    After I implemented your DataReceived suggestion and found it to not work I went berserk and nuked a small nation. That cooled my head down, then I made a simple program in VB6 which worked and suprised me to no extent. But then while typing it out I noticed the message ends in + Chr(13) which is carriage return. Ofcourse I considered the newline problem before and I tried to Write(w & Chr(13)) in VB 2008 some time ago before giving up and coming here.

    But armed with new knowledge from the MSDN about System.IO.SerialPort, I tried setting SerialPort1.NewLine = Chr(13). It worked. Now the microcontroller returns text, the PROPER text, with a funny square character at the end which I assume to be a Line Feed. The main part of the problem is over. Thanks for the help.
    0xABADA55D00D

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] [Migration] Serial port device is not responding to my VS 2008 code

    No problem. Glad you figured it out! Yea, I had the same problem talking to a Motor Controller on a stepper motor. Took me a day to realize I wasn't sending a newline (vbCrLf) after my commands.

    I confess, I do a similar trick where after I sent the command, I did a DoEvents() loop until it either gave a response or my time-out value was hit.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: [RESOLVED] [Migration] Serial port device is not responding to my VS 2008 code

    Yes DoEvents() is supposed to be primitive and bad but I can't think of anything else to do short of multi-threading. However I don't put DoEvents() in a loop while waiting for a response because who knows the user might click on another read/write command and that leads to race conditions. I put it between read/write commands. So the program pauses while serial communication is taking place, but who cares, serial communication is the ONLY thing it's doing!

    Peace,
    Daarklord
    0xABADA55D00D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width