Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Code for write and read com port on Visual Basic?

Options
  • 29-09-2011 9:37pm
    #1
    Registered Users Posts: 33


    I'm writing a VB code at the moment but I seem to keep getting problems. What I need to do is to read from and write to a com port. What I have in the code so far is just for open and close. Anyone have an idea what code should I use for read and write please?? Also the com port number is 2 hence why it's com2.

    Code I have so far is:

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim com2 As IO.Ports.SerialPort = Nothing
    com2 = My.Computer.Ports.OpenSerialPort("Com2")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    Dim com2 As IO.Ports.SerialPort = Nothing

    If com2 IsNot Nothing Then com2.Close()

    End Sub

    End Class


Comments

  • Registered Users Posts: 33 ciaramkm


    Can anyone help me out on this one please :) ??


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    The first link i found on google was this http://www.codeworks.it/net/VBNetRs232.htm

    seems to cover what you want to do.


  • Registered Users Posts: 1,456 ✭✭✭FSL


    Why are you defining your com port twice.

    Where are you going to initialise the settings, assuming they are not the defaults.


    Below is an extract from a VB6 programme which communicated with devices which didn't support handshaking the input was always a fixed length string smaller that the buffer size allocated to the port the output was sent one character at a time the speed of which was controlled by a timer the timer only being enabled when there was anything to output str2 contained the output string, i was initialised as 1 and j set to the length of the string before enabling the timer.

    If handshaking is supported then in VB6 you simply used:-
    stringvariable = comsdevice.Input
    comsdevice.Outpit = stringvariable

    There must be similar commands in VB Net

    Sub setPorts()

    Me.MSComm1.Settings = "9600,n,8,1"
    Me.MSComm1.Handshaking = comNone
    Me.MSComm1.RTSEnable = True
    Me.MSComm1.RThreshold = 1
    Me.MSComm1.SThreshold = 1
    Me.MSComm1.CommPort = 4
    Me.MSComm1.PortOpen = True
    Me.MSComm1.DTREnable = True
    Me.MSComm2.Settings = "9600,n,8,1"
    Me.MSComm2.Handshaking = comNone
    Me.MSComm2.RTSEnable = True
    Me.MSComm2.RThreshold = 372
    Me.MSComm2.InputMode = comInputModeBinary
    Me.MSComm2.InBufferSize = 2048
    Me.MSComm2.SThreshold = 1
    Me.MSComm2.CommPort = 5
    Me.MSComm2.PortOpen = True
    Me.MSComm2.DTREnable = True
    End Sub

    Private Sub MSComm2_OnComm()

    If Me.MSComm2.CommEvent = comEvReceive Then
    buf = MSComm2.Input


    rest of code to process input

    End Sub

    Private Sub Timer1_Timer()


    Me.MSComm1.Output = Mid(str2, i, 1)
    i = i + 1
    If i > j Then
    Me.Timer1.Enabled = False
    str2 = ""
    End If
    End Sub


Advertisement