Answer the question
In order to leave comments, you need to log in
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;
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 = "Заново";
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);
}
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;
}
}
}
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);
Answer the question
In order to leave comments, you need to log in
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:
{}
SerialPort port;
port = new SerialPort("COM12", 9600);
port.Open();
port.WriteLine(textBox1.Text);
port.Close();
Out of bounds array:
int arr[4];
....
while (i < 5)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question