|
-
Aug 23rd, 2016, 11:52 PM
#1
Thread Starter
Junior Member
client data receive only device connected
Dear all.
I have the Hardware device which connected to http://www.usriot.com/p/rs485-to-wifi-converters/ above converter. I have connected 2 devices so far and Indivually working fine. I have written Small Vb code to get data from wifi and display on Vb screen. With above setup i am facing problem in software hangs
The software get hangs in 4 cases:
- Wifi device has low signal stregth
- wifi device is not powered on
- wifi device stop communication; problem facing opening client port
- Hardware device fail to send data.
This my client code to receive the data over wifi. With this I can able to update without any issue . unless wifi and hardware working fine
Code:
Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim Port1 As String
Port1 = SMCB1_Port.Text
Dim port As Int32 = CInt(Port1)
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
I have tried to modify code using client syntax by checking connected; avilable; active or not
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
This is working fine with all true cases. But not working fine with false case.
-
Aug 24th, 2016, 06:54 AM
#2
Re: client data receive only device connected
I usually put the connection and receiver code in a background thread, that way if the device is not available or goes away, it doesn't hang the GUI. The background process can update status variables so that the GUI can indicate whether the connection is currently active or not.
-
Aug 24th, 2016, 06:58 AM
#3
Thread Starter
Junior Member
Re: client data receive only device connected
 Originally Posted by passel
I usually put the connection and receiver code in a background thread, that way if the device is not available or goes away, it doesn't hang the GUI. The background process can update status variables so that the GUI can indicate whether the connection is currently active or not.
How can i change code so process got fast.Even tried with simple instruction with wifi device off. It taking long time to generate error.Please help me out sort error
-
Aug 24th, 2016, 12:15 PM
#4
Re: client data receive only device connected
Well, the process can probably stall, without actually failing, for a fairly long period.
That is why some functions allow specifying a timeout so if what you want to do, such as connect to a server, doesn't happen in a reasonable period, it will time out and return to you with an error code.
In other cases, I might want to allow it to wait as long as it will to do something, but usually have a "GUI Timer" that runs periodically to update various status indicators and update the form, because in some of my uses, the form has a number of tab pages to show different state data for different connections, so I don't update all the controls on all the pages all the time, just the current showing page for the currently selected interface. And data may be coming in at a faster rate than I want to update the display, so the display updates are governed by the GUI timer, and the state data for all interfaces is updated at whatever rate it is received.
In that scenario, the background thread can set a variable to indicate what it is trying to do (e.g. connect to the server, or waiting for a response), and also set a "watchdog" integer variable to some desired countdown value at the start of the action.
For instance, if my GUI timer is running at 10hz, and the background thread is trying to connect to the server, and I want to allow for around a 1/2 second for that to happen, the background thread can set the "watchdog" variable to 5, and start trying to connect. The GUI timer would see that we're in a "trying to connect state" and decrement the "watchdog" variable each pass, and if it reaches zero, flag a connection error.
I don't have time to check, but in the case of doing a TCP connect, there is probably already a timeout value you can specify, so you don't have to go the "watchdog" route for that phase if you don't want to.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|