Na řádku
Dim prijato As Integer = SerialPort.BaseStream.EndRead(at)
dojde k vyjímce System.NullReferenceException was unhandled Message="Object reference not set to an instance of an object." Source="CasomiraGPrenos" StackTrace: at SerialCom2.CtiDataCom(IAsyncResult at) in D:\Bin\Komunikace.vb:line 869 at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped) at System.Threading._IOCompletionCallback.IOCompletionCallback_Context(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) protože Objekt SerialPort, který je součástí třídy SerialCom2, už v tu chvíli má hodnotu Nothing. Pokud objekt SerialPort nenastavím na Nothing, ale pouze uzavřu port příkazem SerialPort.Close(), objeví se odpovídající vyjímka: System.InvalidOperationException was unhandled Message="The BaseStream is only available when the port is open." Source="System" StackTrace: at System.IO.Ports.SerialPort.get_BaseStream() at SerialCom2.CtiDataCom(IAsyncResult at) in D:\Bin\Komunikace.vb:line 869 at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped) at System.Threading._IOCompletionCallback.IOCompletionCallback_Context(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) Vysvětluju si to laicky (a možná špatně) tak, že příkazem
SerialPort.BaseStream.BeginRead(mBuffer, 0, mBufferSize, acCtiDataCom, Nothing)
vytvořím samostatné vlákno, které žije vlastním životem. Jen mě mate, že program do procedury acCtiDataCom vletí i když na sériový port nepřijde žádný znak.
|