Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Im trying to create a simple form to Read Scale Data through a serial port and move to a text Box
The trick is that the scale transmits continuously so I need to make code wait for a <stx> character (02) before storing
Scale data is transmited in the Following format
<stx>,<pol>,<wwwwwww>,<unit>,<g n="">,,<termination>
Where
<stx> = ascii 02
<pol> = <space> for Positive, <_> for negative
<wwwwwww> = weight data 7 digits with decimal point
<unit> = <l> for lbs, <k> for Kilograms
<g n=""> = <g> for gross, <n> for net
= Status <space> for valid, <m> for motion, <o> for over/under range
<term> = <cr><lf> Ascii 13,10

Ultimatly only want to modify incoming <pol> to change "_" to "-" but baby steps, first is to wait for the STX before storing

What I have tried:

Ive tried moving Characters into a new string using mystring.char(x) but have no clue how to wait for the STX
Posted
Comments
RedDk 11-Oct-23 14:04pm    
Huh. I like it. The strikethrough use in this post. Isn't it a shame that using strikethrough on a text that is potentially indexed by some bot somewhere doesn't deindex the struck through text? Looking forward to the day ...

1 solution

We can't be specific, because "Visual-Studio" covers a wide range of languages and frameworks, from golden oldies like C to modern languages like C#, with a smattering of Javascript, F#, and even Python and PHP - and the specific details of what you need to do is going to be strongly language and framework bound.

But I'll give a general overview of how I handled such in the past.
Start by setting up a second (preferably background) thread to hand the "raw input" from the serial port.
It reads the stream character by character, scanning for your "start of message" indicator (STX in your case) then buffers the data until it gets the "end of message" (CR / LF in your case)
That buffer is then transferred up to the original thread for validation and processing before the background thread goes back to scanning for the "start of message".

That way, you separate the message transmission from the message data, and your main code gets complete messages to process when it's ready.
 
Share this answer
 
Comments
Andrew2112 11-Oct-23 12:27pm    
Im using visual Basic, do you have any code samples as far as the thread?
OriginalGriff 11-Oct-23 12:34pm    
Have a look at BackgroundWorker: it can pass whole messages back to the GUI via the progress event system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900