This is an efficiency question to the following code, written in MS Visual in C#. If any explanation is missing, please let me know and I will add. What this part of the program does is:
- Button is pushed
- Motor starts moving (by sending 9-byte array with command)
- Waiting for Sensor to register a change in value
- Motor stops
- Motorposition is saved (command to get position is send and 9-byte array response is send by the motor), to have access to it later on and return there
Calls like rotate()
and stop()
are commands that write data into a 9-byte-array and write bytearray to serial port.
Now, my main issue is Step 5. I read on Stack Overflow that "if you have to use wait()
and there is no point of using the DataReceived
implementation". So should I really get rid of this or is there a nice way to handle this? If I do it without the wait()
it saves the answer that is send after the stop command (every command also sends an answer).
And would it be more efficient to use DataReceived
in step 3? In the beginning I save a value as new 0, and compare the current sensor readout here to this 0 value.
private void button13_Click(object sender, EventArgs e)
{
//rotate right speed
string Test = textBox4.Text;
byte[] A = new byte[9];
rotate(A, Test);
//read sensor
serialPort2.WriteLine("SR,00,002\r\n");
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);
// stop motor
byte[] B = new byte[9];
stop(B);
//save position
System.Threading.Thread.Sleep(5000);
byte[] C = new byte[9];
getPosi(C);
System.Threading.Thread.Sleep(5000);
M = buf;
//move to 0
byte[] D = new byte[9];
MoveTo0(D);
}
In case it is needed for understanding the code above:
//global variables
byte[] buf = new byte[9];
byte[] M = new byte[9];
private void button1_Click(object sender, EventArgs e) //Ports öffnen
{
serialPort3 = new SerialPort();
serialPort3.PortName = "COM6";
serialPort3.Handshake = Handshake.RequestToSend;
serialPort3.ReceivedBytesThreshold = 8;
serialPort3.DataReceived += new SerialDataReceivedEventHandler(DataRecievedHandler);
serialPort3.Open();
}
private void DataRecievedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int bytes = serialPort3.BytesToRead;
byte[] buffer = new byte[bytes];
sp.Read(buffer, 0, bytes);
string hex = BitConverter.ToString(buffer);
hex.Replace("-", "");
MessageBox.Show(hex);
buf = buffer;
}
Other methods:
private void rotate(byte[] A, string Test) //Rotate Motor Right
{
A = MB.TMCL_RMR(Test);
serialPort3.Write(A, 0, A.Length);
}
private void stop(byte[] B) // Stop Motor
{
B = MB.TMCL_MST();
serialPort3.Write(B, 0, B.Length);
}
private void getPosi(byte[] C) //Get Position
{
C = MB.GET_POSI();
serialPort3.Write(C, 0, C.Length);
}
private void MoveTo0(byte[] D) //Move to 0
{
D = MB.MoveTo0();
serialPort3.Write(D, 0, D.Length);
}
And as example a method from my seperate motorcontrolclass on how the bytearray is created. Each hex-byte references a certain information, last byte is a checksum. If this array is send to the serial port it will request the motorposition as an answer.
public byte[] GET_POSI()
{
byte[] E = new byte[9];
E[0] = 0x1;
E[1] = 0x6;
E[2] = 0x1;
E[3] = 0x0;
E[4] = 0x0;
E[5] = 0x0;
E[6] = 0x0;
E[7] = 0x0;
E[8] = 0x08;
return E;
}
The code is working as I want it to for several input tests. I just thought there might be a better way than the Thread.Sleep()
.