I need help to translate the serial code below to VBA so that are factored in the VBA code below:<\/p>\n
' start TCP socket and BeginAccept listener\n instance.connect(\"192.2.168.100\", 5000)\n<\/code><\/pre>\n'start sending data\ninstance.sendData(readbuffer, \"192.2.168.200\", 5000)\n\n<\/code><\/pre>\n'Start Receiving data\ninstance.ReadData(readbuffer, \"192.2.168.200\", 5000)\n\n<\/code><\/pre>\n
Advertisement
Below is the VBA code that require the above code after translation:<\/p>\n
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4\n Dim lngStatus As Long\n Dim strError As String\n Dim strData As String\n\n ' Initialize Communications\n lngStatus = CommOpen(intPortID, \"COM\" & CStr(intPortID), _\n \"baud=9600 parity=N data=8 stop=1\")\n \n If lngStatus <> 0 Then\n\t' Handle error.\n lngStatus = CommGetError(strError)\n\tMsgBox \"COM Error: \" & strError\n End If\n \n\n ' Set modem control lines.\n lngStatus = CommSetLine(intPortID, LINE_RTS, True)\n lngStatus = CommSetLine(intPortID, LINE_DTR, True)\n\n ' Write data to serial port.\n lngSize = Len(strData)\n lngStatus = CommWrite(intPortID, strData)\n If lngStatus <> lngSize Then\n\t' Handle error.\n End If\n\n ' Read maximum of 64 bytes from serial port.\n lngStatus = CommRead(intPortID, strData, 64)\n If lngStatus > 0 Then\n ' Process data.\n ElseIf lngStatus < 0 Then\n ' Handle error.\n End If\n\n ' Reset modem control lines.\n lngStatus = CommSetLine(intPortID, LINE_RTS, False)\n lngStatus = CommSetLine(intPortID, LINE_DTR, False)\n\n ' Close communications.\n Call CommClose(intPortID)\n<\/code><\/pre>\n
Advertisement
The original VB6 Code which works in similar manner is also reproduced below:<\/p>\n
Imports System.IO.Ports\nPublic Class Form1\n Private WithEvents com1 As New SerialPort\n 'make a instance on class scope:\n Private WithEvents instance As New TCPChat\n Private readbuffer As String\n\n Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\n\n Try\n With com1\n .PortName = \"Com1\"\n .BaudRate = 9600\n .Parity = Parity.None\n .DataBits = 8\n .StopBits = StopBits.One\n 'we need a character to terminate the readline\n .NewLine = vbCr\n End With\n com1.Open()\n Catch ex As Exception\n MsgBox(ex.ToString)\n End Try\n\n ' start TCP socket and BeginAccept listener\n instance.connect(\"192.2.168.100\", 5000)\n\n End Sub\n ' receive data from com1\n Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _\n ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _\n Handles com1.DataReceived\n\n Try\n\n readbuffer = com1.ReadLine()\n\n Me.Invoke(New EventHandler(AddressOf DoUpdate))\n\n Catch ex As Exception\n MsgBox(ex.Message)\n End Try\n\n End Sub\n ' data from Serial Port to LAN\n Public Sub DoUpdate()\n\n instance.sendData(readbuffer, \"192.2.168.200\", 5000)\n\n End Sub\n ' data from LAN to Serial Port:\n Private Sub recdata(ByVal txt As String) Handles instance.Datareceived\n com1.WriteLine(txt)\n End Sub\n\nEnd Class\n<\/code><\/pre>","upvoteCount":4,"answerCount":2,"datePublished":"2021-07-11T11:24:15.000Z","author":{"@type":"Person","name":"spiceuser-t7sgg","url":"https://community.spiceworks.com/u/spiceuser-t7sgg"},"suggestedAnswer":[{"@type":"Answer","text":"I need help to translate the serial code below to VBA so that are factored in the VBA code below:<\/p>\n
' start TCP socket and BeginAccept listener\n instance.connect(\"192.2.168.100\", 5000)\n<\/code><\/pre>\n'start sending data\ninstance.sendData(readbuffer, \"192.2.168.200\", 5000)\n\n<\/code><\/pre>\n'Start Receiving data\ninstance.ReadData(readbuffer, \"192.2.168.200\", 5000)\n\n<\/code><\/pre>\nBelow is the VBA code that require the above code after translation:<\/p>\n
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4\n Dim lngStatus As Long\n Dim strError As String\n Dim strData As String\n\n ' Initialize Communications\n lngStatus = CommOpen(intPortID, \"COM\" & CStr(intPortID), _\n \"baud=9600 parity=N data=8 stop=1\")\n \n If lngStatus <> 0 Then\n\t' Handle error.\n lngStatus = CommGetError(strError)\n\tMsgBox \"COM Error: \" & strError\n End If\n \n\n ' Set modem control lines.\n lngStatus = CommSetLine(intPortID, LINE_RTS, True)\n lngStatus = CommSetLine(intPortID, LINE_DTR, True)\n\n ' Write data to serial port.\n lngSize = Len(strData)\n lngStatus = CommWrite(intPortID, strData)\n If lngStatus <> lngSize Then\n\t' Handle error.\n End If\n\n ' Read maximum of 64 bytes from serial port.\n lngStatus = CommRead(intPortID, strData, 64)\n If lngStatus > 0 Then\n ' Process data.\n ElseIf lngStatus < 0 Then\n ' Handle error.\n End If\n\n ' Reset modem control lines.\n lngStatus = CommSetLine(intPortID, LINE_RTS, False)\n lngStatus = CommSetLine(intPortID, LINE_DTR, False)\n\n ' Close communications.\n Call CommClose(intPortID)\n<\/code><\/pre>\nThe original VB6 Code which works in similar manner is also reproduced below:<\/p>\n
Imports System.IO.Ports\nPublic Class Form1\n Private WithEvents com1 As New SerialPort\n 'make a instance on class scope:\n Private WithEvents instance As New TCPChat\n Private readbuffer As String\n\n Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\n\n Try\n With com1\n .PortName = \"Com1\"\n .BaudRate = 9600\n .Parity = Parity.None\n .DataBits = 8\n .StopBits = StopBits.One\n 'we need a character to terminate the readline\n .NewLine = vbCr\n End With\n com1.Open()\n Catch ex As Exception\n MsgBox(ex.ToString)\n End Try\n\n ' start TCP socket and BeginAccept listener\n instance.connect(\"192.2.168.100\", 5000)\n\n End Sub\n ' receive data from com1\n Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _\n ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _\n Handles com1.DataReceived\n\n Try\n\n readbuffer = com1.ReadLine()\n\n Me.Invoke(New EventHandler(AddressOf DoUpdate))\n\n Catch ex As Exception\n MsgBox(ex.Message)\n End Try\n\n End Sub\n ' data from Serial Port to LAN\n Public Sub DoUpdate()\n\n instance.sendData(readbuffer, \"192.2.168.200\", 5000)\n\n End Sub\n ' data from LAN to Serial Port:\n Private Sub recdata(ByVal txt As String) Handles instance.Datareceived\n com1.WriteLine(txt)\n End Sub\n\nEnd Class\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2021-07-11T11:24:15.000Z","url":"https://community.spiceworks.com/t/translating-vb6-to-vba-help-required/805215/1","author":{"@type":"Person","name":"spiceuser-t7sgg","url":"https://community.spiceworks.com/u/spiceuser-t7sgg"}},{"@type":"Answer","text":"