arg_343
September 15, 2024, 3:29am
1
Hi all,
I'm very new to Arduino but, I have some experience with C#.
I am working through some very basic Serial Port communication examples.
So in VS 2022 I have:
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace Arduino_v2
{
public partial class Form1 : Form
{
SerialPort sp;
public Form1()
{
InitializeComponent();
sp = new SerialPort("COM4", 9600);
try
{
sp.Open();
}
catch (Exception)
{
Console.WriteLine("Unable to open COM port.");
}
}
private void btn_on_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.WriteLine("1");
}
}
private void btn_off_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.WriteLine("0");
}
}
}
}
In Arduino:
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
char input = Serial.read();
if(input == '1')
{
digitalWrite(LED_BUILTIN, HIGH);
}
if(input == '0')
{
digitalWrite(LED_BUILTIN, LOW);
}
delay(100);
}
}
For some reason this code will not work from VS 2022 or VS 2017.
I have turned FIFO buffers on and off in the Device Manager.
My RP 2040 is on COM4.
This code runs correctly on the RP 2040 from Arduino IDE.
I have tried setting the encoding to UTF8 that didn't help either.
I would like to start making a GUI to work with this device but I can't this basic code to work. The Output in VS2022 doesn't offer much help either.
Has anyone else ran into this? Most of the tutorials make this look very easy.
I purchased a brand new USB cable today, just to rule that out as well.
Please help, Thank you.
westfw
September 15, 2024, 3:35am
2
I'm not familiar with either VS or C#, but it looks pretty reasonable.
sp.WriteLine("0");
WriteLine() will presumably write a newline after sending the string you specified, but the Arduino code you posted should be ignoring that.
have you confirmed (ie by debugger) that the PC is doing the Write?
arg_343
September 15, 2024, 3:46am
3
I have stepped through the code in VS by breakpoint and the interesting part is the first time the write appears to go through, except it has no effect on the RP 2040 Connect, the LED never changes.
The second time either button is pressed the running forms app hangs, like its waiting for a response.
If I use:
sp.WriteTimeout = 5000;
I get an error message after 5 seconds:
"An unhandled exception of type 'System.IO.IOException' occurred in System.dll
The semaphore timeout period has expired."
This happens in both 2017 and 2022 regardless of any settings I change. I have tried it with a number of simple tutorials to the same result.
Thanks!
westfw
September 15, 2024, 4:08am
4
That certainly sounds like some sort of problem on the PC/C# side, beyond my knowledge.
Have you see Interfacing Arduino with C#/.NET - DEV Community ? Their Serial port initialization looks significantly different than yours.
arg_343
September 15, 2024, 4:14am
5
Well, here's the fix:
sp.DtrEnable = true;
Thread.Sleep(150);
using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace Arduino_v2
{
public partial class Form1 : Form
{
SerialPort sp;
public Form1()
{
InitializeComponent();
sp = new SerialPort("COM4", 9600);
try
{
sp.Open();
sp.WriteTimeout = 5000;
sp.DtrEnable = true;
Thread.Sleep(150);
}
catch (Exception)
{
Console.WriteLine("Unable to open COM port.");
}
}
private void btn_on_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.WriteLine("1");
Thread.Sleep(150);
}
}
private void btn_off_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.WriteLine("0");
Thread.Sleep(150);
}
}
}
}
The strange thing is that none of the tutorials or code examples I watched or tried mentioned either of these and their code appeared to work fine.
Thanks for helping!
1 Like
system
Closed
March 14, 2025, 4:15am
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.