0

I want receive datas commands on RS232 with VB.net

Currently my code work well, but except if no data are received, the userForm can't be used, because the code is in serialport.read continuously. How to do to allow user to access the form. I planned to implement DoEvent() function, but how ? Have you ideas ?

Here's my code:

    Try
          readBytes = Form1.SerialPort1.Read(buffer_rx, offset, remaining)
        offset += readBytes
        remaining -= readBytes
        totalReadBytes += readBytes
Catch ex As TimeoutException
ReDim Preserve buffer_rx(totalReadBytes - 1)
End Try

Thx,

morbak
  • 317
  • 4
  • 17
  • 1
    You'll have to throw this away, it isn't fit to get the job done. Use the SerialPort.DataReceived event. Or move this code into a BackgroundWorker so it doesn't block your UI. Or use Async/Await on the serialport's BaseStream.ReadAsync() method. – Hans Passant Aug 01 '16 at 09:26
  • Use the `DataReceived` event of the SerialPort to read data from the port. DoEvents won't solve your issue – Matt Wilko Aug 01 '16 at 09:32
  • ok, i've try to done it before, but i can't success to manage to send (PC TX) a RS command, and wait the answer (device), analyse the answer and to the job (function, sub ,etc...) according the instruction containing in the answer, before sending the next command (PC). Because, all TX instruction are send before waiting the traitment of the first command. Have you an exemple to to that (if i'm clear enough? ;) ) – morbak Aug 01 '16 at 12:14

0 Answers0