vb串口通讯发送和接受的难处
各位好,现在正在做一程序,vb和西门子plc,s7-300通讯的项目(十六进制)。单独做上位机接受数据时可以正常接收到plc发过来的十六进制信息 ,但是然后续上上位机发送数据代码就不能通讯成功,不知道发送数据应该如何写。协议是:上位机接受40字节。发送25字节。第一位起始位255。中间的数据就忽略不写了。发送和接受时交叉进行的。哪位能给具体提示一下逻辑上需要如何改正啊,现在就感觉不对,但是不知道该修改哪里。谢谢!
具体代码如下
[code=VB][/code]Private Sub MSComPLC_OnComm()
Dim buffer As Variant
Dim InString(0 To PLCRxCount - 1) As Byte
Dim i As Integer
Dim SupplyNum As Integer
Dim CheckInt As Integer
Dim CheckByte As Byte
Dim errTemp As Integer
Select Case MSComPLC.CommEvent
'==================================================================
' 接收数据
'==================================================================
Case comEvReceive
MSComPLC.InputLen = 0
buffer = MSComPLC.Input
For i = 0 To PLCRxCount - 1
InString(i) = buffer(i)
Next i
'==================================================================
' 检查数据完整性/ 计算校验和
'==================================================================
CheckInt = 0
For i = 0 To PLCRxCount - 2
CheckInt = CheckInt + InString(i)
Next i
CheckByte = CheckInt Mod 256
'==================================================================
' 更新上纸台信息,并设置Refresh标志以便更新显示
'==================================================================
If (InString(0) = 255) And (CheckByte = InString(PLCRxCount - 1)) Then
'上纸台可以接受命令
...
...
...(中间程序,可以忽略与本问题无关)
End If
End Select
End Sub
Private Sub TmPLC_Timer()
Dim OutString(0 To PLCTxCount - 1) As Byte
Dim CheckInt As Integer
Dim j As Integer
Dim i As Integer
Dim AndBit As Integer
' 特征字节
OutString(0) = 255
...
...
...(z中间字节,可以忽略,与本问题无关)
' 校验字节,由计算确定
CheckInt = 0
For j = 0 To PLCTxCount - 2
CheckInt = CheckInt + OutString(j)
Next j
OutString(PLCTxCount - 1) = CheckInt Mod 256
Next i
'==========================================================================
' 发送通讯字节内容
'==========================================================================
MSComPLC.InBufferCount = 0
MSComPLC.Output = OutString
End Sub
[解决办法]
学习
[解决办法]
Option Explicit Dim PLCRxCount As LongPrivate Sub Form_Load() TmPLC.Interval = 0 PLCRxCount = 40 MSComPLC.Settings = "9600,n,8,1" MSComPLC.InputMode = comInputModeBinary MSComPLC.RThreshold = PLCRxCount MSComPLC.PortOpen = TrueEnd SubPrivate Sub MSComPLC_OnComm() Dim buffer As Variant Dim InString(0 To 40 - 1) As Byte Dim i As Integer Dim SupplyNum As Integer Dim CheckInt As Integer Dim CheckByte As Byte Dim errTemp As Integer Select Case MSComPLC.CommEvent ' 接收数据 Case comEvReceive MSComPLC.InputLen = 0 buffer = MSComPLC.Input Label1 = UBound(buffer) For i = 0 To PLCRxCount - 1 InString(i) = buffer(i) Next i ' 检查数据完整性/ 计算校验和 CheckInt = 0 For i = 0 To PLCRxCount - 2 CheckInt = CheckInt + InString(i) Next i CheckByte = CheckInt Mod 256 Call TmPLC_Timer ' 更新上纸台信息,并设置Refresh标志以便更新显示 '================================================================== If (InString(0) = 255) And (CheckByte = InString(PLCRxCount - 1)) Then '上纸台可以接受命令 ' ...(中间程序,可以忽略与本问题无关) End If End SelectEnd SubPrivate Sub TmPLC_Timer() Dim OutString(0 To 25 - 1) As Byte Dim CheckInt As Integer Dim j As Integer Dim i As Integer Dim AndBit As Integer ' 特征字节 OutString(0) = 255 '... '... '...(z中间字节,可以忽略,与本问题无关) ' 校验字节,由计算确定 CheckInt = 0 For j = 0 To PLCTxCount - 2 CheckInt = CheckInt + OutString(j) Next j '缺少For 条件 'OutString(PLCTxCount - 1) = CheckInt Mod 256 'Next i '========================================================================== ' 发送通讯字节内容 '========================================================================== MSComPLC.InBufferCount = 0 MSComPLC.Output = OutStringEnd Sub
[解决办法]
更正1个错误:
Private Sub MSComPLC_OnComm() Dim buffer As Variant Dim InString(0 To 40 - 1) As Byte Dim i As Integer Dim SupplyNum As Integer Dim CheckInt As Integer Dim CheckByte As Byte Dim errTemp As Integer Select Case MSComPLC.CommEvent ' 接收数据 Case comEvReceive MSComPLC.InputLen = 0 buffer = MSComPLC.Input Label1 = UBound(buffer) For i = 0 To PLCRxCount - 1 InString(i) = buffer(i) Next i ' 检查数据完整性/ 计算校验和 CheckInt = 0 For i = 0 To PLCRxCount - 2 CheckInt = CheckInt + InString(i) Next i CheckByte = CheckInt Mod 256 ' 更新上纸台信息,并设置Refresh标志以便更新显示 '================================================================== If (InString(0) = 255) And (CheckByte = InString(PLCRxCount - 1)) Then '上纸台可以接受命令 ' ...(中间程序,可以忽略与本问题无关) Call TmPLC_Timer End If End SelectEnd Sub
[解决办法]
鼎云厉害