[VB2005]Q of Serialport ----> Need Help

        I jz start to learn the programing of vb2005, but there is a new plugin of serialport, i reali dun no how to use it, who can tell me how to write the code to achieve a simple network connection, like chating, transfer the text

 

form1 include: textbox1,button1

form2 ~~~~~: textbox2.button2

 

how to make tt when i click the button1, the content of textbox1 will be sent to textbox2

 (use network ee.. not form2.textbox2.text = textbox1.text ....)

hope can reply ^-^ thx veri much!




Answer this question

[VB2005]Q of Serialport ----> Need Help

  • Michiel de Bruijn

    Do you want to specifically use the serial port or just create a means for two programs to communicate over a network.

    If you want to get two programs communicating over a network -then you should look at something called .NET Remoting. A web search on this will reveal a lot of information.

    If you want to use the serial port specifically .NET has a class specifically for the serial port. This has some examples.

    http://msdn2.microsoft.com/en-us/library/30swa673.aspx


  • JeffreyCollins

    thx for reply me~! ^^ but i dun no how to try out the solution tt in the website which u gave me, im a foreigner hence my English so sux, can help more

    about this:

    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    
    Public Class PortChat
      Shared _continue As Boolean
      Shared _serialPort As SerialPort
    
      Public Shared Sub Main()
        Dim name As String
        Dim message As String
        Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase
        Dim readThread As Thread = New Thread(AddressOf Read)
    
        ' Create a new SerialPort object with default settings.
        _serialPort = New SerialPort()
    
        ' Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName)
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
        _serialPort.Parity = SetPortParity(_serialPort.Parity)
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)
    
        ' Set the read/write timeouts
        _serialPort.ReadTimeout = 500
        _serialPort.WriteTimeout = 500
    
        _serialPort.Open()
        _continue = True
        readThread.Start()
    
        Console.Write("Name: ")
        name = Console.ReadLine()
    
        Console.WriteLine("Type QUIT to exit")
    
        While (_continue)
          message = Console.ReadLine()
    
          If sComparer.Equals("quit", message) Then
            _continue = False
          Else
            _serialPort.WriteLine( _
              String.Format("<{0}>: {1}", name, message))
          End If
        end while
    
        readThread.Join()
        _serialPort.Close()
      End Sub
    
      Public Shared Sub Read()
        While (_continue)
          Try
            Dim message As String = _serialPort.ReadLine()
            Console.WriteLine(message)
          Catch ex As TimeoutException
            ' Do nothing
          End Try
        End While
      End Sub
    
      Public Shared Function SetPortName(ByVal defaultPortName As String) As String
        Dim newPortName As String
    
        Console.WriteLine("Available Ports:")
        Dim s As String
        For Each s In SerialPort.GetPortNames()
          Console.WriteLine("  {0}", s)
        Next s
    
        Console.Write("COM port({0}): ", defaultPortName)
        newPortName = Console.ReadLine()
    
        If newPortName = "" Then
          newPortName = defaultPortName
        End If
        Return newPortName
      End Function
    
      Public Shared Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer
        Dim newBaudRate As String
    
        Console.Write("Baud Rate({0}): ", defaultPortBaudRate)
        newBaudRate = Console.ReadLine()
    
        If newBaudRate = "" Then
          newBaudRate = defaultPortBaudRate.ToString()
        End If
    
        Return Integer.Parse(newBaudRate)
      End Function
    
      Public Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity
        Dim newParity As String
    
        Console.WriteLine("Available Parity options:")
        Dim s As String
        For Each s In [Enum].GetNames(GetType(Parity))
          Console.WriteLine("  {0}", s)
        Next s
    
        Console.Write("Parity({0}):", defaultPortParity.ToString())
        newparity = Console.ReadLine()
    
        If newparity = "" Then
          newparity = defaultPortParity.ToString()
        End If
    
        Return CType([Enum].Parse(GetType(Parity), newParity), Parity)
      End Function
    
      Public Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer
        Dim newDataBits As String
    
        Console.Write("Data Bits({0}): ", defaultPortDataBits)
        newDataBits = Console.ReadLine()
    
        If newDataBits = "" Then
          newDataBits = defaultPortDataBits.ToString()
        End If
    
        Return Integer.Parse(newDataBits)
      End Function
    
      Public Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits
        Dim newStopBits As String
    
        Console.WriteLine("Available Stop Bits options:")
        Dim s As String
        For Each s In [Enum].GetNames(GetType(StopBits))
          Console.WriteLine("  {0}", s)
        Next s
    
        Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())
        newStopBits = Console.ReadLine()
    
        If newStopBits = "" Then
          newStopBits = defaultPortStopBits.ToString()
        End If
    
        Return CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits)
      End Function
    
      Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake
        Dim newHandshake As String
    
        Console.WriteLine("Available Handshake options:")
        Dim s As String
        For Each s In [Enum].GetNames(GetType(Handshake))
          Console.WriteLine("  {0}", s)
        Next s
    
        Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())
        newHandshake = Console.ReadLine()
    
        If newHandshake = "" Then
          newHandshake = defaultPortHandshake.ToString()
        End If
    
        Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake)
      End Function
    End Class
     
    Could u like to send me a sample programme tt i can investigate it by myself ^^
    


  • Navaron

    It looks like you found an example.

    This looks like a simple console application.  So create a console application and then copy/paste this code and compile it which should generate you and EXE which you can then run.

    Obviously you will need two machines connected via a serial cable.

     

     


  • [VB2005]Q of Serialport ----> Need Help