I need help to translate the serial code below to VBA so that are factored in the VBA code below:

' start TCP socket and BeginAccept listener
    instance.connect("192.2.168.100", 5000)
'start sending data
instance.sendData(readbuffer, "192.2.168.200", 5000)

'Start Receiving data
instance.ReadData(readbuffer, "192.2.168.200", 5000)

Below is the VBA code that require the above code after translation:

Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
    Dim lngStatus As Long
    Dim strError  As String
    Dim strData   As String

    ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=9600 parity=N data=8 stop=1")
    
    If lngStatus <> 0 Then
	' Handle error.
        lngStatus = CommGetError(strError)
	MsgBox "COM Error: " & strError
    End If
    

    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)

    ' Write data to serial port.
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
	' Handle error.
    End If

    ' Read maximum of 64 bytes from serial port.
    lngStatus = CommRead(intPortID, strData, 64)
    If lngStatus > 0 Then
        ' Process data.
    ElseIf lngStatus < 0 Then
        ' Handle error.
    End If

    ' Reset modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, False)
    lngStatus = CommSetLine(intPortID, LINE_DTR, False)

    ' Close communications.
    Call CommClose(intPortID)

The original VB6 Code which works in similar manner is also reproduced below:

Imports System.IO.Ports
Public Class Form1
  Private WithEvents com1 As New SerialPort
  'make a instance on class scope:
  Private WithEvents instance As New TCPChat
  Private readbuffer As String

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try
      With com1
        .PortName = "Com1"
        .BaudRate = 9600
        .Parity = Parity.None
        .DataBits = 8
        .StopBits = StopBits.One
        'we need a character to terminate the readline
        .NewLine = vbCr
      End With
      com1.Open()
    Catch ex As Exception
      MsgBox(ex.ToString)
    End Try

    ' start TCP socket and BeginAccept listener
    instance.connect("192.2.168.100", 5000)

  End Sub
  ' receive data from com1
  Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                    Handles com1.DataReceived

    Try

      readbuffer = com1.ReadLine()

      Me.Invoke(New EventHandler(AddressOf DoUpdate))

    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

  End Sub
  ' data from Serial Port to LAN
  Public Sub DoUpdate()

    instance.sendData(readbuffer, "192.2.168.200", 5000)

  End Sub
  ' data from LAN to Serial Port:
  Private Sub recdata(ByVal txt As String) Handles instance.Datareceived
    com1.WriteLine(txt)
  End Sub

End Class
4 Spice ups

The original code is VB.Net, not VB6. Using a .Net object in VBA is problematic. Was the original TCPchat class compiled with COM Interoperability? I can’t find the reference page for this library which isn’t surprising since it was last updated 10 years ago.

My advice would be to rewrite it without relying on the TCPchat class library. This isn’t actually that tough based on what I think your code is doing.

Public Function ComWrite() as String
	dim ComString as String
	Dim char as String
	Dim sResponse as String
	
	' Open the com port
	Open "COM1:9600,N,8,1,X" For Binary Access Read Write As #1
	ComString = "Command for the far end" + VbCR ' You probably have to terminate strings with a Carriage Return, Line Feed or both
	' Send a string
	Put #1, , ComString
	'Get responses
	char = Input(1, #1)
	' loop until the far end sends a carriage return
	While (char <> vbCR)
		' Start building the response string
		sResponse = sResponse + char
		' get anbother character
		char = Input(1, #1)
	Wend
	' Always clean up!
    Close #1
	ComWrite = sResponse
End Function

Obviously you’ll want to add error handling and you might need more complexity, but this should point you in a decent direction.