当前位置:编程学习 > VB >>

用VB 如何进行并口通信?

想做一蓝牙地址自加的界面后写入蓝牙芯片中,写入成功后界面中的地址自动加一,继续下一个IC的地址输入.

打算用VB做界面,将起始地址输入到界面内,单击"输入"按键,则蓝牙地址通过计算机主机的并口输出,输出到并口转换板(型号为IDS-1309C,淘宝上买的 http://item.taobao.com/item.htm?id=4456008392)上在通过SPI写入蓝牙IC中.

问题1:如何将界面中的数据(既蓝牙地址)输出到并口(即VB如何进行并口通信)?

问题2:估计是要通过计算机并口模拟SPI通信,25PIN并口如何模拟SPI通信?大致思路是怎样? --------------------编程问答-------------------- 比串口麻烦一些,参考。 --------------------编程问答-------------------- 参考这里 --------------------编程问答-------------------- 使用并口一般需要使用驱动级的动态链接库,比如WinIO.dll不过都比较复杂。楼上两位已经给出了例子,我就不再献丑了。 --------------------编程问答-------------------- 非常感谢,一定认真参考。
--------------------编程问答-------------------- WINIO那个貌似WIN9X下面的东东了. --------------------编程问答-------------------- 比串口麻烦一些 --------------------编程问答--------------------
引用 5 楼 wallescai 的回复:
WINIO那个貌似WIN9X下面的东东了.

XP下是能使用的
http://download.csdn.net/source/2321502 --------------------编程问答--------------------
这个事情我干过。

1 到网上下载一个 Dllport.dll 之类的库。里面有两个函数

Public Declare Function Inp Lib "DLLPORT.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Byte

Public Declare Sub Out Lib "DLLPORT.dll" Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Byte)

2 如下连接并口和 SPI 线:
'并口引脚编号    并口引脚名称    寄存器地址  数据位  对应SPI引脚
'11             状态Busy        0x379       Bit 7   MISO
'2              数据Data0       0x378       Bit 0   MOSI
'1              控制Strobe      0x37A       Bit 0   SCK
'9              数据Data7       0x378       Bit 7   /SS
'25             地                                  GND --GND

3 用 VB 控制 /SS 和时钟线的同时,读写数据线。
--------------------编程问答--------------------
引用 8 楼 of123 的回复:
这个事情我干过。

1 到网上下载一个 Dllport.dll 之类的库。里面有两个函数

Public Declare Function Inp Lib "DLLPORT.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Byte

Public Declare Sub Out Lib "DLLPORT.dll" Alias "……

这个新鲜,能否具体些? --------------------编程问答-------------------- 能否具体一点,不会用呢! --------------------编程问答-------------------- 这么老的帖子能翻出来。
下面是两个主要的模块代码:

'Declare Inp and Out for port I/O
Public Declare Function Inp Lib "DLLPORT.dll" _
Alias "Inp32" (ByVal PortAddress As Integer) As Byte
Public Declare Sub Out Lib "DLLPORT.dll" _
Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Byte)


Option Explicit

Public SCKActive As Long
'As Clock, the /Strobe is hardware inverted
Public Const SCKHighActive As Long = 0
Public Const SCKLowActive As Long = 1

Public SCKPhase As Long
Public Const SCKPhase1st As Long = 0
Public Const SCKPhase2nd As Long = 1

Public SCKFrequency As Long

Public BitSequence As Long
Public Const MSBFirst As Long = 0
Public Const LSBFirst As Long = 1

Public SCKHalf As Long
Public SampleFirst As Long
Public SampleDelta As Long

Const SSInactive As Byte = &H80
Const SSActive As Byte = &H0

Const DataPort As Integer = &H378
Const StatePort As Integer = &H379
Const ControlPort As Integer = &H37A

Dim ByteToSend As Byte
Dim Sample1 As Byte
Dim Sample2 As Byte
Dim Sample3 As Byte
Dim ByteReceived As Byte
Dim i As Integer

Public Sub SCKDelay(ByVal X As Long)
    QueryPerformanceCounter T1
    Do
        QueryPerformanceCounter T2
    Loop Until (T2.lowpart - T1.lowpart) >= X
