首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb关于串口发送和接受数据的有关问题

2012-01-02 
vb关于串口发送和接受数据的问题各位好,现在正在做一程序,vb和西门子plc,s7-300通讯的项目。上位机接受数据

vb关于串口发送和接受数据的问题
各位好,现在正在做一程序,vb和西门子plc,s7-300通讯的项目。上位机接受数据成功了 ,然后不知道发送数据应该如何写。协议是:上位机接受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
  
 ' On Error GoTo Err
  
  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
  '上纸台可以接受命令
  If InString(1) And 1 Then
  Supply(1).RightReady = True
  Else
  Supply(1).RightReady = False
  End If

End Select
  Exit Sub
Err:
  LblError.Caption = "MsComPLC:" & Err.Description
  Resume Next
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
  '自动发纸命令
  AndBit = 1
  For i = 1 To 3
  If Supply(i).RightTransmit Then
  OutString(1) = OutString(1) + AndBit
  ElseIf Supply(i).LeftTransmit Then
  OutString(1) = OutString(1) + AndBit * 2
  End If
  AndBit = AndBit * 4
  Next i
  AndBit = 1
  For i = 4 To SUPPLY_NUMBER
  If Supply(i).RightTransmit Then
  OutString(2) = OutString(2) + AndBit
  ElseIf Supply(i).LeftTransmit Then
  OutString(2) = OutString(2) + AndBit * 2
  End If
  AndBit = AndBit * 4
  Next i

  ' 急停与复位
  AndBit = 1
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).Stop Then OutString(3) = OutString(3) + AndBit
  AndBit = AndBit * 2
  Next i
  '手动命令
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).mode = MANUAL_MODE Then
  OutString(3 + i) = Supply(i).Order
  Else
  OutString(3 + i) = 0
  End If
  Next i
   
  '自动模式
  AndBit = 1
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).mode = AUTO Then OutString(10) = OutString(10) + AndBit
  AndBit = AndBit * 2


  Next i
  '手动模式
  AndBit = 1
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).mode = MANUAL_MODE Then OutString(11) = OutString(11) + AndBit
  AndBit = AndBit * 2
  Next i
  '故障模式
  AndBit = 1
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).mode = ERROR_MODE Then OutString(12) = OutString(12) + AndBit
  AndBit = AndBit * 2
  Next i
  '故障恢复信号
  AndBit = 1
  For i = 1 To SUPPLY_NUMBER
  If Supply(i).Reset Then OutString(13) = OutString(13) + AndBit
  AndBit = AndBit * 2
  Next i
   
  ' 校验字节,由计算确定
  CheckInt = 0
  For j = 0 To PLCTxCount - 2
  CheckInt = CheckInt + OutString(j)
  Next j
  OutString(PLCTxCount - 1) = CheckInt Mod 256
   
  Labout.Caption = ""
  For i = 0 To PLCTxCount - 1
  Labout.Caption = Labout.Caption + " " + CStr(OutString(i))
  Next i
   
  '==========================================================================
  ' 发送通讯字节内容
  '==========================================================================
  MSComPLC.InBufferCount = 0
  MSComPLC.Output = OutString
   

End Sub
但是发送就是不通,是不是在timer里面写发送的代码啊?还请各位指教一下

[解决办法]

VB code
Option Explicit    Dim OutString() As BytePrivate Sub Form_Load()    MSComPLC.CommPort = 1    MSComPLC.Settings = "9600,n,8,1"    MSComPLC.PortOpen = True    TmPLC.Interval = 1000End SubPrivate Sub TmPLC_Timer()    ReDim OutString(1 To 25)    OutString(1) = &HFF    '写OutString数组的其它代码    MSComPLC.Output = OutStringEnd Sub 

热点排行