How do I use VB to control my Ethernet to Serial device?

FAQs

Brainboxes provides a .NET API which allows easy integration of Brainboxes Ethernet to Serial modules into your Visual Basic Windows software applications.

The Brainboxes.IO .NET API does not use the serial port API, instead it communicates with the Ethernet to Serial Devices using raw TCP. One benefit of this is that Brainboxes Windows COM port drivers don’t need be to installed on any machine. On the Brainboxes Ethernet to serial device, a configuration step is required to allow raw TCP communication. The Ethernet to serial device then handles all handshaking and com port settings on the serial port.

Requirements

Configure Brainboxes Ethernet to Serial for Raw TCP

files/pages/support/faqs/Images/ethernet-to-serial/configure-ethernet-to-serial-raw-tcp.png

  1. Navigate to the homepage of your brainboxes ethernet to serial device in a web browser
  2. Click on the serial port bottom on the left hand side navigation
  3. Set the appropriate settings for you serial port connection and then check the « always use these settings checkbox »
  4. Change the Protocol Type to « Raw TCP »
  5. Click Save
  6. You can test this set up by connecting to the IP address and port using a terminal such as Putty

Visual Basic VB.NET Code Sample

Brainboxes.IO VB API in visual studio for Ethernet to serial

The following code works in Visual Basic targeted at .Net version 2.0 and above:

Module Module1

    Sub Main()

        Dim ES As ESDevice = New ES246("192.168.2.144")
        Try
            Console.WriteLine("Connecting to " + ES.ToString())
            'connect method opens all serial ports of the device, an es-246 only has 1 port
            ES.Connect()
            'alternatively just open the port you need
            'ES.Ports(0).Connect()

            ES.Ports(0).Label = "Serial Weighing scale" 'you can give Each port a label, useful For debugging

            Console.WriteLine("Sending Commands")

            'es-246 only has one port, note the ports are indexed from 0
            'the default protocol automatically appends carriage return to the end of a message And encodes data into ASCII
            ES.Ports(0).Send("HELLO ... ")
            ES.Ports(0).Send("HELLO AGAIN...")

            'you can use a different protocol to suit the device you are communicating with Like this
            Dim protocol As DefaultSerialProtocol = New DefaultSerialProtocol()
            protocol.TerminatingCharacters = "rn" 'you can Set multiple Char line endings With the Default protocol
            protocol.Encoding = System.Text.Encoding.Unicode 'you can also change the character encoding
            ES.Ports(0).Protocol = protocol
            'alternatively create a class which implements ISerialProtocol

            ' 1 minutes in milliseconds
            Dim msToRx As Integer = 1 * 60 * 1000

            Console.WriteLine("Waiting " + (msToRx / 1000) + " seconds for data to be received")
            Dim sw As Stopwatch = Stopwatch.StartNew()

            While (sw.ElapsedMilliseconds < msToRx)
                Try
                    'write any received data out to the console
                    'note this Is a blocking function, if you have many connections, Or a UI
                    'you will need to do this in another thread
                    'the default protocol waits for the terminating character (by default a /r CARRIAGE RETURN) to determine the end of a message
                    'the default protocol removes the terminating character before returning the message as a string
                    Dim recievedData As String = ES.Ports(0).Receive()
                    Console.WriteLine("Received DATA : " + recievedData)
                Catch ex As TimeoutException
                    'if the receive function times out it throws an exception
                    'but it doesn't matter as we are just waiting for data in this example
                    Console.WriteLine("No data received within timeout")
                End Try

            End While

            sw.Stop()

            ES.Ports(0).Send("GOODBYE!!!!!!!")
        Catch e As Exception
            Console.WriteLine("An error occurred")
            Console.WriteLine(e)
        Finally
            Console.WriteLine("Press enter to exit...")
            Console.ReadKey()

            Console.WriteLine("Disconnecting")
            ES.Disconnect()
        End Try

    End Sub

End Module
FAQs