c# - Split message in serial communication -
c# - Split message in serial communication -
i new serial communication. have read fair few tutorials, , of trying working, have question regarding serial communication c#. have micro controller sending info through serial line. info ist in format: bxxxxixx.xx,xx.xx* x's represent different numbers, + or - signs. @ times want read info c# programme on pc. problem having messages seem split in random positions though using readto("*"); assumed read upto * character. how can create sure message recieved complete?
thank help.
public string receivecommandhc() { string messagehc = ""; if (serialhc.isopen) { serialhc.discardinbuffer(); messagehc = serialhc.readto("*"); } homecoming messagehc; }
you'll find in serial comms info messages (unless small) split. downwards speed of communication , point @ retrieve info port.
usually you'd set code run in separate thread (to help prevent impacting performance of rest of code) raises event when finish message received , takes total messages in transmission. read , write functionality dealt worker threads (serial comms traffic slow).
you'll need read , write buffer. these should suitabley big hold info several cycles.
append info read input end of read buffer. have read buffer read on cyclicly finish messages, start of buffer.
depending on protocol used there info start , maybe info end indicator , somewhere message size (this may fixed, 1 time again depending on protocol). gather form protocol message start character 'b' , message end character '*'. discard info preceeding message start character ('b'), incomplete message.
when finish message found, strip front end of buffer , raise event indicate arrival.
a similar process run sending data, except may need split message, hence info sent appended end of buffer , info beingness sent read start.
i hope helps in understanding how cope serial comms.
as pointed out marc you're clearing buffer in way cause problems.
edit
as said in comment don't recognise serialhc, if dealing raw info @ using serialport class. more info on how utilize , illustration (which uses process described above) can found here.
c# serial-port packets
Comments
Post a Comment