Skip to main content
edited tags
Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177
Added missing methods to understand the code
Source Link
Chris
  • 75
  • 1
  • 1
  • 6

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 waitThread.Sleep().

The code is working as I want it to for several input tests. I just thought there might be a better way than the wait().

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().

added 203 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Efficieny of C# Code (Serial Serial Port Communication, avoiding .Wait() )

This is an efficienyefficiency question to the following code, writtewritten in MS Visual in C#. If any explanation is missing, please let me know and I will add. What this part of the programmprogram does is:

  1. Button is pushed
  2. Motor starts moving (by sending 9-byte array with command)
  3. Waiting for Sensor to register a change in value
  4. Motor stops
  5. Motorposition is saved (command to get position is send and 9-byte array response is send by the motor), to have accesaccess to it later on and return there

Calls like rotate(), stop()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 stackoverflowStack Overflow that "if you have to use wait()wait() and there is no point of using the DataReceivedDataReceived implementation". So should iI really get rid of this or is there a nice way to handle this? If I do it without the wait()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 onDataReceived in step 3? In the beginning I save a value as new 0, and compare the current sensor readout here to this 0 value. Now for the code:

    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);
    }
    //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;         
    }

Thanks in advance for helpful tipps. CodeThe code is working as iI want it to for several input tests. I just thought there might be a better way than the wait()wait().

Efficieny of C# Code (Serial Port Communication, avoiding .Wait() )

an efficieny question to the following code, writte in MS Visual in C#. If any explanation is missing, please let me know and I will add. What this part of the programm does is:

  1. Button is pushed
  2. Motor starts moving (by sending 9-byte array with command)
  3. Waiting for Sensor to register a change in value
  4. Motor stops
  5. Motorposition is saved (command to get position is send and 9-byte array response is send by the motor), to have acces to it later on and return there

Calls like rotate(), 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 stackoverflow that "if you have to use wait() 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 on step 3? In the beginning I save a value as new 0, and compare the current sensor readout here to this 0 value. Now for the code:

    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);
    }
    //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;         
    }

Thanks in advance for helpful tipps. Code is working as i want it to for several input tests. I just thought there might be a better way than the wait().

Serial Port Communication

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:

  1. Button is pushed
  2. Motor starts moving (by sending 9-byte array with command)
  3. Waiting for Sensor to register a change in value
  4. Motor stops
  5. 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);
}
//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;         
}

The code is working as I want it to for several input tests. I just thought there might be a better way than the wait().

Source Link
Chris
  • 75
  • 1
  • 1
  • 6
Loading