B4J Question How to connect B4J directly with serial port server?

amorosik

Expert
Licensed User
1766594438747.png


We use HF5122 to connect rs232 (barcode scanner, cash register, etc..) devices with a pc
Typically, you can use software like VCOM to add one or more virtual COM ports and "talk" to the serial server's serial ports
However, I'd like to connect directly, using the "TCP server" feature, which can be configured for each of the device's serial ports
For each of the two serial ports, you can configure a port number so that the client uses IP_address : port (for esample 192.168.1.100:23 x seriale1, 192.168.1.100:26 x serial2) to connect to the desired serial port

So the question is: how can I use B4J code to connect to the TCP server available on the device and send/receive the data that the device connected to serial_port_1 is transmitting/receiving?
 
Last edited:

aminoacid

Active Member
Licensed User
Longtime User
View attachment 169024

So the question is: how can I use B4J code to connect to the TCP server available on the device and send/receive the data that the device connected to serial_port_1 is transmitting/receiving?

I have done something very similar. Quite Simple. Here's a skeleton version of how you can do it:

Connect to Serial Server:
Sub Process_Globals
    Private Socket1 As Socket
    Private AStreams As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    ' Initialize socket
    Socket1.Initialize("Socket1")

    ' Connect to TCP server
    Log("Connecting to server...")
    Socket1.Connect("192.168.1.100", 23, 5000)

 End Sub
 
 Sub Socket1_Connected (Successful As Boolean)
    If Successful = False Then
        Log("Connection failed")
        Return
    End If

    Log("Connected to server")

    ' Initialize AsyncStreams (NON-prefix mode)
    AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
End Sub

Sub Socket1_Disconnected
    Log("Socket disconnected")
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim received As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log("Received: " & received)
End Sub

Sub AStreams_Error
    Log("AsyncStreams error")
End Sub

Sub AStreams_Terminated
    Log("AsyncStreams terminated")
End Sub
 
Sub SendToSever (s as string)
    If AStreams.IsInitialized = False Then
        Log("Not connected")
        Return
    End If

    AStreams.Write(s.GetBytes("UTF8"))
    Log("Sent: " & msg)
End Sub
 
Upvote 0
Top