c#


Efficiency of “Eventhandling”


Good evenig, using C# in MS Visual Community 2015 is there a better way to implement following problem? I'm quite sure there is, but I'm new to C# so hence the question.
private void button6_Click(object sender, EventArgs e)
{
byte[] a = new byte[9]; // this is just command for motor
a[0] = 1; // to start rotating
a[1] = 1; //
a[2] = 0; //
a[3] = 0; //
a[4] = 0; //
a[5] = 0; //
a[6] = 0; //
a[7] = 63; //
a[8] = 65; //
serialPort3.Write(a, 0, a.Length);
string b = serialPort2.ReadLine();
decimal caliber = decimal.Parse(Regex.Split(b, "SR,00,002,")[1]);
decimal b1 = 0;
do
{
serialPort2.WriteLine("SR,00,002\r\n");
string z = serialPort2.ReadLine();
b1 = decimal.Parse(Regex.Split(z, "SR,00,002,")[1]);
}
while (b1 <= caliber);
byte[] c = new byte[9]; // this is command to stop rotating
c[0] = 1; //
c[1] = 3; //
c[2] = 0; //
c[3] = 0; //
c[4] = 0; //
c[5] = 0; //
c[6] = 0; //
c[7] = 0; //
c[8] = 04; //
serialPort3.Write(c, 0, c.Length);
}
I have the motor commands as extra functions, this is just for testing. My goal is to rotate the motor until the SerialPort returns a change in value. The serialPort2 is a Sensor and has a default value of around -3500 (always up to change a bit, hence i set it as caliber). The motor moves the sensor. I want the motor to stop as soon as the value changes from the caliber(and the motorposition will be saved).
My code works as planned, and on my machine quite quick, but I'm not sure if its efficient this way as it must check the Serial Port 2 super often.
I would also create an extra function to read and return data from the serial port. In my understanding it would not change the way the code runs, except for looking shorter.
This part of my programm is only used for one starting calibration and it's not used so often, but in case similar things, like "do event A until specific event happens", should be needed later on, and out of interest for the way C# works I'd appreciate your opinion/help on this.
I see two problems with your code:
Regex.Split will compile the pattern each time you call it, meanwhile you always call it with the same pattern. You can save computation time by reusing a Regex-instance that holds the already compiled pattern
You could use a BitConverter to do the translation of command codes etc. to a binary format and assign constants to the command codes. This would make your code much more readable.
Furthermore, Stack Overflow is not meant as a code review platform, so please ask more specific questions.

Related Links

How to set the DisplayMemberPath in a DataGrid?
Search a list of objects for any and all matches
Searching controls by name in Windows Phone c# application
Can I periodically run a background task in WP8 without my app running?
System.Nullable<System.TimeSpan>' does not contain a definition for 'TotalMinutes'
Instantiate a class (or derived) with internal constructor
SqlMembershipProvider not able to login with frames or iframes only in Internet Explorer
MonoMac Sqlite version
How do you embed a C# Form into HTML?
How to get values from dynamically created elements using jquery in code behind
Capturing KeyDown and KeyUp events for cursor keys
Pocket PC SQL Server connection error
Update multiple pictureboxes at the same time
Use of DOM/SAX instead of XmlSerializer over HttpWebResponse
Casting Linq Query Results to an Interface
Get first KeyValuePair in a dictionary that has a specific value

Categories

HOME
gulp
kendo-ui
abc
file-upload
glsl
data-modeling
pandoc
snmp
msbi
foreach
gnuradio
websharper
ng2-charts
box2d
brightway
airbnb
gomobile
ejb-3.1
angular2-pipe
procedural-generation
poisson
hierarchy
jbossfuse
multilanguage
ng2-bootstrap
circle
outlook-2010
libconfig
idea-plugin
rbac
bootstrap-carousel
google-chrome-storage
confluent
jscoverage
kendo-scheduler
selection-sort
programming-pearls
cc
blackberry
therubyracer
optional-parameters
alfred
avi
transaction-isolation
abstract
containstable
google-app-engine-php
personality-insights
exchange-server-2007
eaaccessory
polarion
wingdb
crystal-reports-7
javadb
redmine-plugins
triplestore
autofilter
xcode6.1-gm-seed
google-mirror-api
xcode5.1
pechkin
aspnet-compiler
static-class
oc4j
gssapi
socketasynceventargs
maven-eclipse-plugin
httppostedfilebase
openwrap
overlays
file-processing
noise-reduction
zend-acl
bootstrap-protocol
memory-consumption

Resources

Encrypt Message