您现在所在的是:

串口通信

回帖:0个,阅读:1348 [上一页] [1] [下一页]
1289
kuailin
文章数:91
年度积分:50
历史总积分:1289
注册时间:2003/6/21
发站内信
发表于:2003/7/8 18:36:00
#0楼
请教各位。下面是本人写的程序,为何得不到正确的结果???

Private Sub Command1_Click()
  测试PLC是否接收到计算机发送的信息,并返回相应的响应。
Dim inputStr As String
Dim a As String,b As String,c As String,d As String
Dim e As String,f As String,g As String,h As String
Dim i As String,k As String,l As String,m As String
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
DeviceDat = "00FFBW0Y000101"  或者是:DeviceDat = "00FFBW0X000102",
                               DeviceDat = "00FFBW0T000101"等等。
MSComm1.Output = Chr(5) + DeviceDat + SumCheck(DeviceDat) + Chr(13)
TimeDelay 100  时间延迟函数
inputStr = MSComm1.Input
a = Mid(inputStr, 1, 1):b = Mid(inputStr, 2, 1)
c = Mid(inputStr, 3, 1):d = Mid(inputStr, 4, 1)
e = Mid(inputStr, 5, 1):f = Mid(inputStr, 6, 1)
g = Mid(inputStr, 7, 1):h = Mid(inputStr, 8, 1)
i = Mid(inputStr, 9, 1):k = Mid(inputStr, 10, 1)
l = Mid(inputStr, 11, 1):m = Mid(inputStr, 12, 1)
然后用一个文本框显示接收的字符串相应的字符码
Text1.Text = Text1.Text + "  " + CStr(Asc(a)) + "  " + CStr(Asc(b)) + "  " + CStr(Asc(c)) + "  " + CStr(Asc(d)) + "  " + CStr(Asc(e)) + "  " + CStr(Asc(f)) _
  + "  " + CStr(Asc(g)) + "  " + CStr(Asc(h)) + "  " + CStr(Asc(i)) ' + "  " + CStr(Asc(k))   '+ "  " + CStr(asc(l)) + "  " + CStr(asc(m))
但接收文本框的结果是:6  -27243  -27243  -27243  -27243  -27243  -27243  -27243  -27243
从实际运行中观察,当计算机向PLC发送MSComm1.Output 时,PLC的LED灯亮,接收
的字符码里也有6,说明PLC能接收到计算机发来的请求信息,并能响应,但为什么
其他返回的字符码却不正确(如用字符显示是同一个汉字。)


下面代码是如果PLC能正确返回PLC中的Y1点输出,就使相应的SHAPE1控件显红色
。但结果都是出现“返回错误”
'If inputStr = Chr(2) + "00FF1" + Chr(3) Then
'Shape1.FillColor = vbRed
'Else
'MsgBox "返回错误"
'End If


如复我信箱,那就理谢谢了(lj-000@163.net)。

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

31.2002