It was pretty difficult to understand your concerns. Anyway, by all means, avoid such thing as to "pass those parameters directly to the methods in Form 1". Just the opposite, you should isolate UI from other aspects of functionality as much as possible.
In the code you show, you should have at least three distinct and isolated parts: 1) serial communication, 2) parsing of data coming from serial port (probably, you also have to feed some data to serial communication, but you did not describe this part); 3) UI.
You should abstract those parts from each other. As to the serial communications, in most cases you will need to have it in a separate thread, do avoid mixing timing, logic and keep UI responsive. Why do you use
BeginInvoke
in your code? Does it mean that you already use a thread? In a single thread,
Invoke
or
BeginInvoke
is not required.
Why
BeginInvoke
, not
Invoke
? Sort it out. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
Your code has many problems. You long "if" statement is based on a number of hard coded
immediate constants 'I', 'M', 'T', etc. This is not supportable. At least declare them explicitly as constants. Do you guarantee that
f[1]
always exists? What about
f[0]
? Never use names like
serialPort1_DataReceived
,
Form1
and the like. They violate (good) Microsoft naming conventions. Do yourself a big favor: meed them: no underscore, no numerals. Generally, those auto-generated names are not intended to be used in real code: why do you think you are provided with the refactoring language? Use it to rename all names to something semantic, as soon as you start actually use the names. Also, don't use "", use
string.Empty
; you will see the convenience of it when you get rid of immediate constants. Finally, your set of many
out
parameters is inconvenient. Better put them all in some structure/class you could return using one
return
statement; this would greatly simplify your code. Why are all the parameters are strings? Don't you think you are trying to work with strings representing data instead of data itself? If so, parse all strings to data immediately and discard them.
—SA