Question Weird problems with connecting to a mobile device

-=!F34R!=-

New member
Joined
Jul 14, 2011
Messages
4
Programming Experience
3-5
Hi There, I’m new to posting so I apologise if I’m doing this wrong but here goes.
Background: I got a MS SQL Data base where I got a lot of data and tables that holds information about products and orders. I’m building a system where I got some cipher lab 8400 scanners(mobile scanner that has got a screen, wireless LAN and is fully programmable in basic) which will scan an order number, requests the data in the order number from the server and display what needs to be picket from stock.

My full code looks like this.
Imports System.Net.Sockets
Imports System.Text
Imports system.data.sqlclient
Imports System.Data
Imports System.IO
 
Module Module1
    Dim clientsList As New Hashtable
    'Dim clientSocket As TcpClient
    Public con As New SqlConnection
 
    Sub Main()
        Dim serverSocket As New TcpListener(1024)
        Dim clientSocket As TcpClient
        Dim infiniteCounter As Integer
        Dim counter As Integer
        Dim clno As String
        Dim i As Integer
 
        Try
            con.ConnectionString = ("Integrated Security=SSPI; Initial Catalog=Ball_Stratof; Data Source=(local)\sqlexpress;")
            con.Open()
            'MsgBox("Connection made...")
            con.Close()
        Catch ex As Exception
            MsgBox("Error while connecting to sql server." & ex.Message)
            con.Close()
        End Try
 
        'Try
        serverSocket.Start()
        msg("Server Started ....")
        counter = 0
        infiniteCounter = 0
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()
 
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
 
            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            'dataFromClient = _
            'dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
 
            clientsList(dataFromClient) = clientSocket
 
            'broadcast(dataFromClient + " Joined ", dataFromClient, False)
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
            Console.WriteLine(dataFromClient.ToString & " connected ")
            Console.WriteLine("Client>> " & dataFromClient.ToString)
            'For i = 1 To 2
            Dim msg1 As String
            msg1 = "*001,connected" & Chr(13)
            Dim Item As DictionaryEntry
            For Each Item In clientsList
                Dim broadcastSocket As TcpClient
                broadcastSocket = CType(Item.Value, TcpClient)
                Dim broadcastStream As NetworkStream = _
                broadcastSocket.GetStream()
                Dim broadcastBytes As [Byte]()
 
                broadcastBytes = Encoding.ASCII.GetBytes(msg1)
                Console.WriteLine("server>>" & msg1)
 
                broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
                broadcastStream.Flush()
            Next
            'Next
 
        Next
 
        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
        'Catch ex As Exception
        'MsgBox("Error." & ex.Message)
        'con.Close()
        'End Try
 
    End Sub
 
    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(" >> " + mesg)
    End Sub
    Private Sub broadcast(ByVal msg As String) ', _
        'ByVal uName As String, ByVal flag As Boolean)
        'Try
        Dim Item As DictionaryEntry
        For Each Item In clientsList
            Dim broadcastSocket As TcpClient
            broadcastSocket = CType(Item.Value, TcpClient)
            'broadcastSocket = clientSocket
            Console.WriteLine("Broad cast socket: " & broadcastSocket.ToString)
            Dim broadcastStream As NetworkStream = _
                    broadcastSocket.GetStream()
            Dim broadcastBytes As [Byte]()
 
            'broadcastBytes = Encoding.ASCII.GetBytes(msg)
            broadcastBytes = Encoding.ASCII.GetBytes(msg)
            Console.WriteLine("server>>" & msg)
 
            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
            broadcastStream.Flush()
        Next
    End Sub
 
    Public Class handleClinet
        Dim clientSocket As TcpClient
        Dim clNo As String
        Dim clientsList As Hashtable
 
        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String, ByVal cList As Hashtable)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Me.clientsList = cList
            Console.WriteLine("Client Added: " & cList.ToString)
            Console.WriteLine("Client Added: " & inClientSocket.ToString)
            Console.WriteLine("Client Added: " & clineNo.ToString)
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub
 
        Private Sub doChat()
 
            Dim infiniteCounter As Integer
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim sendBytes As [Byte]()
            Dim serverResponse As String
            Dim rCount As String
            requestCount = 0
            For infiniteCounter = 1 To 2
                infiniteCounter = 1
                'Try
                requestCount = requestCount + 1
                Dim networkStream As NetworkStream = _
                        clientSocket.GetStream()
                networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                'MsgBox(dataFromClient.ToString)
                On Error GoTo Handler_1
                dataFromClient = _
                dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
 
                'msg("From client - " + clNo + " : " + dataFromClient)
                Dim SplitData() As String
                Dim DataType As String
                Dim ScanName As String
 
                SplitData = Split(dataFromClient, ",", )
                DataType = SplitData(0)
                ScanName = SplitData(1)
                Console.WriteLine(ScanName & ">> " & dataFromClient.ToString)
 
                Select Case DataType
                    Case Is = "001" 'Connection
                        broadcast("*001,connected" & Chr(13))
                        'broadcast("*OVER", clNo, True)
 
                    Case Is = "002" 'Returnes order to scanner
                        Dim command1 As String
                        Dim DataR1 As SqlDataReader
 
                        command1 = "select"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.entered_dt,"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.ord_no,"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.cus_no, "
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.ship_to_name,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.line_no,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.item_no,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.item_desc_1,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.loc,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.qty_ordered,"
                        command1 = command1 & Chr(13) & "OEORDLIN_SQL.uom"
                        command1 = command1 & Chr(13) & "FROM"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL inner join OEORDLIN_SQL on"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.ord_type = OEORDLIN_SQL.ord_type"
                        command1 = command1 & Chr(13) & "and OEORDHDR_SQL.ord_no = OEORDLIN_SQL.ord_no"
                        command1 = command1 & Chr(13) & "WHERE"
                        command1 = command1 & Chr(13) & "OEORDHDR_SQL.ord_type = 'O'"
                        command1 = command1 & Chr(13) & "and OEORDHDR_SQL.status < '8'"
                        command1 = command1 & Chr(13) & "and OEORDLIN_SQL.loc in ('PLU','VEG','SUR','LAN','MP')"
                        command1 = command1 & Chr(13) & "and OEORDHDR_SQL.ord_no = " & SplitData(2)
 
                        Dim mycommand = New SqlCommand(command1, con)
                        Dim DataToSend1 As String
                        'Try
                        con.Open()
                        DataR1 = mycommand.executeReader()
                        While DataR1.Read()
                            'Form1.DataGridView1.Rows.Add(New String() {Data(0).ToString(), Data(1).ToString(), Data(2).ToString(), Data(3).ToString(), Data(4).ToString, Data(5).ToString, Data(6).ToString, Data(7).ToString, Data(8).ToString, Data(9).ToString()})
                            DataToSend1 = "*002" & "," & DataR1(0).ToString() & "," & DataR1(1).ToString() & "," & DataR1(2).ToString() & "," & DataR1(3).ToString() & "," & DataR1(4).ToString & "," & DataR1(5).ToString & "," & DataR1(6).ToString & "," & DataR1(7).ToString & "," & DataR1(8).ToString & "," & DataR1(9).ToString()
                            broadcast(DataToSend1)
                        End While
                        broadcast("*OVER")
                        con.Close()
                        'Catch ex As Exception
                        'MsgBox("Query Unsucseesfull: " & ex.Message)
                        'con.Close()
                        'End Try
 
                    Case Is = "003" 'Returnes stock detailes scanned
                        Dim command2 As String
                        Dim DataR2 As SqlDataReader
 
                        command2 = command2 & Chr(13) & "select"
                        command2 = command2 & Chr(13) & "SFORDFIL_SQL.ord_no,"
                        command2 = command2 & Chr(13) & "SFORDFIL_SQL.item_no,"
                        command2 = command2 & Chr(13) & "SFORDFIL_SQL.ord_loc,"
                        command2 = command2 & Chr(13) & "qty_on_scan = case"
                        command2 = command2 & Chr(13) & "when SFORDFIL_SQL.inv_uom = 'TR' then 1"
                        command2 = command2 & Chr(13) & "when SFORDFIL_SQL.inv_uom = 'EA' then cast(right(rtrim(SFORDFIL_SQL.item_desc_2),3) as int)"
                        command2 = command2 & Chr(13) & "else"
                        command2 = command2 & Chr(13) & "0"
                        command2 = command2 & Chr(13) & "End"
                        command2 = command2 & Chr(13) & "from SFORDFIL_SQL"
                        command2 = command2 & Chr(13) & "where"
                        command2 = command2 & Chr(13) & "ord_no = " & SplitData(2)
                        command2 = command2 & Chr(13) & "and (SFORDFIL_SQL.ord_status in ('R', 'S', 'E') or "
                        command2 = command2 & Chr(13) & "(SFORDFIL_SQL.ord_status = 'C'and cast(cast(SFORDFIL_SQL.compl_dt AS nvarchar) AS smalldatetime) >= dateadd(m, -2, getdate())))"
 
                        Dim mycommand2 = New SqlCommand(command2, con)
                        Dim DataToSend2 As String
                        'Try
                        con.Open()
                        DataR2 = mycommand2.executeReader()
                        While DataR2.Read()
                            DataToSend2 = "003" & "," & DataR2(0).ToString() & "," & DataR2(1).ToString() & "," & DataR2(2).ToString() & "," & DataR2(3).ToString()
                            broadcast(DataToSend2)
                        End While
                        'broadcast("*OVER")
                        con.Close()
                        'Catch ex As Exception
                        'MsgBox("Query Unsucseesfull: " & ex.Message)
                        'con.Close()
                        'End Try
 
 
 
                End Select
 
                rCount = Convert.ToString(requestCount)
 
                'broadcast(dataFromClient, clNo, True)
                'Catch ex As Exception
                'MsgBox(ex.ToString)
                'End Try
            Next
            Exit Sub
