M
M
mIka012021-04-21 20:28:58
Arduino
mIka01, 2021-04-21 20:28:58

How to send values ​​over SerialPort to Arduino using c#?

Hello, help me solve a seemingly simple problem.
I have an Iskra Neo microcontroller.
It is necessary to transfer information from the textBox via SerialPort to Arduino. There are 4 variables in total.
I'm stupid or something but I can't do it.

Please suggest your code.

My code. Non-working.
Libraries.

using System.Threading;
using System.IO.Ports;

The code.
SerialPort port;
            int timeout = 100;
            port = new SerialPort("COM7", 9600);
            port.Open();
            port.ReadTimeout = timeout;

            textBox3.Text = "Начало";
            port.WriteLine(textBox1.Text);
            while (port.BytesToRead <= 0) { Thread.Sleep(1000); }
            port.WriteLine(textBox2.Text);
            while (port.BytesToRead <= 0) { Thread.Sleep(1000); }
            port.WriteLine(textBox6.Text);
            while (port.BytesToRead <= 0) { Thread.Sleep(1000); }
            port.WriteLine(textBox5.Text);
            while (port.BytesToRead <= 0) { Thread.Sleep(1000); }
            textBox3.Text = "Конец";
            Thread.Sleep(1000);
            textBox3.Text = "Заново";

6080609fdf3d5629122350.png
Arduino.
void setup() {
      Serial.begin(9600);
      Serial.setTimeout(100);
}

void loop() {
  
  int arr[4];
  int i = 0;
  while (i < 4) {
    if (Serial.available()) {
    arr[i] = Serial.parseInt(); 
    Serial.println("ещё");  
    i++;    
    }
  }   
  turn(arr[0], arr[1], 1, 2, 3, 4, 18); 
  turn(arr[2], arr[3], 6, 7, 8, 9, 18); 
}

Function.
void turn(int a, int velocity, byte shim, byte in1, byte in2, int encoderIn, int holes)
{
  int detectState = 0;
  bool freq;
  int longer = 0;

  pinMode(encoderIn, INPUT);
  detectState = digitalRead(encoderIn);
  freq = detectState;

  bool executed = true;
  int tics = (abs(a) * 2) / (360 / holes);

  pinMode( shim, OUTPUT );
  pinMode( in1, OUTPUT );
  pinMode( in2, OUTPUT );
  analogWrite( shim, velocity );

  if (a < 0) {
    digitalWrite( in1, HIGH );
    digitalWrite( in2, LOW );
  } else {
    digitalWrite( in1, LOW );
    digitalWrite( in2, HIGH );
  }



  while (executed) {
    detectState = digitalRead(encoderIn);

    if (detectState != freq) {
      longer++;
      freq = detectState;
    }

    if (longer > tics) {
      digitalWrite( in1, LOW );
      digitalWrite( in2, LOW );
      executed = false;
    }

  }

}


However, if someone may have an opinion that turn does not work, then you are mistaken.
Here is an example of a working code.
void setup() {
      Serial.begin(9600);
      Serial.setTimeout(100);
}

void loop() {
  while (true) {
    if (Serial.available()) {
       turn(Serial.parseInt(), 255, 1, 2, 3, 4, 18); 
       goto d;
    }
  }   
d:{}
}

turn from the previous code.

SerialPort port;
            int timeout = 100;
            port = new SerialPort("COM7", 9600);
            port.Open();
            port.ReadTimeout = timeout;
            port.WriteLine(textBox1.Text);


Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mIka01, 2021-05-31
@mIka01

I got it like this.
Arduino.

Serial.begin(9600);
      Serial.setTimeout(100);
    while (true) {
    if (Serial.available()) {   
    turn(Serial.parseInt(), 100, 2, 3, 4, 5, 18);
    goto g0;
    }
  }   
   g0:
   {}

And the code for c#.
SerialPort port;
            port = new SerialPort("COM12", 9600);

            port.Open();            
            port.WriteLine(textBox1.Text);
            port.Close();

V
Vladimir Kozhevin, 2021-04-22
@vovik0

Out of bounds array:

int arr[4];
....
while (i < 5)

well, goto d; It's better not to use it at all, especially in your example. Simple break; more than enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question