VB.NET Rs232串口通讯类编程问题(高分求教!急!急!急!) 我使用了vb.net的串口RS232通讯类编了一个串口采集程序! 当计算机正常登陆时运行一切正常,不过当计算机掉电重新启动后,在未登陆系统之前 该程序在
后台执行(或登陆后重新运行程序),均不能从串口取到数据,只能登陆后使用串口调试工具运行一次后才能 采集到数据,附
代码,请问各位大侠如何
解决! 以下为部分
代码: Public m_CommPort As New Rs232 Public Function comm_accept(ByVal address As String,ByVal length As Integer,ByVal receive_waittime As Double,ByVal yblx As Integer) As String Dim start_time As Double Dim comm_length As Integer Dim loop_int As Integer Dim loop_time As Integer Dim receive_str_temp As String Dim acc_check As Integer Dim cal_str_temp As String accept_string = "" m_CommPort.ClearInputBuffer() start_time = cal_time() m_ModemPort = 1 m_CommPort.Open(m_ModemPort,9600,length,Rs232.DataParity.Parity_None,Rs232.DataStopBit.StopBit_1,4096) m_CommPort.Write(Encoding.ASCII.GetBytes(address)) While (m_CommPort.Read(1) <> -1) accept_string = accept_string + Chr(m_CommPort.InputStream(0)) System.Threading.Thread.Sleep(1) If Len(accept_string) = length Then comm_error_code = 0 Exit While End If If start_time + receive_waittime >= 86399.999 Then If cal_time() - start_time + 864399.999 >= receive_waittime Then comm_error_code = 1 Exit While End If Else If (cal_time() - start_time) >= receive_waittime Then comm_error_code = 1 Exit While End If End If End While m_CommPort.Close() Application.DoEvents() End Function RS232串口类: Public Class Rs232 Private mhRS As Integer = -1 Private miPort As Integer = 1 Private miTimeout As Integer = 600 Private miBaudRate As Integer = 9600 Private meParity As DataParity = 0 Private meStopBit As DataStopBit = 0 Private miDataBit As Integer = 8 Private miBufferSize As Integer = 512 Private mabtRxBuf As Byte() Private meMode As Mode Private mbWaitOnRead As Boolean Private mbWaitOnWrite As Boolean Private mbWriteErr As Boolean Private muOverlapped As OVERLAPPED Private muOverlappedW As OVERLAPPED Private muOverlappedE As OVERLAPPED Private mabtTmpTxBuf As Byte() Private moThreadTx As Thread Private moThreadRx As Thread Private miTmpBytes2Read As Integer Private meMask As EventMasks Public Overloads Sub Open() Dim uDcb As DCB,iRc As Integer Dim iMode As Integer = Convert.ToInt32(IIf(meMode = Mode.Overlapped,_ FILE_FLAG_OVERLAPPED,0)) If miPort > 0 Then Try mhRS = CreateFile("COM" & miPort.ToString,_ GENERIC_READ Or GENERIC_WRITE,_ OPEN_EXISTING,iMode,0) If mhRS <> -1 Then Dim lpErrCode As Integer iRc = ClearCommError(mhRS,lpErrCode,0&) iRc = PurgeComm(mhRS,PurgeBuffers.RXClear Or _ PurgeBuffers.TxClear) iRc = GetCommState(mhRS,uDcb) Dim sParity As String = "NOEM" sParity = sParity.Substring(meParity,1) Dim sDCBState As String = String.Format( _ "baud={0} parity={1} data={2} stop={3}",_ miBaudRate,sParity,miDataBit,CInt(meStopBit)) iRc = BuildCommDCB(sDCBState,uDcb) iRc = SetCommState(mhRS,uDcb) If iRc = 0 Then Dim sErrTxt As String = pErr2Text(GetLastError()) Throw New CIOChannelException( _ "Unable to set COM state0" & sErrTxt) End If iRc = SetupComm(mhRS,miBufferSize,miBufferSize) pSetTimeout() Else Throw New CIOChannelException( _ "Unable to open COM" & miPort.ToString) End If Catch Ex As Exception Throw New CIOChannelException(Ex.Message,Ex) End Try Else Throw New ApplicationException("COM Port not defined," + _ "use Port property to set it before invoking InitPort") End If End Sub Public Overloads Sub Open(ByVal Port As Integer,_ ByVal BaudRate As Integer,ByVal DataBit As Integer,_ ByVal Parity As DataParity,ByVal StopBit As DataStopBit,_ ByVal BufferSize As Integer) Me.Port = Port Me.BaudRate = BaudRate Me.DataBit = DataBit Me.Parity = Parity Me.StopBit = StopBit Me.BufferSize = BufferSize Open() End Sub Public Function Read(ByVal Bytes2Read As Integer) As Integer Dim iReadChars,iRc As Integer If Bytes2Read = 0 Then Bytes2Read = miBufferSize If mhRS = -1 Then Throw New ApplicationException( _ Else Try If meMode = Mode.Overlapped Then pHandleOverlappedRead(Bytes2Read) Else ReDim mabtRxBuf(Bytes2Read - 1) iRc = ReadFile(mhRS,mabtRxBuf,Bytes2Read,iReadChars,Nothing) If iRc = 0 Then Else If iReadChars < Bytes2Read Then Else mbWaitOnRead = True Return (iReadChars) End If End If End If Catch Ex As Exception Throw New ApplicationException("Read Error: " & Ex.Message,Ex) End Try End If End Function Public Overloads Sub Write(ByVal Buffer As Byte()) Dim iBytesWritten,iRc As Integer If mhRS = -1 Then Else Try If meMode = Mode.Overlapped Then If pHandleOverlappedWrite(Buffer) Then Throw New ApplicationException( _ "Error in overllapped write") End If Else PurgeComm(mhRS,PURGE_RXCLEAR Or PURGE_TXCLEAR) iRc = WriteFile(mhRS,Buffer,Buffer.Length,_ iBytesWritten,Nothing) If iRc = 0 Then Throw New ApplicationException( _ "Write Error - Bytes Written " & _ iBytesWritten.ToString & " of " & _ Buffer.Length.ToString) End If End If Catch Ex As Exception Throw End Try End If End Sub Public Overloads Sub Write(ByVal Buffer As String) Dim oEncoder As New System.Text.ASCIIEncoding Dim aByte() As Byte = oEncoder.GetBytes(Buffer) Me.Write(aByte) End Sub 太长未发全! __________________________________________________________________________ 每次程序啟動,你將口設置初始化 了沒? 這里串口程序初始化的一部分! If msRS232_B.PortOpen = True Then msRS232_B.InBufferCount = 0 msRS232_B.OutBufferCount = 0 msRS232_B.PortOpen = False End If msRS232_B.CommPort = intReaderComPortCode msRS232_B.Settings = "9600,N,8,1" msRS232_B.PortOpen = True msRS232_B.InBufferCount = 0 msRS232_B.OutBufferCount = 0 __________________________________________________________________________ 楼上的说的是用MsComm32的ACTIVE组件.楼主说的是用API.没看明白不要乱说. 楼主.我感觉你应该在程序结束时
加上CloseFile();
关闭掉串口.不然没有正常
关闭的情况下串口可能被占用,那你第二次
调用CREATEFILE时不能正确得到句柄. __________________________________________________________________________ 我已经在m_CommPort.Close() 做了
关闭串口操作,并且也检测过串口,确实已经
关闭了,
错误原因不在这! __________________________________________________________________________