条码枪有三种接口:PS2, COM, USB。问题:如何用这三个接口读取条码数据?能否给出示范代码。如果用PS2口,是否就完全等于键盘输入,无需程序处理?

解决方案 »

  1.   

    数据采集器(手持式数据采集器)通过通讯接口设备连接到计算机的COM1将数据传入计算机。供货商提供了一个动态链接库PlatW95.dll。该动态连接库用于数据的接收和发送。在使用之前需要声明如下:
    Private Declare Function ReceiveYModemDll Lib "C:\XXX\PlatW95.dll" (ByVal s1$, ByVal s2$, ByVal s3$, ByVal s4%) As Long
    其中ReceiveYModemDll为接收数据时使用的函数的函数名。该函数共有4个字符型的参数。其中第一个参数用来设定通讯接口、传输速率等;第二个参数用于设置数据读入后数据文件的存放路径,第三个参数设定文件名,在这里使用“空”文件名,当数据读入后该函数自动将文件名设置为它的默认文件名“source.dbf”;最后一个参数为与设备握手前的等待时间的秒数值, 从 1 ~30。数据采集器中的数据以.dbf文件的形式存入硬盘。
      
    在窗体装入时给变量赋初值
        Fname = Space$(15)
    需要读入数据时按下面的方式调用:
        Ret = ReceiveYModemDll("COM1:19200,n,8,1", App.Path + "\", Fname, 6)
    这个函数的返回值仅反映数据采集器在数据传输时的工作状态,根据它的返回值可以确定数据传输是否成功。    返回值为整数     0 : 成功
                         1 : 传递给函数的参数错误
                         2 : 串口无法使用
                         3 : 文件打不开
                         4 : 文件读取错误
                         5 : 文件写入错误
                         6 : 设备未就绪
                         7 : 校验和错误
                        10 : 对方应答错误
                        11 : 文件长度不一致
                        12 : 用户 Esc 退出
                        13 : 用户 鼠标右键 退出除了这个数据采集器外,有些读卡器也提供了类似的动态链接库。现在使用的这个动态链接库有个小小的不足,即不能在注册表中注册。
    其他详细情况可参见姚巍编著《VB数据库开发及工程实例》一书第六章。
        祝好!
      

  2.   

    to xqw001:尼的回答2:我的意思不是问是否要软件,而是问我自己的程序是否需要修改,以适应不同接口(ps2,usb,com)的变化?
      

  3.   

    顺便向你请教个问题,我在用vc做个五笔考试的软件,遇到几个问题:
       1、我的view是继承的CView, 请问如何在view上显示我从键盘输入法的汉字
       2、我如何在view上取某一位置的汉字
      

  4.   

    我只做COM接口的,只要用控件MSCOMM就可以完全控制了.
      

  5.   

    XQW001,你用的是VB写的吧,可不可借我看一看 [email protected]
      

  6.   

    lang8848  看什么?条码?
      

  7.   

    xqw001,
    1.sorry,我不会VC
    2。能发一份打印条码和读条码的代码到[email protected]么?多谢啦
      

  8.   

    读条码不需要程序
    打印程序:
    Function PrintTiaoMa(strCode As String, xpos As Integer, ypos As Integer)
    '注释:  参数说明:
    '注释:  strBarCode -要打印的条形码字符串
    '注释: intXPos, intYPos - 打印条形码的左上角坐标(缺省为(0,0),坐标刻度为:毫米)
    '注释: intHeight     - 打印高度(缺省为一厘米,坐标刻度为:毫米)
    '注释:  bolPrintText -是否打印人工识别字符(缺省为true)'注释: "0"-"9","A-Z","-","%","$"和"*" 的条码编码格式,总共 40 个字符
    Dim strBarCode As String
    Dim intXPos As Integer
    Dim intYPos As Integer
    Dim intPrintHeight As Integer
    Dim bolPrintText As Boolean
    strBarCode = strCode
    intXPos = xpos
    intYPos = ypos
    intPrintHeight = 11
    bolPrintText = True
    Static strBarTable(39) As String'注释:  初始化条码编码格式表
        strBarTable(0) = "001100100"     '注释: 0
        strBarTable(1) = "100010100"     '注释: 1
        strBarTable(2) = "010010100"     '注释: 2
        strBarTable(3) = "110000100"     '注释: 3
        strBarTable(4) = "001010100"     '注释: 4
        strBarTable(5) = "101000100"     '注释: 5
        strBarTable(6) = "011000100"     '注释: 6
        strBarTable(7) = "000110100"     '注释: 7
        strBarTable(8) = "100100100"     '注释: 8
        strBarTable(9) = "010100100"     '注释: 9
        strBarTable(10) = "100010010"    '注释: A
        strBarTable(11) = "010010010"    '注释: B
        strBarTable(12) = "110000010"    '注释: C
        strBarTable(13) = "001010010"    '注释: D
        strBarTable(14) = "101000010"    '注释: E
        strBarTable(15) = "011000010"    '注释: F
        strBarTable(16) = "000110010"    '注释: G
        strBarTable(17) = "100100010"    '注释: H
        strBarTable(18) = "010100010"    '注释: I
        strBarTable(19) = "001100010"    '注释: J
        strBarTable(20) = "100010001"    '注释: K
        strBarTable(21) = "010010001"    '注释: L
        strBarTable(22) = "110000001"    '注释: M
        strBarTable(23) = "001010001"    '注释: N
        strBarTable(24) = "101000001"    '注释: O
        strBarTable(25) = "011000001"    '注释: P
        strBarTable(26) = "000110001"    '注释: Q
        strBarTable(27) = "100100001"    '注释: R
        strBarTable(28) = "010100001"    '注释: S
        strBarTable(29) = "001100001"    '注释: T
        strBarTable(30) = "100011000"    '注释: U
        strBarTable(31) = "010011000"    '注释: V
        strBarTable(32) = "110001000"    '注释: W
        strBarTable(33) = "001011000"    '注释: X
        strBarTable(34) = "101001000"    '注释: Y
        strBarTable(35) = "011001000"    '注释: Z
        strBarTable(36) = "000111000"    '注释: -
        strBarTable(37) = "100101000"    '注释: %
        strBarTable(38) = "010101000"    '注释: $
        strBarTable(39) = "001101000"    '注释: *
        If strBarCode = "" Then Exit Function '注释: 不打印空串
    '注释:      保存打印机 ScaleMode
        Dim intOldScaleMode As ScaleModeConstants
        intOldScaleMode = Printer.ScaleMode
    '注释:      保存打印机 DrawWidth
        Dim intOldDrawWidth As Integer
        intOldDrawWidth = Printer.DrawWidth
    '注释:      保存打印机 Font
        'Dim fntOldFont As stdFont
        'Set fntOldFont = Printer.Font
        Printer.ScaleMode = vbTwips '注释: 设置打印用的坐标刻度为缇(twip=1)
        Printer.DrawWidth = 1     '注释: 线宽为 1
        Printer.FontName = "宋体" '注释: 打印在条码下方字符的字体和大小
        Printer.FontSize = 10
        Dim strBC As String         '注释: 要打印的条码字符串
        strBC = UCase(strBarCode)
        Dim x As Integer
        Dim y As Integer
        Dim intHeight As Integer
        '注释: 将以毫米表示的 X 坐标、Y 坐标、高度 转换为以缇表示
        x = Printer.ScaleX(intXPos, vbMillimeters, vbTwips)
        y = Printer.ScaleY(intYPos, vbMillimeters, vbTwips)
        intHeight = Printer.ScaleY(intPrintHeight, vbMillimeters, vbTwips)    '注释:      是否在条形码下方打印人工识别字符
        If bolPrintText = True Then
        '注释:          条码打印高度要减去下面的字符显示高度
            intHeight = intHeight - Printer.TextHeight(strBC)
        End If
        Const intWidthCU As Integer = 30 '注释: 粗线和宽间隙宽度
        Const intWidthXI As Integer = 10 '注释: 细线和窄间隙宽度
        Dim intIndex As Integer            '注释: 当前处理的字符串索引
        Dim i As Integer, j As Integer, k As Integer    '注释: 循环控制变量
        '注释:      添加起始字符
        If Left(strBC, 1) <> "*" Then
            strBC = "*" & strBC
        End If
        '注释:      添加结束字符
        If Right(strBC, 1) <> "*" Then
            strBC = strBC & "*"
        End If
       
        '注释:      循环处理每个要显示的条码字符
        For i = 1 To Len(strBC)
            '注释: 确定当前字符在 strBarTable 中的索引
            Select Case Mid(strBC, i, 1)
            Case "*"
                intIndex = 39
            Case "$"
                intIndex = 38
            Case "%"
                intIndex = 37
            Case "-"
                intIndex = 36
            Case "0" To "9"
                intIndex = CInt(Mid(strBC, i, 1))
            Case "A" To "Z"
                intIndex = Asc(Mid(strBC, i, 1)) - Asc("A") + 10
            Case Else
                MsgBox "要打印的条形码字符串中包含无效字符!当前版本只支持字符 '注释:0'注释:-'注释:9'注释:,'注释:A'注释:-'注释:Z'注释:,'注释:-'注释:,'注释:%'注释:,'注释:$'注释:和'注释:*'注释:"
            End Select
            '注释:          是否在条形码下方打印人工识别字符
            If bolPrintText = True Then
                Printer.CurrentX = x
                Printer.CurrentY = y + intHeight
                Printer.Print Mid(strBC, i, 1)
            End If
            For j = 1 To 5
            '注释:              画细线
                If Mid(strBarTable(intIndex), j, 1) = "0" Then
                    For k = 0 To intWidthXI - 1
                        Printer.Line (x + k, y)-Step(0, intHeight)
                    Next k
                    x = x + intWidthXI
                '注释:              画宽线
                Else
                    For k = 0 To intWidthCU - 1
                        Printer.Line (x + k, y)-Step(0, intHeight)
                    Next k
                    x = x + intWidthCU
                End If
                '注释:              每个字符条码之间为窄间隙
                If j = 5 Then
                    x = x + intWidthXI * 3
                    Exit For
                End If
                '注释:              窄间隙
                If Mid(strBarTable(intIndex), j + 5, 1) = "0" Then
                    x = x + intWidthXI * 3
                '注释:              宽间隙
                Else
                    x = x + intWidthCU * 2
                End If
            Next j
        Next i
        
        '注释:      恢复打印机 ScaleMode
        Printer.ScaleMode = intOldScaleMode
        '注释:      恢复打印机 DrawWidth
        Printer.DrawWidth = intOldDrawWidth
        '注释:      恢复打印机 Font
        'Set Printer.Font = fntOldFont
        'Printer.EndDoc
    End Function