End Sub

Public Sub SendByte(ByVal a As Byte)

    ByteToSend = a
    'set clock to inactive
    Out ControlPort, SCKActive Xor 1
    
    Out DataPort, SSActive
    
    'If SCKPHA = 1 then delay a half of a clock cycle
    If SCKPhase = SCKPhase2nd Then SCKDelay SCKHalf
    
    For i = 1 To 8
        'data bit change
        If BitSequence = MSBFirst Then
            If (ByteToSend And &H80) > 0 Then
                Out DataPort, SSActive Or 1
            Else
                Out DataPort, SSActive
            End If
            ByteToSend = ((ByteToSend And &H7F) * 2)
        Else
            If ByteToSend And &H1 > 0 Then
                Out DataPort, SSActive Or 1
            Else
                Out DataPort, SSActive
            End If
            ByteToSend = ByteToSend \ 2
        End If
        
        'Switching clock
        If SCKPhase = SCKPhase2nd Then
            Out ControlPort, SCKActive
        Else
            Out ControlPort, (SCKActive Xor 1)
        End If
        
        SCKDelay SCKHalf
        
        'Switching clock
        If SCKPhase = SCKPhase1st Then
            Out ControlPort, SCKActive
        Else
            Out ControlPort, (SCKActive Xor 1)
        End If
        
        SCKDelay SCKHalf
        
    Next i
    
    'If SCKPHA = 0 then switch clock to inactive and delay a half of a clock cycle
    If SCKPhase = SCKPhase1st Then
        Out ControlPort, SCKActive
        SCKDelay SCKHalf
    End If
    
    Out DataPort, SSInactive
    SCKDelay SCKHalf
End Sub

Public Function RecvByte() As Byte
    'set clock to inactive
    Out ControlPort, SCKActive Xor 1
    
    Out DataPort, SSActive
    
    'If SCKPHA = 1 then delay a half of a clock cycle
    If SCKPhase = SCKPhase2nd Then SCKDelay SCKHalf
    
    ByteReceived = 0
    
    For i = 1 To 8
        
        'Switching clock
        If SCKPhase = SCKPhase2nd Then
            Out ControlPort, SCKActive
        Else
            Out ControlPort, (SCKActive Xor 1)
        End If
        
        'Sample triple
        SCKDelay SampleFirst
        Sample1 = (Inp(StatePort) And &H80) / 128

        SCKDelay SampleDelta
        Sample2 = (Inp(StatePort) And &H80) / 128

        SCKDelay SampleDelta
        Sample3 = (Inp(StatePort) And &H80) / 128
        
        Sample1 = Sample1 + Sample2 + Sample3
        
        'Get a bit
        
        If BitSequence = MSBFirst Then
            ByteReceived = (ByteReceived And &H7F) * 2
            If Sample1 < 2 Then ByteReceived = ByteReceived Or 1
        Else
            ByteReceived = ByteReceived \ 2
            If Sample1 < 2 Then ByteReceived = ByteReceived Or &H80
        End If
        
        'Switching clock
        If SCKPhase = SCKPhase1st Then
            Out ControlPort, SCKActive
        Else
            Out ControlPort, (SCKActive Xor 1)
        End If
        
        SCKDelay SCKHalf
        
    Next i
    
    'If SCKPHA = 0 then switch clock to inactive and delay a half of a clock cycle
    If SCKPhase = SCKPhase1st Then
        Out ControlPort, SCKActive
        SCKDelay SCKHalf
    End If
    
    Out DataPort, SSInactive
    SCKDelay SCKHalf
    
    RecvByte = ByteReceived
End Function
--------------------编程问答-------------------- 哦,还缺一个定时模块:

Option Explicit

Public Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

Public Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long


Public Type LARGE_INTEGER
    lowpart As Long
    highpart As Long
End Type


Public T1 As LARGE_INTEGER, T2 As LARGE_INTEGER
Public SYSFrequency As LARGE_INTEGER
'Public tt As Single, totalTime As Single, sngTime As Single

Public TickPerCycle As Single
'Public Overhead As Single

Public Declare Function GetTickCount Lib "kernel32" () As Long
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,