1

This is a continuation of my last post: How to read weight from scale using ethernet connection

After creating the TCP connection in vb10 - I am now trying to read the weight from the scale in iFix (vb6). The code below works if I create a breakpoint and step through: strdata takes the weight of the scale (51g at the moment). However, when i simply run the code, I get the error:

Invalid operation at current state 40020.

What i think is happening is something to do with how quickly it reads or trying to read multiple times. Any tips would be great.

TCPclient is referring to winsock, and frmclient refers to my form. The command "S" is the necessary command for the scale to grab the weight value. Thanks!

Public Sub test()
On Error GoTo errHandler

Dim strData As String
frmClient.tcpClient.LocalPort = 0
frmClient.tcpClient.Connect "192.168.0.1", 8000

'Dim i As Integer

' For i = 0 To 2000
'   Debug.Print "connection status=" & frmClient.tcpClient.State
'   If frmClient.tcpClient.State = 7 Then 
'   Exit For Next i

frmClient.tcpClient.SendData "S" & vbCrLf

frmClient.tcpClient.GetData strData
MsgBox ("weight =" & strData)

'Exit Sub
errHandler:
MsgBox Err.Description & " " & Err.Number

'Resume Next
 End Sub
Community
  • 1
  • 1
Dallas
  • 197
  • 1
  • 3
  • 14

2 Answers2

0

Use the DataArrival event of your Winsock Control.

So something like:

' ... in your "frmClient" Form ...
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    tcpClient.GetData strData, vbString
    MsgBox ("weight =" & strData)
End Sub

*Obviously removing the GetData() call in your original test() method.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • I tried this but it did not work. The tcpClient is initialized in the form designer but I am closing it in the sub, thus it tries to use it again but doesn't reinitialize because the form is still alive. Does this make sense? I think I made this unnecessarily complicated because I call the module in the form designer so the form designer keeps running – Dallas Jul 10 '15 at 15:47
  • Maybe don't close it then?...if you must close it, set it back up and use the Connect() method. – Idle_Mind Jul 10 '15 at 15:53
  • The code snippet above also fails to deal with the fact that TCP data is a stream and receive data may arrive one octet at a time or chunked up in any manner at all. Your code is supposed to reassemble the stream and parse off units of data to consume them. TCP is not a datagram protocol. – Bob77 Jul 11 '15 at 03:40
  • True...but we've been given absolutely no information about the protocol for this scale. The point here is that the Winsock control has a `DataReceived()` event that only fires when the connection is in the correct state. Without it you have to use polling loops to wait for the [ConnectionState](https://msdn.microsoft.com/en-us/library/aa228160(v=vs.60).aspx) to be in the correct state before you move on with other code. – Idle_Mind Jul 11 '15 at 03:46
0

Got it working! The code is below. I created a picture sub to initialize the ports/IP at the beginning of the code execution and then to close the connection at the end. I made a timer to automatically read the weight upon stabilization, so the weight can be found by clicking the button, or simply waiting 2 seconds (2000ms). Best of luck and thanks for the help!

Public tcpC As New Winsock

Private Sub CFixPicture_Close()
  tcpC.Close
End Sub

Private Sub CFixPicture_Initialize()
  tcpC.LocalPort = 0
  tcpC.Connect "192.168.0.1", 8000
End Sub

Private Sub CommandButton1_Click()

 On Error GoTo errHandler
  Dim strData As String

  tcpC.SendData "S" & vbCrLf 
  tcpC.GetData strData
  Text4.Caption = "Weight: " & strData
 Exit Sub

 errHandler:
    MsgBox "error:" & Err.Description
End Sub

Private Sub readScale_OnTimeOut(ByVal lTimerId As Long)
  Dim strData As String

  tcpC.SendData "S" & vbCrLf 
  tcpC.GetData strData
  Text4.Caption = "Weight: " & strData
 Exit Sub
End Sub
Dallas
  • 197
  • 1
  • 3
  • 14