您现在的位置是:首页 > 单片机

简单的VB串口发送程序示例

2020-06-30 05:01:11

'-----发送按钮Click事件子程序-----------
Private Sub Fasong_Click()
Dim JIHAO(0)    As Byte             ’机号
Dim head_data(4) As Byte           ’5 Byte控制字
Dim end_data(0) As Byte            '1 Byte 结束字
    JIHAO(0) = Val(Text3.Text)
    head_data(0) = Val(Text4.Text)
    head_data(2) = &HEE                 'TIMH
    head_data(3) = &HEE                 'TIML
    head_data(4) = Val(Combo1.Text)     'INMOD
    end_data(0) = &HFF
    If Combo2.Text = "增加" Then head_data(1) = &H99
    If Combo2.Text = "清空" Then head_data(1) = &H33
    If Combo2.Text = "删除" Then head_data(1) = &H32
    Ready = 0: ErrCount = 0
    On Error GoTo ERRORCOM              ’打开错误处理
'----------------------------------------------------------
     If com1.Value Then MSComm1.CommPort = 1    'Use com1
     If com2.Value Then MSComm1.CommPort = 2    'Use com2

     MSComm1.SetTIngs = FORM1.Combo3.Text + ",M,8,2"     '设定波特率和置校验和位为1
     MSComm1.InputLen = 0                '
     MSComm1.PortOpen = -1               'Open the port
     MSComm1.OutBufferCount = 0
     MSComm1.Output = JIHAO              ‘发送机号
     MSComm1.PortOpen = False            ’关闭串口
     MSComm1.SetTIngs = FORM1.Combo3.Text + ",S,8,2" '设定波特率和置校验和位为空
     MSComm1.OutBufferCount = 0
     MSComm1.PortOpen = True
     MSComm1.Output = head_data
     MSComm1.Output = Text2.Text
     MSComm1.Output = end_data
     MSComm1.PortOpen = False
     Text1.Text = "发送成功!" + Chr(13) & Chr(10) + "发送至" + Text3.Text + "屏体," + "信息编号:" + Text4.Text + Chr(13) & Chr(10) + Chr(13) & Chr(10) + Text1.Text
     GoTo comend
ERRORCOM:
     Text1.Text = "ERROR!请重新选择COM口!" + Chr(13) & Chr(10) + Chr(13) & Chr(10) + Text1.Text
comend:
     On Error GoTo 0
End Sub