I am trying to resolve a serial communication issue. I've found that other people have had this same problem and I've tried many different solutions with luck. The board is a Mega, and I'm trying to build an app in C#. I've discovered that in C# the Handshaking property has to be "none" and DtsEnabled and RtsEnabled properties have to be set to true. But, this didn't solve the issue. I know the Arduino sketch works because the Arduino serial monitor sends data with not problem. My suspicion is that C# is sending too many characters. I have tried 9600 and 115200 baudrates, and checked and rechecked all of the port properties. I know the C# app does connect to Arduino because the Arduino LED flashes when it does. But, then it times out (I think).
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for USB port to connect.
}
}
void loop()
{
initialize();
if (initialized)
{
blinker();
}
}
void initialize()
{
if (Serial.available() > 0)
{
incoming_string = Serial.readString();
}
if (incoming_string.toInt() == 100)
{
initialized = true; //Serial.write("1. CONNECTED\n"); // I tried this with and without
}
delay(100);
}
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Some remarks about your c# code:
Declare the port and portName and open the port at the beginning of your program
Keep it open until you close the program.
This is an example:
using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;
namespace Serial_example
{
public partial class Form1 : Form
{
//Replace "COMYourport" with the port in use (COM1 or COMwhatever).
//new SerialPort(string portName, int Badrate, Parity parity, int dataBits, StopBits stopBits)
private SerialPort SerialPort = new SerialPort("COMYourport", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
// Always use try catch to prevent crashing if port doesn't exist and
// check if port is already open.
try
{
if (SerialPort.IsOpen)
{
SerialPort.Close();
}
SerialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void INIT_button_Click(object sender, EventArgs e)
{
SerialPort.Write("100");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SerialPort.Close();
}
}
}
peternic:
I am not sure why, but I believe that opening the port when the form loaded is what made the difference.
Normally the Arduino resets when the PC program opens the serial port and the PC program must allow a few seconds for the reset to complete before sending data. This also means that the PC program should open the serial port and keep it open until it is completely finished with the Arduino.
Robin2:
Normally the Arduino resets when the PC program opens the serial port and the PC program must allow a few seconds for the reset to complete before sending data. This also means that the PC program should open the serial port and keep it open until it is completely finished with the Arduino.
...R
I'm using the software Br@ys Terminal as serial monitor.
Bray's terminal is configurable to even automatically open/close the serial connection through winning/loosing focus (=beeing the window in the foreground/backround) without resetting the Arduino-Board.
This is very convenieant if you use another editor for the code as f.e. notepad++
On the internet you will find version 2014. unfortunately in this version autoscroll does not work
so I attach version 2012 where autoscroll works
you can also log the received content it has macros and even a scripting language for simulating the partner-device
best regards Stefan