vb sdk 串口编程.doc_第1页
vb sdk 串口编程.doc_第2页
vb sdk 串口编程.doc_第3页
vb sdk 串口编程.doc_第4页
vb sdk 串口编程.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VB SDK 函数 串口编程 收藏 view plaincopy to clipboardprint?Private 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 Private Declare Function GetCommState Lib kernel32 _ (ByVal hFile As Long, ByRef lpDCB As DCB) As Long Private Declare Function SetCommState Lib kernel32 _ (ByVal hFile As Long, ByRef lpDCB As DCB) As Long Private Declare Function SetupComm Lib kernel32 (ByVal hFile As Long, _ ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long Private Declare Function PurgeComm Lib kernel32 _ (ByVal hFile As Long, ByVal dwFlags As Long) As Long Private Declare Function SetCommTimeouts Lib kernel32 _ (ByVal hFile As Long, ByRef lpCommTimeouts As COMMTIMEOUTS) As Long Private Declare Function CloseHandle Lib kernel32 ( _ ByVal hObject As Long) As Long Private Declare Function ReadFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _ lpOverlapped As OVERLAPPED) As Long Private Declare Function WriteFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _ lpOverlapped As OVERLAPPED) As Long Private Declare Function WriteFile Lib kernel32 ( _ ByVal hFile As Long, lpBuffer As Any, _ ByVal nNumberOfBytesToWrite As Long, _ lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long Declare Function ReadFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _ lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long Const PURGE_TXABORT = &H1 Const PURGE_RXABORT = &H2 Const PURGE_TXCLEAR = &H4 Const PURGE_RXCLEAR = &H8 Const MAXDWORD = &HFFFFFFFF Const GENERIC_WRITE = &H40000000 Const GENERIC_READ = &H80000000 Const FILE_ATTRIBUTE_NORMAL = &H80 Const OPEN_EXISTING = 3 Const FILE_FLAG_OVERLAPPED = &H40000000 Const INVALID_HANDLE_VALUE = -1 Private Type DCB DCBlength As Long BaudRate As Long fXXX 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 wReserved1 As Integer End Type Private Type COMMTIMEOUTS ReadIntervalTimeout As Long ReadTotalTimeoutMultiplier As Long ReadTotalTimeoutConstant As Long WriteTotalTimeoutMultiplier As Long WriteTotalTimeoutConstant As Long End Type Private hCom As Long Public Function OpenCom(ByVal strCom As String, ByVal Baud As Long) As Boolean hCom = CreateFile(strCom, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, 0) hCom = CreateFile(strCom, GENERIC_READ Or GENERIC_WRITE, 0, 0, _ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) If hCom = INVALID_HANDLE_VALUE Then OpenCom = False Exit Function End If 定义设备控制块 Dim typDCB As DCB *获取设备控制块* GetCommState hCom, typDCB typDCB.BaudRate = Baud 定义波特率 typDCB.Parity = 0 0/1/2/3= 无校验/奇校验/偶校验/标记校验 typDCB.ByteSize = 8 数据位 typDCB.StopBits = 0 停止位 0/1/2 = 1/1.5/2 *设置串口参数* SetCommState hCom, typDCB *设置缓冲区大小* SetupComm hCom, 1024, 1024 *清空读写缓冲区* PurgeComm hCom, PURGE_RXABORT Or PURGE_RXCLEAR Or PURGE_TXABORT Or PURGE_TXCLEAR 定义超时结构体 Dim typCommTimeouts As COMMTIMEOUTS typCommTimeouts.ReadIntervalTimeout = MAXDWORD 相邻两字节读取最大时间间隔(为0表示不使用该超时间隔) typCommTimeouts.ReadTotalTimeoutMultiplier = 0 一个读操作的时间常数 typCommTimeouts.ReadTotalTimeoutConstant = 0 读超时常数 typCommTimeouts.WriteTotalTimeoutMultiplier = 0 一个写操作的时间常数(为0表示不使用该超时间隔) typCommTimeouts.WriteTotalTimeoutConstant = 0 写超时常数(为0表示不使用该超时间隔) *超时设置* SetCommTimeouts hCom, typCommTimeouts OpenCom = True End Function Public Function ReadCom(ByRef ReadBuff() As Byte, ByVal ToLen As Long, ByRef ReadLen As Long) As Boolean Dim fSuccess As Long fSuccess = ReadFile(hCom, ReadBuff(0), ToLen, ReadLen, 0) If fSuccess = 0 Then ReadCom = False Exit Function End If ReadCom = True End Function Public Function WriteCom(ByRef WirteBuff() As Byte, ByVal ToLen As Long, ByRef WriteLen As Long) As Boolean Dim fSuccess As Long fSuccess = WriteFile(hCom, WirteBuff(0), ToLen, WriteLen, 0) If fSuccess = 0 Then WriteCom = False Exit Function End If WriteCom = True End Function Public Function CloseCom() CloseHandle hCom End Function ,*, 窗体代码 * Private Sub CommRElectric_Click() Dim Receive(10) As Byte Dim RevLen As Long Dim str As String If ReadCom(Receive, 10, RevLen) = True Then 显示字符串 LabStat.Caption = StrConv(Receive, vbUnicode) 显示16进制 For i = 0 To RevLen - 1 If i 0 Then str = str + str = str + Hex(Receive(i) Next LabStat.Caption = str Else LabStat.Caption = 接收失败 End If End Sub Private Sub ComStar_Click() Dim Send(10) As Byte Dim SendLen As Long Send(0) = 1 Send(1) = 5 Send(2) = 8 Send(3) = 10 If WriteCom(Send, 4, SendLen) = True Then LabStat.Caption = 发送成功,字节数: & SendLen Else LabStat.Caption = 发送失败 End If End Sub Private Sub Form_Load() If OpenCom(COM1, 19200) = False Then LabStat.Caption = 串口打开失败 Else LabStat.Caption = 串口打开成功 End If End Sub Private Sub Form_Unload(Cancel As Integer) CloseCom End Sub Private 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 Private Declare Function GetCommState Lib kernel32 _ (ByVal hFile As Long, ByRef lpDCB As DCB) As LongPrivate Declare Function SetCommState Lib kernel32 _ (ByVal hFile As Long, ByRef lpDCB As DCB) As Long Private Declare Function SetupComm Lib kernel32 (ByVal hFile As Long, _ ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As LongPrivate Declare Function PurgeComm Lib kernel32 _ (ByVal hFile As Long, ByVal dwFlags As Long) As LongPrivate Declare Function SetCommTimeouts Lib kernel32 _ (ByVal hFile As Long, ByRef lpCommTimeouts As COMMTIMEOUTS) As Long Private Declare Function CloseHandle Lib kernel32 ( _ ByVal hObject As Long) As LongPrivate Declare Function ReadFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _ lpOverlapped As OVERLAPPED) As Long Private Declare Function WriteFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _ lpOverlapped As OVERLAPPED) As LongPrivate Declare Function WriteFile Lib kernel32 ( _ ByVal hFile As Long, lpBuffer As Any, _ ByVal nNumberOfBytesToWrite As Long, _ lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As LongDeclare Function ReadFile Lib kernel32 (ByVal hFile As Long, _ lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _ lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long Const PURGE_TXABORT = &H1Const PURGE_RXABORT = &H2Const PURGE_TXCLEAR = &H4Const PURGE_RXCLEAR = &H8Const MAXDWORD = &HFFFFFFFFConst GENERIC_WRITE = &H40000000Const GENERIC_READ = &H80000000Const FILE_ATTRIBUTE_NORMAL = &H80Const OPEN_EXISTING = 3Const FILE_FLAG_OVERLAPPED = &H40000000Const INVALID_HANDLE_VALUE = -1Private Type DCB DCBlength As Long BaudRate As Long fXXX 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 wReserved1 As IntegerEnd TypePrivate Type COMMTIMEOUTS ReadIntervalTimeout As Long ReadTotalTimeoutMultiplier As Long ReadTotalTimeoutConstant As Long WriteTotalTimeoutMultiplier As Long WriteTotalTimeoutConstant As LongEnd TypePrivate hCom As LongPublic Function OpenCom(ByVal strCom As String, ByVal Baud As Long) As Boolean hCom = CreateFile(strCom, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, 0) hCom = CreateFile(strCom, GENERIC_READ Or GENERIC_WRITE, 0, 0, _ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) If hCom = INVALID_HANDLE_VALUE Then OpenCom = False Exit Function End If 定义设备控制块 Dim typDCB As DCB *获取设备控制块* GetCommState hCom, typDCB typDCB.BaudRate = Baud 定义波特率 typDCB.Parity = 0 0/1/2/3= 无校验/奇校验/偶校验/标记校验 typDCB.ByteSize = 8 数据位 typDCB.StopBits = 0 停止位 0/1/2 = 1/1.5/2 *设置串口参数* SetCommState hCom, typDCB *设置缓冲区大小* SetupComm hCom, 1024, 1024 *清空读写缓冲区* PurgeComm hCom, PURGE_RXABORT Or PURGE_RXCLEAR Or PURGE_TXABORT Or PURGE_TXCLEAR 定义超时结构体 Dim typCommTimeouts As COMMTIMEOUTS typCommTimeouts.ReadIntervalTimeout = MAXDWORD 相邻两字节读取最大时间间隔(为0表示不使用该超时间隔) typCommTimeouts.ReadTotalTimeoutMultiplier = 0 一个读操作的时间常数 typCommTimeouts.ReadTotalTimeoutConstant = 0 读超时常数 typCommTimeouts.WriteTotalTimeoutMultiplier = 0 一个写操作的时间常数(为0表示不使用该超时间隔) typCommTimeouts.WriteTotalTimeoutConstant = 0 写超时常数(为0表示不使用该超时间隔) *超时设置* SetCommTimeouts hCom, typCommTimeouts OpenCom = TrueEnd FunctionPublic Function ReadCom(ByRef ReadBuff() As Byte, ByVal ToLen As Long, ByRef ReadLen As Long) As Boolean Dim fSuccess As Long fSuccess = ReadFile(hCom, ReadBuff(0), ToLen, ReadLen, 0) If fSuccess = 0 Then ReadCom = False Exit Function End If ReadCom = True End FunctionPublic Function WriteCom(ByRef WirteBuff() As Byte, ByVal ToLen As Long, ByRef WriteLen As Long) As Boolean Dim fSuccess As Long fSuccess = WriteFile(hCom, WirteBuff(0), ToLen, WriteLen, 0) If fSuccess = 0 Then WriteCom = False Exit Function End If WriteCom = True End FunctionPubl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论