-3

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.

EgoPingvina
  • 734
  • 1
  • 10
  • 33
  • Have you considered using the [`SerialPort.DataReceived event`](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx)? – stuartd Dec 05 '16 at 16:15
  • @stuartd not yet. As said did not do much C# so far, will read into this. thanks – Christoph Poser Dec 05 '16 at 16:19
  • See eg [Serial Port Polling and Data handling](http://stackoverflow.com/questions/15124132/serial-port-polling-and-data-handling) for examples on usage – stuartd Dec 05 '16 at 16:39

1 Answers1

0

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.

Georg
  • 5,626
  • 1
  • 23
  • 44
  • You shouldn't be posting an answer to tell someone that their question isn't appropriate. If you feel the question isn't an appropriate question then post a *comment* to explain how it can be approved and vote/flag to close as appropriate. – Servy Dec 05 '16 at 16:22
  • oh okay, thanks for the info, ill look into your two tips. Should i delete the question if it is not suitable for this plattform? Quite new here, don't want to upset/offend anyone =) – Christoph Poser Dec 05 '16 at 16:26
  • @ChristophPoser http://codereview.stackexchange.com may be more appropriate for this kind of questions – Georg Dec 05 '16 at 16:30