C
C#4w ago
JacTech

Opening serial port in windows service (Solved)

Hey, this is my "first" time coding anything other than Arduino. I'm trying to create a Windows service to open a serial port to my Arduino, so the Arduino knows when my PC is on or off. So far, so good. I've managed to create a Windows service, and it seems to open the COM port correctly (according to the event log). When the service is running, I'm (as expected) not able to connect to the Arduino via the serial monitor anymore, which further shows that the port is opened by the service. However, for some reason, the Arduino doesn't recognize that the port is open. The Arduino does correctly recognize it when I open the port using the serial monitor, but not when I open it in the service.
4 Replies
JacTech
JacTech4w ago
Here is the Windows Service code:
c#
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.ServiceProcess;
using System.Threading;

namespace JT_Controll_service
{
public partial class Service1 : ServiceBase
{
private SerialPort _serialPort;
private EventLog eventLog;

public Service1()
{
InitializeComponent();
eventLog = new EventLog();
if (!EventLog.SourceExists("JT_Controll_service"))
{
EventLog.CreateEventSource("JT_Controll_service", "Application");
}
eventLog.Source = "JT_Controll_service";
eventLog.Log = "Application";
}

protected override void OnStart(string[] args)
{
eventLog.WriteEntry("Service starting...");
try
{
_serialPort = new SerialPort("COM20", 115200); // Adjust COM port and settings as needed

_serialPort.Open();
if (_serialPort.IsOpen)
{
eventLog.WriteEntry("Serial Port is open");
}
}
catch (Exception ex)
{
eventLog.WriteEntry($"Error in OnStart: {ex.Message}", EventLogEntryType.Error);
}
}

protected override void OnStop()
{
try
{
if (_serialPort != null && _serialPort.IsOpen)
{
eventLog.WriteEntry("Serial Port was open");
_serialPort.Close();
}
}
catch (Exception ex)
{
eventLog.WriteEntry($"Error in OnStop: {ex.Message}", EventLogEntryType.Error);
}
}
}
}
c#
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.ServiceProcess;
using System.Threading;

namespace JT_Controll_service
{
public partial class Service1 : ServiceBase
{
private SerialPort _serialPort;
private EventLog eventLog;

public Service1()
{
InitializeComponent();
eventLog = new EventLog();
if (!EventLog.SourceExists("JT_Controll_service"))
{
EventLog.CreateEventSource("JT_Controll_service", "Application");
}
eventLog.Source = "JT_Controll_service";
eventLog.Log = "Application";
}

protected override void OnStart(string[] args)
{
eventLog.WriteEntry("Service starting...");
try
{
_serialPort = new SerialPort("COM20", 115200); // Adjust COM port and settings as needed

_serialPort.Open();
if (_serialPort.IsOpen)
{
eventLog.WriteEntry("Serial Port is open");
}
}
catch (Exception ex)
{
eventLog.WriteEntry($"Error in OnStart: {ex.Message}", EventLogEntryType.Error);
}
}

protected override void OnStop()
{
try
{
if (_serialPort != null && _serialPort.IsOpen)
{
eventLog.WriteEntry("Serial Port was open");
_serialPort.Close();
}
}
catch (Exception ex)
{
eventLog.WriteEntry($"Error in OnStop: {ex.Message}", EventLogEntryType.Error);
}
}
}
}
and if it helps the Arduino code, tho im sure it works:
c++
#include <Arduino.h>
#include <FastLED.h>

CRGB RGB[1];

#define APA102_SDI_PIN 23
#define APA102_CLK_PIN 22

void setup()
{
Serial.begin(115200);
FastLED.addLeds<SK9822, APA102_SDI_PIN, APA102_CLK_PIN, BGR>(RGB, 1);
FastLED.setMaxRefreshRate(0);
FastLED.setBrightness(100);
RGB[0] = CRGB::Red;
FastLED.show();
}

void loop()
{
if (Serial)
{
RBB[0] = CRGB::Green;
FastLED.show();
}
else
{
RGB[0] = CRGB::Red;
FastLED.show();
}
}
c++
#include <Arduino.h>
#include <FastLED.h>

CRGB RGB[1];

#define APA102_SDI_PIN 23
#define APA102_CLK_PIN 22

void setup()
{
Serial.begin(115200);
FastLED.addLeds<SK9822, APA102_SDI_PIN, APA102_CLK_PIN, BGR>(RGB, 1);
FastLED.setMaxRefreshRate(0);
FastLED.setBrightness(100);
RGB[0] = CRGB::Red;
FastLED.show();
}

void loop()
{
if (Serial)
{
RBB[0] = CRGB::Green;
FastLED.show();
}
else
{
RGB[0] = CRGB::Red;
FastLED.show();
}
}
ffmpeg -i me -f null -
from what i understand you are not really sending data, just monitoring a pin that should change state when port is open? because i don't exactly remember the default configuration of the serial port from windows but you could need to change it
Jimmacle
Jimmacle4w ago
dunno if it'll help, but i specifically check Serial.dtr() to check if a new connection was made i'm sure i did that for a reason but i can't remember exactly what it was that's using a MCU with built in USB being used as a serial port
JacTech
JacTech4w ago
Thanks for the replies, I figured it out. You have to enable drt on the C# code, for the Arduino to recognize the open port.
Want results from more Discord servers?
Add your server
More Posts