I have made a program to asynchronously connect to 20 barcode readers via system.net.sockets. I can send and receive data from the reader but the problem is the information that comes back is broken or incomplete. I am new to this so I am wondering what I am missing. Here is all of my receive code
Connection:
Code:readerSock1 = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipAddress1 As IPAddress = IPAddress.Parse(arrIP(1 - 1)) Dim ipEndPoint1 As IPEndPoint = New IPEndPoint(ipAddress1, arrPort(1 - 1)) readerSock1.BeginConnect(ipEndPoint1, New AsyncCallback(AddressOf OnConnect1), Nothing)
Receive:Code:Private Sub OnConnect1(ByVal ar As IAsyncResult) Dim msg1 As String Try readerSock1.EndConnect(ar) readerSock1.BeginReceive(byteData1, 0, byteData1.Length, SocketFlags.None, _ New AsyncCallback(AddressOf OnRecieve1), readerSock1) Catch ex As Exception msg1 = ex.Message End Try RTBappend(msg1) End Sub
Code:Private Sub OnRecieve1(ByVal ar As IAsyncResult) Dim readersock1 As Socket = ar.AsyncState 'readersock1.EndReceive(ar) Dim bytesRec As Byte() = byteData1 Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec) Read1(message) readersock1.BeginReceive(byteData1, 0, byteData1.Length, SocketFlags.None, _ New AsyncCallback(AddressOf OnRecieve1), readersock1) End SubThe reader is constantly sending information, so my guess is the receive doesnt know when to start and stop? attached are what I get from the reader. In Hyper Terminal the information comes across OK, but in my program you can see station 2 does not look the same as hyper terminal.Code:Delegate Sub _Read1(ByVal msg As String) Private Sub Read1(ByVal msg As String) If InvokeRequired Then Invoke(New _Read1(AddressOf Read1), msg) Exit Sub End If lblStation1.Text = msg End Sub
![]()


Reply With Quote