Results 1 to 5 of 5

Thread: HELP with System.IO.Ports.SerialPort

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    21

    Exclamation HELP with System.IO.Ports.SerialPort

    Hello,
    I'm using VB 2005 an i want to receive data from my serial RS232 scanner
    in one TextBox
    Please help me with one tutorial or code example, i want to use the serial component from VS 2005

    Thank's foe your time !

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: HELP with System.IO.Ports.SerialPort

    you can have a look at this code

    vb Code:
    1. Imports System
    2. Imports System.IO
    3.  
    4. Public Class ComTester
    5.  
    6.     Private _inpuData As String = String.Empty
    7.  
    8.     'delegate for asynchronous calls
    9.     'Set the property text on the text box control
    10.     Private Delegate Sub SetTextCallBack(ByVal text As String)
    11.  
    12.     Private Sub ComTester_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
    13.         If Not Me.SerialPort1 Is Nothing AndAlso Me.SerialPort1.IsOpen Then
    14.             Me.SerialPort1.Close()
    15.             Me.SerialPort1.Dispose()
    16.         End If
    17.     End Sub
    18.  
    19.     Private Sub ComTester_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.         For Each sp As String In System.IO.Ports.SerialPort.GetPortNames
    21.             Me.ComboBoxPorts.Items.Add(sp)
    22.         Next sp
    23.  
    24.         Me.SerialPort1 = Nothing
    25.         Me.PropertyGridPorts.SelectedObject = Me.SerialPort1
    26.  
    27.         Me.TabControl1.SelectedTab = Me.TabPageSetup
    28.     End Sub
    29.  
    30. #Region "Tab Setup"
    31.     Private Sub ButtonConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnect.Click
    32.         If Me.ComboBoxPorts.SelectedItem Is Nothing Then
    33.             MessageBox.Show("The port name can't be empty.", "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    34.             Me.ComboBoxPorts.Focus()
    35.             Exit Sub
    36.         End If
    37.  
    38.         Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.ComboBoxPorts.SelectedItem.ToString())
    39.         Try
    40.             If Me.SerialPort1.IsOpen Then Me.SerialPort1.Close()
    41.             Me.SerialPort1.Open()
    42.         Catch ex As Exception
    43.             MessageBox.Show("Error during the opening of the port " & Me.SerialPort1.PortName & "." & Environment.NewLine & ex.Message, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    44.             Exit Sub
    45.         End Try
    46.  
    47.         MessageBox.Show("Com port " & Me.SerialPort1.PortName & " successfully opened.", "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Information)
    48.     End Sub
    49.  
    50.     Private Sub ComboBoxPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxPorts.SelectedIndexChanged
    51.         If Not Me.ComboBoxPorts.SelectedItem Is Nothing Then _
    52.                                                                             Me.SerialPort1 = New System.IO.Ports.SerialPort(Me.ComboBoxPorts.SelectedItem.ToString())
    53.         Me.PropertyGridPorts.SelectedObject = Me.SerialPort1
    54.     End Sub
    55. #End Region
    56.  
    57. #Region "Tab Sending"
    58.     Private Sub Send(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonSendText.Click
    59.         Dim pressedButton As Button = CType(sender, Button)
    60.  
    61.         Dim value As Integer
    62.  
    63.         If Not Me.TextBoxText.Text.Trim.Equals("+") AndAlso Not Me.TextBoxText.Text.Trim.Equals("-") Then
    64.             If Not Integer.TryParse(Me.TextBoxText.Text.Trim, value) Then
    65.                 MessageBox.Show("Invalid input")
    66.                 Exit Sub
    67.             End If
    68.  
    69.             If value < 0 OrElse value > 400 Then
    70.                 MessageBox.Show("Out of bounds input")
    71.                 Exit Sub
    72.             End If
    73.         End If
    74.  
    75.         Select Case pressedButton.Name
    76.             Case Me.ButtonSendText.Name
    77.                 Try
    78.                     Me.SerialPort1.WriteLine(Me.TextBoxText.Text.Trim & " ")
    79.                 Catch ex As Exception
    80.                     MessageBox.Show("Error while sending text to the port." & Environment.NewLine & ex.Message, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    81.                 End Try
    82.         End Select
    83.     End Sub
    84. #End Region
    85.  
    86.     Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    87.         Me._inpuData = Me.SerialPort1.ReadExisting()
    88.  
    89.         If Not Me._inpuData.Equals(String.Empty) Then Me.SetText(Me._inpuData)
    90.     End Sub
    91.  
    92.     Private Sub SetText(ByVal text As String)
    93.         ' InvokeRequired required compares the thread ID of the
    94.         ' calling thread to the thread ID of the creating thread.
    95.         ' If these threads are different, it returns true.
    96.         If Me.TextBoxData.InvokeRequired Then
    97.             Dim d As SetTextCallBack = New SetTextCallBack(AddressOf SetText)
    98.             Me.Invoke(d, New Object() {text})
    99.         Else
    100.             Me.TextBoxData.Text &= text
    101.         End If
    102.     End Sub
    103.  
    104.     Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
    105.         Dim response As DialogResult = Me.SaveFileDialog1.ShowDialog()
    106.         Try
    107.             If response = Windows.Forms.DialogResult.OK Then
    108.                 System.IO.File.WriteAllText(Me.SaveFileDialog1.FileName, Me.TextBoxData.Text)
    109.                 MessageBox.Show("Data saved in " & Me.SaveFileDialog1.FileName, "Serial communication tester", MessageBoxButtons.OK, MessageBoxIcon.Information)
    110.             End If
    111.         Catch ex As Exception
    112.             MessageBox.Show("Incorrect file name.", "Serial communication", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    113.         End Try
    114.     End Sub
    115.  
    116.     Private Sub cmdClearText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearText.Click
    117.         Me.TextBoxData.Clear()
    118.     End Sub
    119. End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    21

    Re: HELP with System.IO.Ports.SerialPort

    Sorry, for this time, i am a beginer in work with serial ports
    please tell me in steps and very easy level for me :

    i have one text box and one Serial scanner ( barcode reader Symbol LS2208 )
    and i want to scan one barcode and the result to apear in my text box
    I try to use a System.IO.Ports.SerialPort component but i cant reach to find the solution

    Thank you again for your time

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: HELP with System.IO.Ports.SerialPort

    By the way, you posted the same thread twice.

    You don't have to copy the code, but use it as a guide.
    The logic starts finding all the available port (form_Load), next it is about the connection to the selected port(Setup region) and finally the actual communication(Sending Region)

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: HELP with System.IO.Ports.SerialPort

    You have two threads going about the same topic I think.

    It would be helpful if you post the code (using the code tags), with the specific error or problem you are having. "i cant reach to find the solution" is hard to diagnose.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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