【原创】wince bluebooth设备编程札记

来源:互联网 发布:python 定时发送邮件 编辑:程序博客网 时间:2024/05/20 18:51

【原创】wince bluebooth设备编程札记

这里写图片描述

Public Shared Function OpenBlueBoothSerialPort() As String        Dim addr As BluetoothAddress = BluetoothAddress.Parse("002233445566")        Dim sqlstr As String        Dim DRow As System.Data.DataRow = Nothing        Try            Dim br As Microsoft.WindowsMobile.SharedSource.Bluetooth.BluetoothRadio = New Microsoft.WindowsMobile.SharedSource.Bluetooth.BluetoothRadio()            '            '如果蓝牙设备不可见,则设置为可见状态,如果蓝牙设备被关闭会自动打开            '            If br.BluetoothRadioMode <> BluetoothRadioMode.Discoverable Then                br.BluetoothRadioMode = BluetoothRadioMode.Discoverable            End If            BlueSelect.ShowDialog()  '选择蓝牙设备            Dim endPoint As BluetoothEndPoint = New BluetoothEndPoint(addr, StandardServices.SerialPortServiceGuid)            Dim port As BluetoothSerialPort = BluetoothSerialPort.CreateClient(endPoint)   '创建串口端点            Return port.PortName        Catch e As Exception        End Try    End Function.................以下为对串口的一般操作            Dim PortName As String            If Frmmain.cmbComMode.Text.Contains("蓝牙") Then                PortName = BlueBooth.OpenBlueBoothSerialPort()                If String.IsNullOrEmpty(PortName) Then                    MessageBox.Show("选择端口有误!")                    Application.Exit()                    Exit Function                End If            Else                PortName = DRow(4)            End If            Serial.CurrentSerialPort = Frmmain.SerialPort1            Serial.SendMode = Serial.SM_Frame            Serial.DataMode = Serial.DM_Asc            Serial.PortName = PortName            Serial.BaudRate = CInt(DRow(2))            Serial.Parity = DRow(1)            Serial.open().......

上面是蓝牙模拟串口的全部流程,使用中发现使用虚拟串口模式连接蓝牙设备时,运行一段时间总会突然丢失串口句柄。提示IO异常或无法找到串口。经测试怀疑为本方法库本身软件问题。

同时,试验中发现,使用蓝牙虚拟串口模式时,如果第一次创建虚拟串口成功时,第二次创建会失败,只有重新启动系统才能保证再次创建串口成功。一般使用方法第一次创建成功后记下串口名称,以后直接打开对应名称的串口设备即可。直到再次重启后才能再次创建成功。

0 0
原创粉丝点击