|
-
Feb 1st, 2010, 06:27 AM
#1
Thread Starter
Addicted Member
winsock data arrival
I made a program that communicate with some electronic device using winsock udp protocol, i am trying to write a function that will tell me rather or not a device is online by looking at if anything is sent back from the device.
this is my function
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString, bytesTotal
If Not Len(strData) = 0 Then
online = 1
End If
End Sub
i declared the variable "online" as a global variable, but somehow "online" is always 0...i think i am trying to do this the wrong way, can anyone give me a hand?
-
Feb 1st, 2010, 11:53 AM
#2
Re: winsock data arrival
Have you checked whether the data actually arrives? Does your strData contain anything?
-
Feb 1st, 2010, 08:47 PM
#3
Thread Starter
Addicted Member
Re: winsock data arrival
yes data does arrive, strdata is the string i receive from my devices.
-
Feb 2nd, 2010, 04:32 AM
#4
Re: winsock data arrival
Try setting a breakpoint somewhere in this sub. I get the feeling that it gets called several times.
-
Feb 2nd, 2010, 09:13 AM
#5
Thread Starter
Addicted Member
Re: winsock data arrival
a break point you mean using the sleep function? yes the function will get call several times since i am trying to test connection with around 5-6 devices
-
Feb 2nd, 2010, 09:44 AM
#6
Re: winsock data arrival
Why sleep? Just press F9 at the needed line of code and the program will stop executing at this very point (you can continue it from this place rather than restart it).
You can count how many times your sub gets called.
This code:
vb.net Code:
If Not Len(strData) = 0 Then online = 1 End If
provides that your sub sets the online variable to 1
if it somehow reverts to 0 then your global variable is modified somewhere else.
A little advice - change your sub into function:
vb.net Code:
Private Function IsOnline(ByVal bytesTotal As Long) As Boolean Dim strData As String Winsock1.GetData strData, vbString, bytesTotal If strData.Length > 0 Then Return True Else Return False End If
Then you can simply call it:
If Online(your args) Then ...
-
Feb 2nd, 2010, 10:45 PM
#7
Thread Starter
Addicted Member
Re: winsock data arrival
unfortunately i cannot modify the sub function because it is associated with the winsock property, i tried changing the data_arrival function to return a value, it will give me an error
in my main function i have this
Do Until (allocate_rs.EOF)
ID = allocate_rs.Fields("Cont_IP").Value
Call check_online
allocate_rs.MoveNext
MsgBox online
Loop
i tried using your suggestion in the sub function
If Not Len(strData) = 0 Then
online = 1
End If
but everytime "online" is displayed as blank (no value)
-
Feb 3rd, 2010, 06:26 AM
#8
Re: winsock data arrival
This code will always set Online = 1 if your strData is not empty.
Code:
If Not Len(strData) = 0 Then
online = 1
End If
ALWAYS!
Either your 'online' variable is modified elsewhere (you don't use multithreading here, do you?) or your strData is empty. There are no other options. You say you've checked and strData is not empty then we only have one remaining option: your global variable called 'online' is modified elsewhere
vb.net Code:
By the way, you could shadow your online variable by some local one with the same name. Module GlobalModule Public Online As Integer = 1 ' Global variable Public Sub Main MsgBox(Online) ' Will return 1 Call SomeSub() MsgBox(Online) ' Will return 1 End Sub Public Sub SomeSub() Dim Online As Integer ' You shadow the global one MsgBox(Online) ' Will return 0 End Sub End Module
-
Feb 4th, 2010, 02:04 AM
#9
Thread Starter
Addicted Member
Re: winsock data arrival
i think i find out the problem now...is not about my code (or maybe it is), i realized that when i do a loop to send data
Do Until (read_controller_rs.EOF)
send = "02000BFFFFFFFFFFFF113100FFD403"
Winsock1.SendData send
read_controller_rs.MoveNext
Loop
my data arrival will only be fired AFTER the loop goes through, so it means that if i have 4 senddata, i will not receive anything until all data has been sent then my data arrival will fire and i get all 4 replys at once.
I am still pretty confuse about why it work this way, i want to get a respond everytime i sent data but instead i get 4 responds back after i send all 4 data....
-
Feb 4th, 2010, 03:23 AM
#10
Re: winsock data arrival
Not a perfect solution but...
vb.net Code:
Do Until(read_controller_rs.EOF) For each c As Char In send Winsock1.SendData c Next read_controller_rs.MoveNext Loop
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
|