none
Sending a string to an Arduino board by use of serial

    Question

  • Hello all.

    I have written an Arduino switch code that uses a / separator to define the case for respective layer.  For example, I can send "1/1/4/1" to the board by use of the serial monitor and that would turn Pin4 HIGH.  Sending "1/1/4/0/" would turn Pin4 LOW.  This code is used to control servo motors and air pumps.

    In order to more easily control the servos and pumps and I am trying to write a Windows Form in C# with buttons that when pressed will send a string, such as "1/1/4/1" to the serial port to activate one of the functions in the Arduino code.  I am having difficulty with this as this is my first C# project.  I've tried several methods (I'm weary to use that word because I know it means something else in Visual Studio) but to no avail.  Example code for two of my buttons can be seen below, where I attempted to use two different codes to send the string.  Neither of them worked.

    //I've truncated the other using statements out of this example code
    using System.IO.Ports;

    namespace MyProject
    {
       public partial class Form1 : Form
       {
          SerialPort serialPort1;

          public Form1()
          {
             InitializeComponent();
             serialPort1 = new SerialPort();
             serialPort1.PortName = "COM10";
             serialPort1.BaudRate = 9600;
             serialPort1.Open();
          }

          private void gripObjBtn_Click(object sender, EventArgs e)
          {
             serialPort1.Write("1/1/4/1/");
          }
    private void dropArmBtn_Click(Object sender, EventArgs e)
    {
       if(!serialPort1.IsOpen == true)) serialPort1.Open();
       string value = "1/1/4/1/";
       serialPort1.Write(value);
    }
    }

    Can anyone please help me get this working? I would greatly appreciate any guidance.

    Thanks

    Thursday, January 12, 2012 7:05 AM

Answers

  • The combo box is just so you could try all the different ports to see if you had the wrong one. I'm not an expert on serial ports, but I believe you need all of the port options to match up. If they don't, I would think you'll either receive (and send) nothing or gibberish. Here's an article involving serial ports and robots, try the serial port settings in the code examples:

     

    http://msdn.microsoft.com/en-us/library/bb483043.aspx

    Thursday, January 12, 2012 5:25 PM

All replies

  • Surely there has to be someone who can explain why my code above doesn't work, and the correct way to send a string to the serial port!
    Thursday, January 12, 2012 4:12 PM
  • I think the reason no one's responded is because the code is right. Are you sure you're using the right port, baud rate, etc...? There are other options you can set as well, such as the number of parity bits and type of handshaking, it all depends on how the board does serial.

    If I were you, I would fill a combo box with all the available ports (using SerialPort.GetPortNames()) and have a connect button which opens the selected port. You also shouldn't need to open the serial port in the button handler if you already did it in the constructor.

    I would also recommend some kind of greeting protocol so you can confirm you're connected to the right device, so you could send it a certain message and it would respond with a certain message.

    Basically the code you have is correct, all you need to do is open the port and write. I'm assuming the problem is with the actual connection, you don't get any errors if there's nothing connected to the port or if the settings are wrong.

    Thursday, January 12, 2012 4:22 PM
  • That is what I thought as well.  I have not been receiving errors when compiling.  I have the Arduino Visual Micro plug-in installed which emulates the Arduino IDE inside of Visual Studio.  When I open up the Arduino serial monitor, inside of Visual Studio, and send my string commands to COM10, using BaudRate = 9600 (Arduino Uno), the servos and air pumps respond as I would like.  For some reason, the board isn't receiving the string commands when I send it through my C# code.

    I'm definitely a beginner with both C# and VS, could you explain why a combo box would be beneficial to me (and perhaps, how I could implement it), and why changing all of those other port options could help?  I was under the impression that as long as the COMport and BaudRate were right there shouldn't be any problems.

    Thursday, January 12, 2012 4:42 PM
  • The combo box is just so you could try all the different ports to see if you had the wrong one. I'm not an expert on serial ports, but I believe you need all of the port options to match up. If they don't, I would think you'll either receive (and send) nothing or gibberish. Here's an article involving serial ports and robots, try the serial port settings in the code examples:

     

    http://msdn.microsoft.com/en-us/library/bb483043.aspx

    Thursday, January 12, 2012 5:25 PM
  • Serial ports require that you not only set the baud rate, but also the number of data bits (usu. 7 or 8), the parity (usu. even, odd, none), and the number of stop bits (usu 1 or 2)

    These values are determined by the type of device to which you are connecting.  Additionally, all serial cables are not created equal.  Make sure that you have the correct pin connections as required for the board.

    Also, pay attention to any handshake requirements.  If the hardware handshake pins aren't jumped, hardware handshaking is probably required.

    Your IDE plugin probably ignores these requirements, but your serial port won't.

    As far as the framing settings, the two most common are (with the baud rate at 9600)

    9600 baud, 8 data bits, None parity, 1 stop bit

    9600 baud, 7 data bits, Even parity, 2 stop bits

    • Edited by CS001 Friday, January 13, 2012 10:15 PM
    Friday, January 13, 2012 10:13 PM
  • I would set DTR and RTS, I've had devices sit unresponsive until I set or "unset" DTR,RTS as needed on port Init at the very least.
    Saturday, January 14, 2012 5:33 AM
  • Hi MapleYamCakes,

    Welcome to the MSDN forum!

    How is it going with the problem currently?

    Please refer to the SerialPort Class at the same time:

    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

    If you need any help, please feel free to let us know.

    Have a nice day!

    yoyo

     

     


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 17, 2012 6:24 AM
    Moderator
  • Good to hear you are using the arduino plugin for visual studio http://www.visualmicro.com/

    We need to see your arduino code.

    Are you sure you have opened the arduino serial at a speed of 9600?

    Is your arduino code expecting line endings such as cr or lf?

    try this with \r\n

    serialPort1.Write("1/1/4/1/\r\n");

    You don't need to enable dtr or rts unless you want the arduino to re-boot.

    The default .net c# serial settings should work fine and you should be able to connect to your arduino without problem

    If the problem is none of the above then let us know what settings show on the visual micro serial window in vs (port, speed, line endings?)

     



    • Edited by Visual Micro Thursday, February 02, 2012 2:01 AM
    Thursday, February 02, 2012 1:55 AM