VB40006错误或者40020错误

wono01 2014-04-17 09:24:13

毕设基于以太网的蓝牙多点测温系统,stm32做服务器,将采集的温度通过以太网送到PC端,PC做客户端,可以通过以太网设置温度,保存成excel和进行折线图的绘制,但是由于能力问题用起来失灵时不灵,会出现40006和40020错误和10048错误,求大神指教
Private Sub Command1_Click() ‘连接按键
If tcpClient.State <> 0 Then
tcpClient.Close
End If
tcpClient.Connect


Timer1.Enabled = True ’开启定时器,定时发送POST采集温度

Command1.Enabled = False ’连接按键失能,防止误操作
End Sub

Private Sub Command2_Click() ‘保存成excel表格
Timer1.Enabled = False
Dim i
Dim j As String
Dim at As String
Dim bt As String
Dim Ti As String

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\lenovo\Desktop\123.xls")

Set xlSheet = xlBook.Worksheets(1) '引用第1张工作表As Long
xlSheet.Range("A1") = "序号"
xlSheet.Range("B1") = "A点温度"
xlSheet.Range("C1") = "B点温度"
xlSheet.Range("D1") = "请求发出时间"
xlSheet.Range("D1") = "收到回复时间"
For i = 1 To ListView1.ListItems.Count
j = "A" + CStr(i + 1)
at = "B" + CStr(i + 1)
bt = "C" + CStr(i + 1)
Ti = "D" + CStr(i + 1)
xlSheet.Range(j) = i '可使用Cells属性进行赋值
xlSheet.Range(at) = ListView1.ListItems(i).SubItems(1)
xlSheet.Range(bt) = ListView1.ListItems(i).SubItems(2)
xlSheet.Range(Ti) = ListView1.ListItems(i).SubItems(3)
Next

xlApp.Application.SaveWorkspace '保存
xlApp.Application.Quit '关闭对象
Set xlApp = Nothing '释放引用
Timer1.Enabled = True
End Sub

Private Sub Command3_Click()
'tcpClient.SendData "A点" + Text1.Text
Shape1.FillColor = RGB(255, 0, 0)

End Sub

Private Sub Command4_Click()
tcpClient.SendData "B点" + Text2.Text
End Sub

Private Sub Command5_Click()
Timer1.Enabled = False
Form2.Visible = True
End Sub

Private Sub Form_Load() ‘初始化服务器IP,端口和listview

tcpClient.RemoteHost = "192.168.1.18"
tcpClient.RemotePort = 2060
tcpClient.LocalPort = Rnd ’网上说端口被占用的原因,所以加了个这个还是不行
Text3.Text = 0
Command5.Enabled = False
Text4.Text = ""
ListView1.ListItems.Clear '清空列表
ListView1.ColumnHeaders.Clear '清空列表头
ListView1.View = lvwReport '设置列表显示方式
ListView1.Gridlines = True '显示网络线
ListView1.LabelEdit = lvwManual '禁止标签编辑
ListView1.FullRowSelect = True '选择整行

ListView1.ColumnHeaders.Add , , "序号", 600
ListView1.ColumnHeaders.Add , , "A点温度", 1200
ListView1.ColumnHeaders.Add , , "B点温度", 1200
ListView1.ColumnHeaders.Add , , "请求发出时间", 1500
ListView1.ColumnHeaders.Add , , "回复到达时间", 1500

End Sub



Private Sub Form_Unload(Cancel As Integer)

If tcpClient.State <> 0 Then ‘退出时关闭winsock
tcpClient.Close
End If

End Sub

'添加数据到listview1中

Private Sub tcpClient_DataArrival _
(ByVal bytesTotal As Long)


Dim strData As String

tcpClient.GetData strData
Text5.Text = strData

Dim X
X = ListView1.ListItems.Count + 1
ListView1.ListItems.Add , , X
ListView1.ListItems(X).SubItems(1) = Mid(Text5.Text, 3, 2) & "." & Mid(Text5.Text, 5, 2)
ListView1.ListItems(X).SubItems(2) = Mid(Text5.Text, 9, 2) & "." & Right(Text5.Text, 2)
ListView1.ListItems(X).SubItems(3) = Hour(Time) & ":" & Minute(Time) & ":" & Second(Time)
ListView1.ListItems(X).SubItems(4) = Text4.Text

Text3.Text = Text3.Text + 1 ’当收到十个以上的数据时绘图功能开启,技术渣只能通过text3来做全局变量
If Text3.Text > 10 Then
Command5.Enabled = True
End If
End Sub



Private Sub Text1_KeyPress(KeyAscii As Integer) ‘温度设置的过滤,保证输入的是数字
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox "请输入整数!", 26, "警告"
KeyAscii = 0
Text1.Text = ""
End If

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer) ‘温度设置的过滤,保证输入的是数字
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox "请输入整数!", 26, "警告"
KeyAscii = 0
Text2.Text = ""
End If
End Sub


Private Sub Timer1_Timer() ’定时发送POST字符,Text4用于记录发送时间,技术渣

tcpClient.SendData "POST"
Text4.Text = Hour(Time) & ":" & Minute(Time) & ":" & Second(Time)
End Sub
代码就这些,求大神帮忙
...全文
599 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
王二.麻子 2014-05-08
  • 打赏
  • 举报
回复
sckBadState 40006 所请求的事务或请求本身的错误协议或者错误连接状态。 sckInvalidOp 40020 在当前状态下的无效操作 网络连接状态是不正常的。发送数据的时候检查网络状态,是否连接服务器。如果不是正常连接状态就排除故障,重新连接。 tcpClient.LocalPort = Rnd Rnd 函数 返回一个包含随机数值的 Single。 语法 Rnd[(number)] 可选的 number 参数是 Single 或任何有效的数值表达式。 返回值 如果 number 的值是 Rnd 生成 小于 0 每次都使用 number 作为随机数种子得到的相同结果。 大于 0 序列中的下一个随机数。 等于 0 最近生成的数。 省略 序列中的下一个随机数。 返回的是0-1的单精度浮点数,赋值给本地端口总是被转换为0,TCP端口0不应该被你用到。 本地端口可以不赋值,由系统自己赋值,一定要赋值的话,分配更高的值,比如大于1000,不过4000附近经常呗qq占用。 tcpClient.LocalPort = rnd*50000+10000‘赋值为10000-60000的一个随机数。

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