我心依旧 2020年03月16日
C#通过Socket 读取托利多电子重量



Dim mWeight(1 To 1) As Long

Dim mNeedWeight(1 To 1) As Boolean


Dim mlblShowText(1 To 1) As String
'=======第一跨Winsock监听计数
Dim WinSockCount(1 To 1) As Integer




Private Sub Form_Load()
Dim I As Integer

'=======第一跨
Me.Winsock_W(1).Listen

'
mlblShowText(1) = "L1_2 "

End Sub


'20120604 Add Start
'Private Sub GetWinSockt(Index As Integer)
' Dim strRetVal As String
'
' If Me.Winsock_W(Index).State = 7 Then
' Me.Winsock_W(Index).GetData strRetVal
' If Len(strRetVal) > 0 Then
' If IsNumeric(Trim(Mid(strRetVal, 5, 6))) Then
' mWeight(Index) = strRetVal 'Int(Trim(Mid(strRetVal, 5, 6)))
' End If
' End If
' End If
' Debug.Print mWeight(Index)
'
'End Sub



Private Sub Timer1_Timer()
Timer1.Interval = 0

For I = 1 To 1
WinSockCount(I) = WinSockCount(I) + 1
If WinSockCount(I) > 100 Then
WinSockCount(I) = 100
End If


If Me.Winsock_W(I).State = 7 And WinSockCount(I) < 10 Then
ShapeStatus(I).FillColor = RGB(0, 255, 0)
Else
ShapeStatus(I).FillColor = RGB(255, 0, 0)

If Me.Winsock_W(I).State <> 0 And Me.Winsock_W(I).State <> 2 Then

Me.Winsock_W(I).Close
Me.Winsock_W(I).Listen
End If
End If
Next

Timer1.Interval = 1000
End Sub

Private Sub Winsock_W_Connect(Index As Integer)
Debug.Print Me.Winsock_W(Index).State
End Sub


Private Sub Winsock_W_ConnectionRequest(Index As Integer, ByVal requestID As Long)
' Me.Winsock_W(Index).Accept requestID
' Debug.Print Me.Winsock_W(Index).State
' Debug.Print requestID

If Me.Winsock_W(Index).State <> 0 Then
Me.Winsock_W(Index).Close
End If
Me.Winsock_W(Index).Accept requestID

End Sub

Private Sub Winsock_W_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strRetVal As String

If bytesTotal > 0 Then
WinSockCount(Index) = 0
If Me.Winsock_W(Index).State = 7 Then
Me.Winsock_W(Index).GetData strRetVal
' If Len(strRetVal) > 0 Then 'And mNeedWeight(Index) = True
' If IsNumeric(Trim(Mid(strRetVal, 5, 6))) Then
' mWeight(Index) = Int(Trim(Mid(strRetVal, 5, 6)))
'
' lblShow(Index).Caption = mlblShowText(Index) & mWeight(Index) & " " & Now()
lblShow(Index).Caption = strRetVal ' "" & mWeight(Index) & " " & Now()
' If mWeight(Index) > 0 And mNeedWeight(Index) = True Then
' SetWeight mWeight(Index), Index
' End If
' End If
' End If
End If
End If
End Sub


'Private Sub lblShow_Click(Index As Integer)
' If Me.Winsock_W(Index).State <> 0 And Me.Winsock_W(Index).State <> 7 Then
' Me.Winsock_W(Index).Close
' Me.Winsock_W(Index).Listen
' End If
'End Sub



'Private Sub lblShow_DblClick(Index As Integer)
'' If Me.Winsock_W(Index).State <> 0 Then
'' Me.Winsock_W(Index).Close
'' End If
'' Me.Winsock_W(Index).Connect
' If Me.Winsock_W(Index).State <> 0 And Me.Winsock_W(Index).State <> 7 Then
' Me.Winsock_W(Index).Close
' Me.Winsock_W(Index).Listen
' End If
'End Sub




以上这部分是用Visual Basic 读取托利多电子秤重量的,用的是Socket 网口方式。

然后我在转化成C#的时候,提示我连接被拒绝。

有没有高手看看怎么把这段VB的代码弄成C#的,并且正常工作。

谢谢谢谢谢谢。
...全文
160 点赞 收藏 3
写回复
3 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告