Handler_1:
            Exit Sub
        End Sub
 
 
    End Class
End Module

Sorry if its long, but I have got no clue where my problem is.

I wrote a client program in vb.net which works perfectly, I get all the data as expected.
When I connect with the scanner to the server I get the right result when connecting: *001,connected. When I request any of the order numbers I get no return from the server, but the server processes all the data and display it on the computer screen.
I programmed the scanner to display any data that’s coming through but all that’s coming through is the *001,connected and nothing else.
Not so long ago i did a simalar program in vb6 using winsock. Tried that program with the same program on the scanner and that work, so i know my problem is not on the scanner.
A second eye on my coding might help me resolve the problem, so please have a look at my code. Any solutions or sugestions will be much apriciated for I’ve stared at this code for about 3 days now
Thanks in advance.
 
Don't stare at the code - that'll rarely help ;)

Get some breakpoints in there and step through the execution. Test variable values at given points and track down where the problem lies.

Also your On Error Goto... is obviously a remnant of you pulling your VB6 code in - .NET supports Try...Catch... blocks.
 
Hi Menthos

Thanks for your reply. I’ve tried that, my code looks quite a mess from doing so. As you probably can see my comments are the remainder of my attempts to see where the problem lies. Everything checked out. I even wrote a client program that displays me anything that goes through that port and it worked.

I’ve done allot of thinking and I think that when the scanner request’s data from the server after it connected, somehow the sockets gets mixed up. I’ve tried assigning variables but it returns a string and not a socket number. Any idea how I can test what socket I’m sending data to?
 
Hi there

I figured out my problem. The second broadcast where I getrequests from the client, I did not close the sockets of properly.

Thanks for your replies.
 
Back
Top