简体   繁体   中英

Serial Writing c# windows forms Arduino

I'm trying to communicate with my Arduido using a windows forms application via serial port, I'm not getting an error but It's not working for some reason.

I know its not the serial connection or the arduino because I have tried another vb code to try to toggle a led lamp on and off and succeeded so the problem is in my c# code

I have imported the proper namespace

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

I have defined a serial port object in the form class

partial class Form1 : Form
    {

        SerialPort sp = new SerialPort();
        ...

and configured it in the load event

private void Form1_Load(object sender, EventArgs e)
{
    sp.Close();
    sp.PortName = "com7";
    sp.BaudRate = 9600;
    sp.DataBits = 8;
    sp.Parity = Parity.None;
    sp.StopBits = StopBits.One;
    sp.Handshake = Handshake.None;
    sp.Encoding = System.Text.Encoding.Default;
    ...

And I have used the keyPress event to try to write to the serial port

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{

    if (e.KeyChar == (char)Keys.Up)
    {
        sp.Open();
        sp.Write("u");
        sp.Close();
    }
    else if (e.KeyChar == (char)Keys.Down)
    {
        sp.Open();
        sp.Write("d");
        sp.Close();
    }
    else if (e.KeyChar == (char)Keys.Right)
    {
        sp.Open();
        sp.Write("r");
        sp.Close();
    }
}

Any help, thanks Note: In my app there is a dialog box that is responsive to the keys I'm using (Up and Down) does it conflict with the keypress event? and how can I solve it

I'd not open/close my serial port every time i need to send command.

you better use the flow:

  1. add another button, say "Open" button.
  2. use it to open your serial port (usually once you start the software you want to open the port ,ie starts communication with your arduino controller or whatever)

    private void OpenButton_Click(object sender, EventArgs e) { try { if (sp.IsOpen == false) { sp.Open();
    } else { MessageBox.Show("port 7 is already opened"); } } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } }

  3. Now whenever you press a button you need only to send the command (write to your com port), without open/close :

    private void Form1_KeyPress(object sender, KeyPressEventArgs e) {

     if (e.KeyChar == (char)Keys.Up) { //sp.Open(); sp.Write("u"); //sp.Close(); } else if (e.KeyChar == (char)Keys.Down) { //sp.Open(); sp.Write("d"); //sp.Close(); } else if (e.KeyChar == (char)Keys.Right) { //sp.Open(); sp.Write("r"); //sp.Close(); } 

    }

  4. Now if you did it like this and still doesn't work, you need to check whether the commands are right or not,, may be sp.Write("u"); doesn't do anything !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM