| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2851 人关注过本帖
标题:得到一段VB代码,请帮我转换成VFP的。谢谢!
只看楼主 加入收藏
pjtyzyq
Rank: 4
等 级:业余侠客
威 望:6
帖 子:232
专家分:240
注 册:2016-2-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
得到一段VB代码,请帮我转换成VFP的。谢谢!


Option Explicit

Global ComNum As Long
Global bRead(255) As Byte

Type COMSTAT
        fCtsHold As Long
        fDsrHold As Long
        fRlsdHold As Long
        fXoffHold As Long
        fXoffSent As Long
        fEof As Long
        fTxim As Long
        fReserved As Long
        cbInQue As Long
        cbOutQue As Long
End Type

Type COMMTIMEOUTS
        ReadIntervalTimeout As Long
        ReadTotalTimeoutMultiplier As Long
        ReadTotalTimeoutConstant As Long
        WriteTotalTimeoutMultiplier As Long
        WriteTotalTimeoutConstant As Long
End Type

Type DCB
        DCBlength As Long
        BaudRate As Long
        fBinary As Long
        fParity As Long
        fOutxCtsFlow As Long
        fOutxDsrFlow As Long
        fDtrControl As Long
        fDsrSensitivity As Long
        fTXContinueOnXoff As Long
        fOutX As Long
        fInX As Long
        fErrorChar As Long
        fNull As Long
        fRtsControl As Long
        fAbortOnError As Long
        fDummy2 As Long
        wReserved As Integer
        XonLim As Integer
        XoffLim As Integer
        ByteSize As Byte
        Parity As Byte
        StopBits As Byte
        XonChar As Byte
        XoffChar As Byte
        ErrorChar As Byte
        EofChar As Byte
        EvtChar As Byte
End Type

Type OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type
Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function GetLastError Lib "kernel32" () As Long
Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long


Function fin_com()
    fin_com = CloseHandle(ComNum)
End Function

'关闭端口
Function FlushComm()
    FlushFileBuffers (ComNum)
End Function

'初始化端口
Function Init_Com(ComNumber As String, Comsettings As String) As Boolean
On Error GoTo handelinitcom
    Dim ComSetup As DCB, Answer, Stat As COMSTAT, RetBytes As Long
    Dim retval As Long
    Dim CtimeOut As COMMTIMEOUTS, BarDCB As DCB
    ' 打开通讯口读/写(&HC0000000).
    ' 必须指定存在的文件 (3).
    ComNum = CreateFile(ComNumber, &HC0000000, 0, 0&, &H3, 0, 0)
    If ComNum = -1 Then
        MsgBox "端口 " & ComNumber & "无效. 请设置正确.", 48
        Init_Com = False
        Exit Function
    End If
    '超时
    CtimeOut.ReadIntervalTimeout = 20
    CtimeOut.ReadTotalTimeoutConstant = 1
    CtimeOut.ReadTotalTimeoutMultiplier = 1
    CtimeOut.WriteTotalTimeoutConstant = 10
    CtimeOut.WriteTotalTimeoutMultiplier = 1
    retval = SetCommTimeouts(ComNum, CtimeOut)
    If retval = -1 Then
        retval = GetLastError()
        MsgBox "端口超时设定无效 " & ComNumber & " 错误: " & retval
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
    retval = BuildCommDCB(Comsettings, BarDCB)
    If retval = -1 Then
        retval = GetLastError()
        MsgBox "无效设备 DCB 块 " & Comsettings & " 错误: " & retval
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
    retval = SetCommState(ComNum, BarDCB)
    If retval = -1 Then
        retval = GetLastError()
        MsgBox "无效设备 DCB 块 " & Comsettings & " 错误: " & retval
        retval = CloseHandle(ComNum)
        Init_Com = False
        Exit Function
    End If
   
    Init_Com = True
handelinitcom:
    Exit Function
End Function

'从串口读取数据
Function ReadCommPure() As String
On Error GoTo handelpurecom
    Dim RetBytes As Long, i As Integer, ReadStr As String, retval As Long
    Dim CheckTotal As Integer, CheckDigitLC As Integer
    retval = ReadFile(ComNum, bRead(0), 255, RetBytes, 0)
    ReadStr = ""
    If (RetBytes > 0) Then
        For i = 0 To RetBytes - 1
            If bRead(i) = 0 Then
                ReadStr = ReadStr & " " & "00"
            Else
                ReadStr = ReadStr & " " & IIf(Len(DEC_to_HEX(Val(bRead(i)))) = 1, "0" & DEC_to_HEX(Val(bRead(i))), DEC_to_HEX(Val(bRead(i)))) 'Chr(bRead(i)))
            End If
                    'ReadStr = ReadStr & Chr(bRead(i))
        Next i
    Else
        FlushComm
    End If
    ReadCommPure = ReadStr
handelpurecom:
    Exit Function
End Function



'向串口写数据
Function WriteCOM32(COMString As String) As Integer
On Error GoTo handelwritelpt
    Dim RetBytes As Long, LenVal As Long
    Dim retval As Long
   
    If Len(COMString) > 255 Then
        WriteCOM32 Left$(COMString, 255)
        WriteCOM32 Right$(COMString, Len(COMString) - 255)
        Exit Function
    End If
   
    For LenVal = 0 To Len(COMString) - 1
        bRead(LenVal) = Asc(Mid$(COMString, LenVal + 1, 1))
    Next LenVal
'    bRead(LenVal) = 0
    retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
'    FlushComm
    WriteCOM32 = RetBytes
   
handelwritelpt:
    Exit Function
End Function
2016-03-24 19:34
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:7 
不会这个
2016-03-24 19:55
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:7 
重在理解,用WinAPI写的串口操作,查查那几个API函数的用法应该能理解。
2016-03-24 19:59
aaaaaa
Rank: 8Rank: 8
等 级:贵宾
威 望:21
帖 子:796
专家分:937
注 册:2012-9-4
收藏
得分:0 
就 API 代码转了下,其他的 VB 代码太长,没有转,不知道对不对。

*!*    VB Code :
*!*    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
*!*    Declare Function GetLastError Lib "kernel32" () As Long
*!*    Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
*!*    Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
*!*    Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
*!*    Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
*!*    Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
*!*    Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
*!*    Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
*!*    Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long


*!*    VFP Code :
Declare Integer CloseHandle In kernel32.Dll ;
    Integer hObject

Declare Integer GetLastError In kernel32

Declare Integer ReadFile In kernel32.Dll ;
    Integer hFile, ;
    String  @lpBuffer, ;
    Integer nNumberOfBytesToRead, ;
    Integer @NumberOfBytesRead, ;
    Integer lpOverlapped

Declare Integer WriteFile In kernel32.Dll ;
    Integer hFile, ;
    String  @lpBuffer, ;
    Integer nNumberOfBytesToWrite, ;
    Integer @lpNumberOfBytesWritten, ;
    Integer lpOverlapped

Declare Integer SetCommTimeouts In kernel32.Dll ;
    Integer hFile, ;
    String  lpCommTimeouts

Declare Integer GetCommTimeouts In kernel32.Dll ;
    Integer hFile, ;
    String  lpCommTimeouts

Declare Integer BuildCommDCB In kernel32.Dll As BuildCommDCBA ;
    String  lpDef, ;
    String  lpDCB

Declare Integer SetCommState In kernel32.Dll ;
    Integer @hCommDev, ;
    String  lpDCB

Declare Integer CreateFile In kernel32.Dll As CreateFileA ;
    String  lpFileName, ;
    Integer dwDesiredAccess, ;
    Integer dwShareMode, ;
    Integer lpSecurityAttributes, ;
    Integer dwCreationDisposition, ;
    Integer dwFlagsAndAttributes, ;
    Integer hTemplateFile

Declare Integer FlushFileBuffers In kernel32 ;
    Integer hFile


[此贴子已经被作者于2016-3-25 00:17编辑过]


民工子弟学校22班团小组长阳光模特队长冲锋篮球队前锋小苹果合唱队领唱蓝天舞蹈队编舞
2016-03-25 00:07
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:0 
不清楚具体做些什么,也没有外设测试。
最好自己先边写边测试,写到哪不明再提出来。
2016-03-25 08:03
pjtyzyq
Rank: 4
等 级:业余侠客
威 望:6
帖 子:232
专家分:240
注 册:2016-2-14
收藏
得分:0 
回复 4楼 aaaaaa
谢谢!待测试后回复。
2016-03-25 10:22
快速回复:得到一段VB代码,请帮我转换成VFP的。谢谢!
数据加载中...
 
   



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

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