多种维生素泡腾片禁忌:.NET 串口通讯 .

来源:百度文库 编辑:中财网 时间:2024/05/05 04:19:48

Vs2005自带了一个串口通讯的控件SerialPort,该控件类似于VB6.0的串口Mscomm,但又有一定区别

 

 

1.串口的打开/关闭
 VB6.0串口打开是MSComm1.PortOpen =True,VB2005则直接调用SerialPort1.Open
 VB6.0串口关闭是MSComm1.PortOpen= False,VB2005则直接调用SerialPort1.Close


2.参数的设置对比
 VB串口设置可以单独设置,也可以通过其Setting属性设置如:MSComm1.Settings = "9600,n,8,2"
 而VB2005的串口属性只能分别设置,如:
 端口:SerialPort1.PortName
 波特率:SerialPort1.BaudRate
 数据位长度:SerialPort1.DataBit
 奇偶校验:SerialPort1.Parity
 停止位:SerialPort1.StopBits


3.向串行端口输出缓冲区写数据
 VB6.0向串口写数据是MSComm1.Output方法,其传递的是字符串或数据的起始地址
 VB2005向串口写数据是通过SerialPort1.Write方法,该方法有三种方式:
 (1).直接输出字符串,该方式只有一个参数,string,如:SerialPort1.Write SendString   
 这里面SendString是一个字符串
 (2).以字节方式输出,这种方式有三个参数:
 第一个参数是要输出的字节数据的起始地址
 第二个参数是从字节数据的第几个开始
 第三个参数是要发送的字节个数
 例如:SerialPort1.Write (SendByte,1,10)就是指把字节数组的SendByte的第1到第10个字节发送到输出缓冲区
 (3).以字符方式输出,这种方式也有三个参数:
 第一个参数是要输出的字符数据的起始地址
 第二个参数是从字符数据的第几个开始
 第三个参数是要发送的字符个数
 例如:SerialPort1.Write (SendChar,1,10)就是指把字符数组的SendByte的第1到第10个字符发送到输出缓冲区


4.从串行端口输入缓冲区读数据
 VB6.0从串口读数据是MSComm1.Input方法,其返回的是字符串或一个一维数据
 VB2005从串口读数据是通过SerialPort1.Read方法,该方法有二种方式:
 (1).以字节方式读出,这种方式有三个参数:
 第一个参数buffer,是将输入写入到其中的字节数组
 第二个参数offset,缓冲区数组中开始读出的偏移量,对于从头读的数据,应将其设为1
 第三个参数count,要读取的字节数,如果读当前缓冲区所有数据,则可用其属性SerialPort1.BytesToRead作为参数传递
 这里面SerialPort1.BytesToRead就是所接收的个数
 例如:SerialPort1.Read (ReadByte,1,10)就是指把缓冲区的第1到第10个字节读到ReadByte字节数组中
 (2).以字节方式读出,这种方式有三个参数:

 

 

5、加载本机所有的com

    Sub GetSerialPortNames()
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(sp)
        Next
        If ComboBox1.Items.Count > 0 Then
            ComboBox1.SelectedIndex = 0
        End If
    End Sub

 

6、打开串口

       If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        Try
            With SerialPort1
                .PortName = ComboBox1.Text
                .BaudRate = 9600

                .Parity = IO.Ports.Parity.None '奇偶校验
                .DataBits = 8 '数据位
                .StopBits = IO.Ports.StopBits.One '停止位
            End With
            '打开
            SerialPort1.Open()

            Label1.Text = "Connected"
            Button1.Enabled = False
            Button2.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

 

7、关闭串口

SerialPort1.close()

 

8、发送数据

一般的数据

dim data as string=textbox1.text

SerialPort1.write(data)

十六进制字符串

发送十六进制字符串时,我们用数组保存要发送的信息

比如发送数据为:FF 01 00 00 01 00 00 FE

        Try
            Dim data(8) As Byte
            data(0) = &HFF
            data(1) = &H1
            data(2) = &H0
            data(3) = &H0
            data(4) = &H1
            data(5) = &H0
            data(6) = &H0
            data(7) = &HFE
            SerialPort1.DiscardOutBuffer()
            SerialPort1.Write(data, 0, 8)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

9、接收数据

SerialPort有个DataReceived事件,我们可以在里面写接受数据代码

接受字符串:SerialPort.ReadExisting

接受流数据:

dim byteToRead as int16=SerialPort.bytestoread(读取缓冲区的字节长度)

dim ch(byteToRead) as byte

dim bytesRead as int16=0

bytesRead=SerialPort.read(ch,0,bytetoread)

for i as int16=0 to bytesRead-1

  indata=indata & DecToHex(ch(i))

next

 

indata 就是读取到的数据

自定义函数:DecToHex (返回十六进制字符)

    Public Function DecToHex(ByVal DecNumber As Byte) As String '转换成十六进制字符串
        If DecNumber <= 15 Then
            DecToHex = "  0" & Hex(DecNumber)
        Else : DecToHex = "  " & Hex(DecNumber)
        End If
    End Function