VB.NET Rs232串口通讯类编程问题(高分求教!急!急!)

hbpqwxh 2007-11-14 10:28:31
我使用了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,0,0,OPEN_EXISTING,iMode,0)
If mhRS<>-1 Then
Dim pErrCode 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=-1Then
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("WriteError - 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
太长未发全!
...全文
182 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbpqwxh 2007-11-15
  • 打赏
  • 举报
回复
诸位大侠没有知道的吗?
hbpqwxh 2007-11-15
  • 打赏
  • 举报
回复
大哥我用的是vs2003,且用其他控件(PCCOMM,MSCOMM等)均出现了通讯时会卡一端时间的现象!所以选用API!
kbryant 2007-11-14
  • 打赏
  • 举报
回复
友情up,
CloneCenter 2007-11-14
  • 打赏
  • 举报
回复
不能用 vs2005 里面的 SerialPort 控件吗?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