Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm doing a project about control dc motor with c# and arduino.
I want to send setspeed to arduino by using textbox,but the textbox didn't work like i want.

What I have tried:

Here the code

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
            if (serialPort1.IsOpen)
                
                serialPort1.WriteLine("vs_set_speed"+textBox1.Text);
        }



And this is the arduino code:

C++
if (mySt.substring(0,12) == "vs_set_speed"){
    set_speed = mySt.substring(12,mySt.length()).toFloat();


It works fine,but here the problem: when i type setspeed in textbox, for example 200, the setspeed chart goes to 2, and then 20, and then 200.

Is there any way the textbox get the number after i press enter or press a button ?
Please help me, thanks !
Posted
Updated 8-Jul-20 3:42am
v2
Comments
Richard MacCutchan 8-Jul-20 9:16am    
It sounds like the arduino function gets called for each received character on the serial port. you need to get the arduino to accumulate the text and not try to set the speed until all numbers have been received.
Member 14884114 8-Jul-20 9:21am    
actually the arduino code have this function to do exactly what you say :

void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
if (inChar != '\n') {
mySt += inChar;
}
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}

Richard MacCutchan 8-Jul-20 9:27am    
Sorry, my mistake, it's the C# code that is wrong. You are sending the text every time a character is added to the TextBox. You need to only send it when there is no more data to add. Probably best to use a button to trigger the send.

Use the KeyPress() event instead, see answer here: keydown - Detect Enter Key C# - Stack Overflow[^]
 
Share this answer
 
So i changed textbox event to keydown() like this and it works.

C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setspeed = textBox1.Text;
                if (serialPort1.IsOpen)
                {

                    serialPort1.WriteLine("vs_set_speed" + setspeed);

                }
            }
        }


Thanks for all the help .
 
Share this answer
 
Use something explicit, at best a Button's Click event.

You should not use an XxxChanged event for controlling peripherals, there will always be some unexpected events:
- while you enter N characters there can be anywhere between 1 and N changed events firing;
- and later when you try and replace it by a new value there will be another one: clearing a TextBox will also trigger the TextChanged event!

And don't forget to validate the input before sending it to the peripheral.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900