[工具下载] 共享一个免费好用的串口助手XCOM V2.0

[复制链接]
6945|36
 楼主| xuanhuanzi 发表于 2018-11-19 23:21 | 显示全部楼层 |阅读模式
XCOM V2.0.rar (215.93 KB, 下载次数: 473)
工欲善其事必先利其器,大家拿走这个利器吧。
yiyigirl2014 发表于 2018-11-19 23:55 | 显示全部楼层
谢谢,这个我电脑里有的,非常好用
598330983 发表于 2018-11-20 19:00 | 显示全部楼层
搞个图看看啊。
598330983 发表于 2018-11-20 19:01 | 显示全部楼层
398645bf3e9894fc8c.png
我来搞个个图。
捉虫天师 发表于 2018-11-20 20:28 | 显示全部楼层
确实是个好东西。
zhuomuniao110 发表于 2018-11-21 20:31 | 显示全部楼层
貌似很好用,希望支持WIN10
zhuomuniao110 发表于 2018-11-21 20:31 | 显示全部楼层
果然支持WIN10,还是单文件版本的,太给力了。
heisexingqisi 发表于 2018-11-22 00:12 | 显示全部楼层
挺好用的串口工具,比用动态链接库或者控件的那种好
chenchangwen 发表于 2018-11-23 12:28 | 显示全部楼层
不能导入导出
734774645 发表于 2018-11-24 00:08 | 显示全部楼层
单文件确实好用。不过串口工具很多的。我记得一个想不起名字了,我去找找
@ziyoudu 发表于 2018-12-28 16:00 | 显示全部楼层
一直在显示错误
Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

Length cannot be less than zero.
Parameter name: length.

320485c25d7d9c91ea.png
 楼主| xuanhuanzi 发表于 2019-1-4 18:17 | 显示全部楼层
@ziyoudu 发表于 2018-12-28 16:00
一直在显示错误
Unhandled exception has occurred in  ...

不知道啊。你的什么系统啊,我这是win10,x64
 楼主| xuanhuanzi 发表于 2019-1-4 18:20 | 显示全部楼层
一般来说,在VB中编写串口通讯程序,首先考虑到是使用MSComm控件,可是该控件不能设置超时,而且对许多内部的参数进行了隐藏,从而不能满足有些具体的工作。而使用API进行串口通信,大多是使用VC,很少见到完整的VB代码,为此,我编写了这个模块。

    同时,由于串口通信是基于字节流的,为方便程序设计,我还编写了三个简单的辅助函数,并写了一个详细的测试代码。

    如果读者有好的建议,欢迎留言告知。具体代码如下:
  1. '* ******************************************************* *
  2. '*    程序名称:basComm.bas
  3. '*    程序功能:在VB中利用API进行串口通信
  4. '*    作者:lyserver
  5. '*    联系方式:http://blog.csdn.net/lyserver
  6. '* ******************************************************* *
  7. Option Explicit
  8. Option Base 0
  9. 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
  10. Private 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
  11. 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
  12. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  13. Private Const GENERIC_READ = &H80000000
  14. Private Const GENERIC_WRITE = &H40000000
  15. Private Const OPEN_EXISTING = 3
  16. Private Const INVALID_HANDLE_VALUE = -1

  17. Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
  18. Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
  19. Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
  20. Private Declare Function SetupComm Lib "kernel32" (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long
  21. Private Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Long, ByVal dwFlags As Long) As Long
  22. Private Const PURGE_TXABORT = &H1     '  Kill the pending/current writes to the comm port.
  23. Private Const PURGE_RXABORT = &H2     '  Kill the pending/current reads to the comm port.
  24. Private Const PURGE_TXCLEAR = &H4     '  Kill the transmit queue if there.
  25. Private Const PURGE_RXCLEAR = &H8     '  Kill the typeahead buffer if there.
  26. Private Type DCB
  27.         DCBlength As Long
  28.         BaudRate As Long
  29.         fBitFields As Long 'See Comments in Win32API.Txt
  30.         wReserved As Integer
  31.         XonLim As Integer
  32.         XoffLim As Integer
  33.         ByteSize As Byte
  34.         Parity As Byte
  35.         StopBits As Byte
  36.         XonChar As Byte
  37.         XoffChar As Byte
  38.         ErrorChar As Byte
  39.         EOFChar As Byte
  40.         EvtChar As Byte
  41.         wReserved1 As Integer 'Reserved; Do Not Use
  42. End Type
  43. Private Type COMMTIMEOUTS
  44.         ReadIntervalTimeout As Long
  45.         ReadTotalTimeoutMultiplier As Long
  46.         ReadTotalTimeoutConstant As Long
  47.         WriteTotalTimeoutMultiplier As Long
  48.         WriteTotalTimeoutConstant As Long
  49. End Type

  50. Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

  51. '串口操作演示
  52. Sub Main()
  53.     Dim hComm As Long
  54.     Dim szTest As String
  55.    
  56.     '打开串口1
  57.     hComm = OpenComm(1)
  58.    
  59.     If hComm <> 0 Then
  60.         '设置串口通讯参数
  61.         SetCommParam hComm
  62.         
  63.         '设置串口超时
  64.         SetCommTimeOut hComm, 2, 3
  65.         
  66.         '向串口写入字符串123
  67.         szTest = "123"
  68.         WriteComm hComm, StringToBytes(szTest)
  69.         
  70.         '读串口
  71.         szTest = BytesToString(ReadComm(hComm))
  72.         Debug.Print szTest
  73.         
  74.         '关闭串口
  75.         CloseComm hComm
  76.     End If
  77. End Sub

  78. '打开串口
  79. Function OpenComm(ByVal lComPort As Long) As Long
  80.     Dim hComm As Long
  81.    
  82.     hComm = CreateFile("COM" & lComPort, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
  83.     If hComm = INVALID_HANDLE_VALUE Then
  84.         OpenComm = 0
  85.     Else
  86.         OpenComm = hComm
  87.     End If
  88. End Function

  89. '关闭串口
  90. Sub CloseComm(hComm As Long)
  91.     CloseHandle hComm
  92.     hComm = 0
  93. End Sub

  94. '读串口
  95. Function ReadComm(ByVal hComm As Long) As Byte()
  96.     Dim dwBytesRead As Long
  97.     Dim BytesBuffer() As Byte
  98.    
  99.     ReDim BytesBuffer(4095)
  100.     ReadFile hComm, BytesBuffer(0), UBound(BytesBuffer) + 1, dwBytesRead, 0
  101.     If dwBytesRead > 0 Then
  102.         ReDim Preserve BytesBuffer(dwBytesRead)
  103.         ReadComm = BytesBuffer
  104.     End If
  105. End Function

  106. '写串口
  107. Function WriteComm(ByVal hComm As Long, BytesBuffer() As Byte) As Long
  108.     Dim dwBytesWrite
  109.    
  110.     If SafeArrayGetDim(BytesBuffer) = 0 Then Exit Function
  111.     WriteFile hComm, BytesBuffer(0), UBound(BytesBuffer) + 1, dwBytesWrite, 0
  112.     WriteComm = dwBytesWrite
  113. End Function

  114. '设置串口通讯参数
  115. Function SetCommParam(ByVal hComm As Long, Optional ByVal lBaudRate As Long = 9600, _
  116.         Optional ByVal cByteSize As Byte = 8, Optional ByVal cStopBits As Byte = 0, _
  117.         Optional ByVal cParity As Byte = 0, Optional ByVal cEOFChar As Long = 26) As Boolean
  118.         
  119.     Dim dc As DCB
  120.     If hComm = 0 Then Exit Function
  121.    
  122.     If GetCommState(hComm, dc) Then
  123.         dc.BaudRate = lBaudRate
  124.         dc.ByteSize = cByteSize
  125.         dc.StopBits = cStopBits
  126.         dc.Parity = cParity
  127.         dc.EOFChar = cEOFChar
  128.         
  129.         SetCommParam = CBool(SetCommState(hComm, dc))
  130.     End If
  131. End Function

  132. '设置串口超时
  133. Function SetCommTimeOut(ByVal hComm As Long, Optional ByVal dwReadTimeOut As Long = 2, _
  134.         Optional ByVal dwWriteTimeOut As Long = 3) As Boolean
  135.         
  136.     Dim ct As COMMTIMEOUTS
  137.     If hComm = 0 Then Exit Function
  138.    
  139.     ct.ReadIntervalTimeout = dwReadTimeOut '读操作时,字符间超时
  140.     ct.ReadTotalTimeoutMultiplier = dwReadTimeOut '读操作时,每字节超时
  141.     ct.ReadTotalTimeoutConstant = dwReadTimeOut '读操作时,固定超时(总超时=每字节超时*字节数+固定超时)
  142.     ct.WriteTotalTimeoutMultiplier = dwWriteTimeOut '写操作时,每字节超时
  143.     ct.WriteTotalTimeoutConstant = dwWriteTimeOut '写操作时,固定超时(总超时=每字节超时*字节数+固定超时)
  144.    
  145.     SetCommTimeOut = CBool(SetCommTimeouts(hComm, ct))
  146. End Function

  147. '设置串口读写缓冲区大小
  148. Function SetCommBuffer(ByVal hComm As Long, Optional ByVal dwBytesRead As Long = 1024, _
  149.         Optional ByVal dwBytesWrite As Long = 512) As Boolean
  150.    
  151.     If hComm = 0 Then Exit Function
  152.     SetCommBuffer = CBool(SetupComm(hComm, dwBytesRead, dwBytesWrite))
  153. End Function

  154. '清空串口缓冲区
  155. Sub ClearComm(ByVal hComm As Long, Optional ByVal InBuffer As Boolean = True, Optional ByVal OutBuffer As Boolean = True)
  156.     If hComm = 0 Then Exit Sub
  157.     If InBuffer And OutBuffer Then '清空输入输出缓冲区
  158.         PurgeComm hComm, PURGE_TXABORT Or PURGE_RXABORT Or PURGE_TXCLEAR Or PURGE_RXCLEAR
  159.     ElseIf InBuffer Then '清空输入缓冲区
  160.         PurgeComm hComm, PURGE_RXABORT Or PURGE_RXCLEAR
  161.     ElseIf OutBuffer Then '清空输出缓冲区
  162.         PurgeComm hComm, PURGE_TXABORT Or PURGE_TXCLEAR
  163.     End If
  164. End Sub

  165. '辅助函数:BSTR字符串转换为CHAR字符串
  166. Function StringToBytes(ByVal szText As String) As Byte()
  167.     If Len(szText) > 0 Then
  168.         StringToBytes = StrConv(szText, vbFromUnicode)
  169.     End If
  170. End Function

  171. '辅助函数:CHAR字符串转换为BSTR字符串
  172. Function BytesToString(bytesText() As Byte) As String
  173.     If SafeArrayGetDim(bytesText) <> 0 Then
  174.         BytesToString = StrConv(bytesText, vbUnicode)
  175.     End If
  176. End Function

  177. '辅助函数:获得CHAR字符串长度
  178. Function Byteslen(bytesText() As Byte) As Long
  179.     If SafeArrayGetDim(bytesText) <> 0 Then
  180.         Byteslen = UBound(bytesText) + 1
  181.     End If
  182. End Function


 楼主| xuanhuanzi 发表于 2019-1-4 18:20 | 显示全部楼层
上面是Excel的VB的API串口通信代码。
huangcunxiake 发表于 2019-1-7 14:54 | 显示全部楼层
可以用,用控件的那种麻烦。
怜香客 发表于 2020-3-16 15:32 | 显示全部楼层
谢谢分享
wanduzi 发表于 2020-3-20 19:54 | 显示全部楼层
看看好用bu
kkzz 发表于 2020-3-21 19:53 | 显示全部楼层

           
hudi008 发表于 2020-3-21 19:53 | 显示全部楼层
串口调试助手哪个好用
lzmm 发表于 2020-3-21 19:54 | 显示全部楼层
都能实现什么功能  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部