本帖最后由 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 上传
打开COM1
HANDLE hCom = CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hCom==INVALID_HANDLE_VALUE ) return false;
设置 波特率、奇偶校验、停止位 等等
DCB CommDCB;
GetCommState( hCom, &CommDCB );
CommDCB.BaudRate = BaudRate;
CommDCB.Parity = EVENPARITY;
CommDCB.StopBits = ONESTOPBIT;
CommDCB.ByteSize = 8;
CommDCB.fBinary = 1;
CommDCB.fParity = 1;
CommDCB.fOutxCtsFlow = 0;
CommDCB.fOutxDsrFlow = 0;
CommDCB.fDtrControl = 0;
CommDCB.fDsrSensitivity = 0;
CommDCB.fTXContinueOnXoff = 0;
CommDCB.fOutX = 0;
CommDCB.fInX = 0;
CommDCB.fErrorChar = 0;
CommDCB.fNull = 0;
CommDCB.fRtsControl = RTS_CONTROL_TOGGLE;
CommDCB.fAbortOnError = 0;
SetCommState( hCom, &CommDCB );
设置缓冲大小
SetupComm( hCom, 100, 100 );
设置超时时间
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts( hCom, &CommTimeouts );
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
SetCommTimeouts( hCom, &CommTimeouts );
/////////////// 这期间就是对COM进行读写了 ///////////////
COMSTAT ComStat; ClearCommError(hCom,&Errors,&ComStat); 后 ComStat.cbInQue 保存着一个数值,指明还有多少字节已经获得但没有用ReadFile取走。
读用 ReadFile,写用 WriteFile。
/////////////////////////////////////////////////////////
关闭COM
CloseHandle( hCom );
'****************************************************************************************************************************************
Public Declare Function OpenComm Lib "User" (ByVal IpComName As String, ByVal wInQueue As Integer, ByVal wOutQueue As Integer) As Integer
Public Declare Function SetCommState Lib "User" (IpDCB As DCB) As Integer
Public Declare Function ReadComm Lib "User" (ByVal nCid As Integer, ByVal IpBuf As String, ByVal nSize As Integer) As Integer
Public Declare Function WriteComm Lib "User" (ByVal nCid As Integer, ByVal IpBuf As String, ByVal nSize As Integer) As Integer
Public Declare Function Closecomm Lib "User" (ByVal nCid As Integer) As Integer
Public Declare Function BuildCommDCB Lib "User" (ByVal IpDef As String, IpDCB As DCB) As Integer
Public Declare Function GetCommError Lib "User" (ByVal nCid As Integer, IpStat As COMSTAT) As Integer
'****************************************************************************************************************************************
Type DCB
Id As String
BaudRate As Integer
ByteSize As String
Parity As String
StopBits As String
RlsTimeout As Integer
CtsTimeout As Integer
DsrTimeout As Integer
Bits1 As String
Bits2 As String
XonChar As String
XoffChar As String
XonLim As Integer
XoffLim As Integer
PeChar As String
EofChar As String
EvtChar As String
TxDelay As Integer
End Type
Type COMSTAT
Bits As String
cbInQue As Integer
cbOutQue As Integer
End Type
'*************************************************************************************
Public IpDCB As DCB
Public nCid As Integer
VB用得少,这个是从来软件中COPY出来得头文件。可以用。
'初始化串口返回=-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
一周热门 更多>