.net - C# Serial Port Conditional & Partial Reading -


i stumped. searched myself blue in face - no go.

i trying establish serial comms device sends 2 different blocks of data (one after other) every 1 second continuously. first block starts "pid" , second block ends "h18".

i need read once every 5 seconds.

my problem 2 fold:

  1. i have no idea/control when read starts , starts mid - block.
  2. i have no control on start , end cycle ensure full 2 blocks need both.

both blocks 200 characters long in total, has no /r @ beginning , has /r/n in between various items.

i have tried doing 2 subsequent reads no success. tried playing startswith , endswith not recognized? code has been on show, here base working currently:

static void datareceivedhandlerbat(object sender,     serialdatareceivedeventargs e)     {         var batm = sender serialport;         if ((batm != null) && (!_gotresponse))         {          while (stringb.length < 200)             {                 byte[] buffer = new byte[batm.bytestoread];                 int numread = batm.read(buffer, 0, buffer.length);                 stringb.append(system.text.encoding.ascii.getstring(buffer));                 // if (stringb.s  == 0)                 //{                   //   _gotresponse = true;                     // break;                 //}               }          }     } 

and

 /// obtain battery string              serialport batm = new serialport();             batm.portname = "com4";             batm.baudrate = 19200;             batm.databits = 8;             batm.parity = parity.none;             batm.stopbits = stopbits.one;              batm.datareceived += new serialdatareceivedeventhandler(datareceivedhandlerbat);              batm.open();              //batm.readexisting();              int timeoutmsb;             timeoutmsb = 1000;              var starttimeb = datetime.now;             while (!_gotresponse && ((datetime.now - starttimeb).totalmilliseconds < timeoutmsb))             {                 thread.sleep(20);             }              batm.close();              _gotresponse = false;              //build battery string              string bat = stringb.tostring(); 

please me - new c# , have struggled 4 days this?

is gui application or background service? either way, ditch datareceived event , use readasync, showed in blog post. then, buffer incoming data list<byte> (this deals messages arrive split two), , implement synchronization logic.

here's outline of how synchronization works:

  1. loop through list<byte>, find beginning of message
  2. determine whether entire message has been received
  3. copy message payload , fire off event business logic acts on message can separate buffering/parsing logic
  4. remove bytes list end of detected message
  5. search remainder of buffer more valid messages (repeat 1-4)
  6. when buffer list<byte> doesn't contain complete message, call readasync again

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -