JacTech
JacTech
CC#
Created by JacTech on 6/12/2024 in #help
Opening serial port in windows service (Solved)
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.
8 replies
CC#
Created by JacTech on 6/12/2024 in #help
Opening serial port in windows service (Solved)
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();
}
}
8 replies
CC#
Created by JacTech on 6/12/2024 in #help
Opening serial port in windows service (Solved)
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);
}
}
}
}
8 replies