本帖最后由 gtrajtr 于 2015-1-26 18:46 编辑
在坛子里看到有坛友抱怨VB6的串口控件mscomm32.ocx 不支持大于16的端口号,有时候使用不方便。
自己就顺手把它破了这个限制,端口号最高支持到255,一般情况应该足够用了。
另外,有坛友说mscomm32在64位系统下用不了,我没有遇到过这种情况。
mscomm32肯定是支持64位系统的,有些老项目的上位机软件是VB写的,一直在64位系统都用得好好的。
用VB写了个简单的测试程序,在win7 64bit下测试通过,串口是用CH340从USB转出来的串口,TX、RX断接,点TX,能正常收发数据。
截图,微软原版本的mscomm32.ocx,打开串口17出错
破解版mscomm32控件-254.png (34.9 KB, 下载次数: 0)
下载附件
2015-1-26 18:34 上传
'初始化串口返回=-1 无效!
Public Function Init_Comm(com_id As String) As Integer
nCid = OpenComm(com_id, 512, 512) '′打开COM2 设置接收,发送缓冲区为512 字节
If nCid < 0 Then
Init_Comm = -1 '′处理错误
Else
Init_Comm = cCid
End If
End Function
'*********************************************************************************
Public Function Build_Comm(com_Info As String) As Integer
Dim comset As String
Dim err As Integer
err = 0
comset = com_Info + " ,n ,8 ,1" '"COM2 :9600 ,n ,8 ,1"
If (BuildCommDCB(comset, LpDCB)) Then err = -1 ' 无法创建
LpDCB.Id = Chr(nCid)
If (SetCommState(LpDCB)) Then err = -2 ' 无法创建′设置串口状态
Build_Comm = err
End Function
'**************************************************************************
'发送数据
Public Function Send_Data_Out_Comm(p As String, cnt As Integer) As Integer
Dim nsend As Integer, x As Integer
Dim Lpstate As COMSTAT '通信状态块
x = GetCommError(nCid, Lpstate) '′读取当前串口错误或状态
If Lpstate.cbOutQue < 512 Then '′送缓冲区有空间否?
nsend = WriteComm(nCid, p, cnt) '′发送inbuff
If nsend <= 0 Then nsend = -nsend ' '′忽略错误
End If
Send_Data_Out_Comm = cnt
End Function
'**************************************************************************
'读取数据
Public Function Read_Data_Out_Comm(p() As String) As Integer
Dim nsend As Integer, x As Integer
Dim Lpstate As COMSTAT '通信状态块
x = GetCommError(nCid, Lpstate) '′读取当前串口错误或状态
If Lpstate.cbInQue < 512 Then '′缓冲区有空间否?
nsend = ReadComm(nCid, p, cnt) '′发送inbuff
If nsend <= 0 Then nsend = -nsend ' '′忽略错误
End If
Read_Data_Out_Comm = Lpstate.cbInQue
End Function
'***********************************************************************************
'关闭串口,释放资源
Public Function Close_Comm()
Dim Nclose As Integer
Nclose = Closecomm(nCid)
nCid = 0
End Function
一周热门 更多>