|
这里把串口控制的函数发上来给大家分享,另外增加一点本贴人气,哈哈
需要引用到MSComm控
以下为读取函数
Private Function OpenCommPort() As Boolean
Dim strParity As String
’这里必须先关闭端口,否则端口中会遗留以前的数据,导致读取重复
OpenCommPort = False
'设置出错指向
On Error GoTo CommErr
'这里的配置和设备本身有关,因二维条码扫描枪默认只能是9600速率
strParity = "9600" 'GetConfigValue(SECTION_SYS_CONFIG, KEY_COMM_PARITY)
'只允许设置以下几种波特率,除此之外都设为默认的9600
Select Case strParity
Case "110", "300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", _
"115200", "230400", "460800", "921600"
Case Else
strParity = "9600"
End Select
Dim Xcom As Integer
Dim inipath As String
‘以下写了一个函数读取.ini文件,目的是读取端口号配置,如谁有能自动判断端口号的方法更好
inipath = IIf(Len(Application.CurrentProject.Path) = 3, Left(Application.CurrentProject.Path, 2), Application.CurrentProject.Path) & "\com.ini"
Xcom = CInt(GetFromINI("端口信息", "端口号", inipath))
'使用MSComm控件,读取对应端口号的内容
With MSComm1
.InputMode = comInputModeBinary
.InputLen = 0 '设为0,一次读出全部缓冲区中的数据
.Settings = strParity & ",n,8,1"
.Tag = strParity
.RThreshold = 1
.CommPort = Xcom 'GetConfigValue(SECTION_SYS_CONFIG, KEY_COMM_PORT, "1")
.PortOpen = True
End With
OpenCommPort = True
Exit Function
CommErr:
OpenCommPort = False
End Function |
|