『关闭窗口』
 
一个简单的VB串口发送程序(源码)!
 '-----发送按钮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                                            

解决方案 »

  1.   

    http://www.yesky.com/20020516/1611460.shtmlhttp://it.enorth.com.cn/system/2001/02/21/000007750.shtml
      

  2.   

    我这没有测试环境,所以未测试,但基本上是这么个意思
    proceddure btnSendClick(Sender: TOBject);
    var
      DevNo: Char;
      Buffer: Array of Char;
    begin
      DevNo := Char(StrToInt(Text3.Text));
      SetLength(Buffer, 6);
      Buffer[0] := Char(StrToInt(Text4.Text));
      Case ComboBox2.ItemIndex of
        0: Buffer[1] := #39;
        1: Buffer[1] := #33;
        2: Buffer[1] := #32;
      end;
      Buffer[2] := #ee;
      Buffer[3] := #ee;
      Buffer[4] := Char(StrToInt(ComboBox1.Text));
      Buffer[5] := #ff;
      Try
        MSComm1.CommPort := 1;
        MSComm1.Settings := ComboBox3.Text + ',M,8,2'
        MSComm1.InputLen := 0;
        MSComm1.PortOpen := true;
        MSComm1.OutBufferCount := 0;
        MSComm1.OutPut := DevNo;
        MSComm1.PortOpen := False;
        MSComm1.Settings := ComboBox3.Text + ',S,8,2'
        MSComm1.OutBufferCount := 0
        MSComm1.PortOpen := True
        MSComm1.Output := Buffer;
        MSComm1.PortOpen := False;
        Text1.Text = '发送成功!'#13'发送至'#13'屏体,信息编号:" + Text4.Text + #13 + #13 + Text1.Text;
      Except
         Text1.Text = 'ERROR!请重新选择COM口!' + #13 + #13 + Text1.Text
      end;
    end;
      

  3.   

    谢谢,那怎么再把数据写到ACCESS里呢
      

  4.   

    和平常写入数据库一样,只不过数据要从字符数组转换为其它类型,转换为字符型可以用
    StrPas(Buffer),不过不是这么简单,要先将Buffer按协议拆分开来再进行转换,主要还是看你的协议是怎么定的