标题:[求助]怎么VB程序里编程直接用USB口通讯
只看楼主
jackiyfan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-9-11
得分:0 
实在非常感谢
2009-09-11 17:06
jackiyfan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-9-11
得分:0 
请问vb编写usb通讯要注意哪些吗?能给我一个大概的一个思路步骤吗? 谢谢啊!
2009-09-11 17:08
li1111
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-7-29
得分:0 
我也来看看学习一下
2010-07-29 10:08
sohu4336
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-10-15
得分:0 
谢谢
2010-10-15 11:12
hzajck
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-24
得分:0 
下载了没看懂
2011-11-24 13:32
Jsrdsdb
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-11-27
得分:0 
系统USB口是通过USB设备转成串口,所以你只用MSCOMM控制就行了
2011-11-27 08:10
Jsrdsdb
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-11-27
得分:0 
就是VB编写串口通讯,以下程序可以参考.我也是看它做的
1.    Option Explicit
2.        Dim a As Integer
3.        Dim BytReceived() As Byte
4.        Dim strData As String
5.        Dim lenInput As Integer
6.        Dim bytSendByte() As Byte    '发送二进制数据
7.        Dim strSendText As String    '发送文本数据
8.        Dim blnAutoSendFlag As Boolean
9.        Dim openFlag As Boolean
10.   
11.    Private Sub cmdOpen_Click() '打开关闭端口
12.        If openFlag Then
13.            cmdOpen.Caption = "打开串口"
14.            MSComm1.PortOpen = False '打开端口
15.            Shape1.FillColor = vbRed
16.            Label1 = "COM" & a & " 关闭"
17.        Else
18.            OpenPortNum
19.            cmdOpen.Caption = "关闭串口"
20.            Shape1.FillColor = vbGreen
21.            Label1 = "COM" & a & " 打开"
22.        End If
23.        openFlag = Not openFlag
24.    End Sub
25.   
26.    Private Sub cmdSendASc_Click() '文本发送
27.        Dim sj_Txt As String
28.        sj_Txt = TxtSend
29.        If MSComm1.PortOpen = True Then
30.            MSComm1.Output = sj_Txt
31.        End If
32.    End Sub
33.   
34.    Private Sub cmdSendHex_Click() '16进制发送
35.        Dim sj() As Byte
36.        Dim sj_Txt As String
37.        Dim i As Integer
38.        sj_Txt = TxtSend
39.        If Len(TxtSend) Mod 2 = 0 And Len(TxtSend) <> 0 Then '检验16进制字符串长
40.            ReDim sj(Len(sj_Txt) / 2 - 1)
41.            For i = 0 To Len(sj_Txt) - 1 Step 2
42.               sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
43.            Next
44.            If MSComm1.PortOpen = True Then
45.                MSComm1.Output = sj
46.            Else
47.                MSComm1.PortOpen = True
48.                Shape1.FillColor = vbGreen
49.                MSComm1.Output = sj
50.            End If
51.        Else
52.            MsgBox ("格式不对!")
53.        End If
54.    End Sub
55.    '字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
56.   
57.    Private Sub OpenPortNum()
58.        On Error GoTo uerror
59.        For a = 1 To 16
60.             = a
61.            MSComm1.PortOpen = True
62.            If MSComm1.PortOpen = True Then
63.                'Print "可用Com号= "; a
64.                Exit Sub
65.            Else
66.            End If
67.        Next
68.        Exit Sub
69.    uerror:
70.        'Print "出错或占用Com号= "; a
71.        Resume Next
72.    End Sub
73.   
74.    Private Sub Form_Load()
75.        MSComm1.Settings = "9600,n,8,1"
76.        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
77.        MSComm1.InBufferCount = 0   '清空接受缓冲区
78.        MSComm1.OutBufferCount = 0  '清空传输缓冲区
79.        MSComm1.RThreshold = 1      '产生MSComm事件
80.        MSComm1.InBufferSize = 1024
81.        TxtSend = ""
82.        TxtSend = "" '800A00113135323634389794"
83.        txtReceive = ""
84.        Text2 = ""
85.        Call cmdOpen_Click
86.    End Sub
87.   
88.    Private Sub MSComm1_OnComm() '接收数据
89.        Dim strBuff As String
90.        Select Case
91.            Case 2
92.                MSComm1.InputLen = 0
93.                strBuff = MSComm1.Input
94.                BytReceived() = strBuff
95.                jieshou
96.                lenInput = Len(strData)
97.                Text2 = lenInput \ 2
98.                '数据处理代码
99.        End Select
100.    End Sub
101.   
102.    Public Function jieshou() '接收数据处理为16进制
103.        Dim i As Integer
104.        For i = 0 To UBound(BytReceived)
105.            If Len(Hex(BytReceived(i))) = 1 Then
106.                strData = strData & "0" & Hex(BytReceived(i))
107.            Else
108.                strData = strData & Hex(BytReceived(i))
109.            End If
110.        Next
111.        txtReceive = strData
112.    End Function
2012-01-07 01:17
mylinden
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-11-20
得分:0 
那位成功了?能不能提供个实例?
2014-11-20 09:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-120817-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.071951 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved