locked
serial communication problem RRS feed

  • Question

  • Hi,

    I am using the following code to send and receive messages to a stepper motor control board. I am going through a virtual COM port at 115200 baud, 8N1 no flow control.  I have installed a port monitor to track outgoing and incoming messages and it tells me that the bytes are being written out, but none are read in. The reply from the boards is supposed to be one byte.  Is there something wrong with this code? 

    Imports System
    Imports System.IO.Ports
    Imports System.Threading

    Public Class Form1
    Shared SerialPort1 As SerialPort
        
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            
    Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
        'Create a new SerialPort object with default settings.
        Dim SerialPort1 As New SerialPort("COM3")
        'Set the read/write timeouts
        SerialPort1.ReadTimeout = 1000
        SerialPort1.WriteTimeout = 100
        SerialPort1.BaudRate = 115200
        SerialPort1.DataBits = 8
        SerialPort1.Parity = 0
        SerialPort1.StopBits = 1

        Dim bytesToSend(7) As Byte
        
    bytesToSend(0) = &H2 'STX
        bytesToSend(1) = &H0 'LEN1"
        bytesToSend(2) = &H0 'LEN2
        bytesToSend(3) = &H3 'MSG
        bytesToSend(4) = &H1 'CMD
        bytesToSend(5) = &H3 'CHK"    
        bytesToSend(6) = &HD 'ETX

        Dim bytesToRead(1) As Byte    
        Dim returnvalue As Integer

        Try
        
    SerialPort1.Open()
        
    If SerialPort1.IsOpen Then
            
    CheckBox1.Checked = True
            SerialPort1.Write(bytesToSend, 0, 7)
            returnvalue = SerialPort1.Read(bytesToRead, 0, 1)
            Label1.Text = returnvalue
        End If

        SerialPort1.Close()

        Catch ex As Exception

        End Try

    SerialPort1.Dispose()
    End Sub
    End
    Class

    Thanks for your help, really appreciate it.

    Monday, January 12, 2009 11:03 PM

Answers

  • Hi

    Try
        ...
    Catch
        'Empty
    End Try

    First do not use an empty catch clause to deal with exceptions. The worst thing about this approach is you can hardly know it when an exception occurs. I would sugggest that you remove the try-catch clause when testing and re-add it later. After removing the try-catch clause, see whether you get any exception.

    Another thing to note is that before you call SerialPort1.Read, check the avaiable bytes to read via SerailPort1.BytesToRead. Make sure SerailPort1.BytesToRead is 1 (or greater that 1) when you expect to read one byte.

    Regards
    Chunsheng Tang


    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    • Marked as answer by warrentang Wednesday, January 21, 2009 11:30 AM
    Thursday, January 15, 2009 4:28 AM