From 99698b677a05e86b239664c7392b7d7aad2aa04d Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Mon, 6 Jan 2025 17:42:59 -0600 Subject: [PATCH] added a couple modbus samples --- Source/Meadow.Samples.sln | 60 +++++++++++++++++++ .../KellerXLine/KellerXLine_Sample.csproj | 14 +++++ Source/Modbus/KellerXLine/Program.cs | 36 +++++++++++ Source/Modbus/StepperMotor/Program.cs | 36 +++++++++++ .../StepperMotor/StepperMotor_Sample.csproj | 14 +++++ .../KellerXLine/KellerXLineSample.csproj | 24 ++++++++ Source/ProjectLab/KellerXLine/MeadowApp.cs | 43 +++++++++++++ Source/ProjectLab/KellerXLine/app.build.yaml | 2 + Source/ProjectLab/KellerXLine/app.config.yaml | 21 +++++++ .../ModbusStepperMotor/MeadowApp.cs | 41 +++++++++++++ .../ModbusStepperMotor_Sample.csproj | 24 ++++++++ .../ModbusStepperMotor/app.build.yaml | 2 + .../ModbusStepperMotor/app.config.yaml | 21 +++++++ 13 files changed, 338 insertions(+) create mode 100644 Source/Modbus/KellerXLine/KellerXLine_Sample.csproj create mode 100644 Source/Modbus/KellerXLine/Program.cs create mode 100644 Source/Modbus/StepperMotor/Program.cs create mode 100644 Source/Modbus/StepperMotor/StepperMotor_Sample.csproj create mode 100644 Source/ProjectLab/KellerXLine/KellerXLineSample.csproj create mode 100644 Source/ProjectLab/KellerXLine/MeadowApp.cs create mode 100644 Source/ProjectLab/KellerXLine/app.build.yaml create mode 100644 Source/ProjectLab/KellerXLine/app.config.yaml create mode 100644 Source/ProjectLab/ModbusStepperMotor/MeadowApp.cs create mode 100644 Source/ProjectLab/ModbusStepperMotor/ModbusStepperMotor_Sample.csproj create mode 100644 Source/ProjectLab/ModbusStepperMotor/app.build.yaml create mode 100644 Source/ProjectLab/ModbusStepperMotor/app.config.yaml diff --git a/Source/Meadow.Samples.sln b/Source/Meadow.Samples.sln index f22cb20f..f4dbf60a 100644 --- a/Source/Meadow.Samples.sln +++ b/Source/Meadow.Samples.sln @@ -579,6 +579,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CellularSample", "ProjectLa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalSignalAnalyzer", "Meadow F7\IO\DigitalSignalAnalyzer\DigitalSignalAnalyzer.csproj", "{55594F06-C48C-46C0-84BA-23C364842E7F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StepperMotor_Sample", "Modbus\StepperMotor\StepperMotor_Sample.csproj", "{3F8BB18D-517E-4C3D-B140-F076A496055B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Motors.StepperOnline", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\Motors.StepperOnline\Driver\Motors.StepperOnline.csproj", "{319CC18B-525B-4833-8D8D-B280E37B6A08}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusStepperMotor_Sample", "ProjectLab\ModbusStepperMotor\ModbusStepperMotor_Sample.csproj", "{C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sensors.Environmental.Keller.XLine", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\Sensors.Environmental.Keller.XLine\Driver\Sensors.Environmental.Keller.XLine.csproj", "{C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KellerXLine_Sample", "Modbus\KellerXLine\KellerXLine_Sample.csproj", "{278CDA85-E114-4113-AC78-76103FFF8C47}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KellerXLineSample", "ProjectLab\KellerXLine\KellerXLineSample.csproj", "{72180A60-F721-42BD-93C9-DC58544CEDD9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -2615,6 +2627,48 @@ Global {55594F06-C48C-46C0-84BA-23C364842E7F}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU {55594F06-C48C-46C0-84BA-23C364842E7F}.Simulation|Any CPU.Build.0 = Debug|Any CPU {55594F06-C48C-46C0-84BA-23C364842E7F}.Simulation|Any CPU.Deploy.0 = Debug|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Release|Any CPU.Build.0 = Release|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {3F8BB18D-517E-4C3D-B140-F076A496055B}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Debug|Any CPU.Build.0 = Debug|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Release|Any CPU.ActiveCfg = Release|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Release|Any CPU.Build.0 = Release|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {319CC18B-525B-4833-8D8D-B280E37B6A08}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Release|Any CPU.Build.0 = Release|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Release|Any CPU.Build.0 = Release|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Release|Any CPU.Deploy.0 = Release|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2}.Simulation|Any CPU.Deploy.0 = Debug|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Debug|Any CPU.Build.0 = Debug|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Release|Any CPU.ActiveCfg = Release|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Release|Any CPU.Build.0 = Release|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {278CDA85-E114-4113-AC78-76103FFF8C47}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Release|Any CPU.Build.0 = Release|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Release|Any CPU.Deploy.0 = Release|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Simulation|Any CPU.ActiveCfg = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Simulation|Any CPU.Build.0 = Debug|Any CPU + {72180A60-F721-42BD-93C9-DC58544CEDD9}.Simulation|Any CPU.Deploy.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2893,6 +2947,12 @@ Global {44E9AEA0-C0E6-4C5F-93FD-3210255320B6} = {A809F07D-DD94-4375-8776-23FBE73591A5} {9628FF7D-2852-41F9-A8C4-2B5B0012B0FA} = {DA0CC626-D072-457F-89B7-C22427D4C775} {55594F06-C48C-46C0-84BA-23C364842E7F} = {0B3C904B-B328-4314-9BAC-E6E3D7B523CC} + {3F8BB18D-517E-4C3D-B140-F076A496055B} = {3B2ADC8C-1ACB-49F3-8A3C-4F453FF9FE75} + {319CC18B-525B-4833-8D8D-B280E37B6A08} = {A809F07D-DD94-4375-8776-23FBE73591A5} + {C1F7C00C-5130-442E-B6B4-EB588F9CBB7F} = {DA0CC626-D072-457F-89B7-C22427D4C775} + {C16D6D8B-F53B-4B6F-A41C-6DAD02BB12D2} = {A809F07D-DD94-4375-8776-23FBE73591A5} + {278CDA85-E114-4113-AC78-76103FFF8C47} = {3B2ADC8C-1ACB-49F3-8A3C-4F453FF9FE75} + {72180A60-F721-42BD-93C9-DC58544CEDD9} = {DA0CC626-D072-457F-89B7-C22427D4C775} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E3F002EA-1A25-487F-9A5D-93D1E7EC6E31} diff --git a/Source/Modbus/KellerXLine/KellerXLine_Sample.csproj b/Source/Modbus/KellerXLine/KellerXLine_Sample.csproj new file mode 100644 index 00000000..8d304761 --- /dev/null +++ b/Source/Modbus/KellerXLine/KellerXLine_Sample.csproj @@ -0,0 +1,14 @@ + + + Exe + net8.0 + enable + enable + 10.0 + + + + + + + \ No newline at end of file diff --git a/Source/Modbus/KellerXLine/Program.cs b/Source/Modbus/KellerXLine/Program.cs new file mode 100644 index 00000000..c151c76e --- /dev/null +++ b/Source/Modbus/KellerXLine/Program.cs @@ -0,0 +1,36 @@ +using Meadow.Foundation.Sensors.Environmental; +using Meadow.Hardware; +using Meadow.Modbus; + +namespace KellerXLine_Sample; + +internal class Program +{ + private static async Task Main(string[] _) + { + await Test(); + } + + private static async Task Test() + { + + var serialPort = "COM12"; + + using (var port = new SerialPortShim(serialPort, KellerTransducer.DefaultBaudRate, Parity.None, 8, StopBits.One)) + { + port.ReadTimeout = TimeSpan.FromSeconds(15); + port.Open(); + + var client = new ModbusRtuClient(port); + var sensor = new KellerTransducer(client, KellerTransducer.DefaultModbusAddress); + + while (true) + { + var pressure = await sensor.ReadPressure(PressureChannel.P1); + Console.WriteLine($"Pressure: {pressure.Millibar} mbar"); + + await Task.Delay(1000); + } + } + } +} \ No newline at end of file diff --git a/Source/Modbus/StepperMotor/Program.cs b/Source/Modbus/StepperMotor/Program.cs new file mode 100644 index 00000000..4f0a0f29 --- /dev/null +++ b/Source/Modbus/StepperMotor/Program.cs @@ -0,0 +1,36 @@ +using Meadow.Foundation.MotorControllers.StepperOnline; +using Meadow.Foundation.Motors.StepperOnline; +using Meadow.Hardware; +using Meadow.Modbus; + +namespace StepperMotor_Sample; + +internal class Program +{ + private static async Task Main(string[] _) + { + var serialPort = "COM12"; + byte controllerAddress = BLD510B.DefaultModbusAddress; + + using (var port = new SerialPortShim(serialPort, BLD510B.DefaultBaudRate, Parity.None, 8, StopBits.One)) + { + port.ReadTimeout = TimeSpan.FromSeconds(15); + port.Open(); + + var client = new ModbusRtuClient(port); + var controller = new BLD510B(client); + var motor = new F55B150_24GL_30S(controller); + motor.SetSpeed(new Meadow.Units.AngularVelocity(500, Meadow.Units.AngularVelocity.UnitType.RevolutionsPerMinute)); + + while (true) + { + await motor.RunFor(TimeSpan.FromSeconds(5), Meadow.Peripherals.RotationDirection.Clockwise); + await motor.Stop(); + await Task.Delay(1000); + await motor.RunFor(TimeSpan.FromSeconds(5), Meadow.Peripherals.RotationDirection.CounterClockwise); + await motor.Stop(); + await Task.Delay(1000); + } + } + } +} \ No newline at end of file diff --git a/Source/Modbus/StepperMotor/StepperMotor_Sample.csproj b/Source/Modbus/StepperMotor/StepperMotor_Sample.csproj new file mode 100644 index 00000000..0da1f1a8 --- /dev/null +++ b/Source/Modbus/StepperMotor/StepperMotor_Sample.csproj @@ -0,0 +1,14 @@ + + + Exe + net8.0 + enable + enable + 10.0 + + + + + + + \ No newline at end of file diff --git a/Source/ProjectLab/KellerXLine/KellerXLineSample.csproj b/Source/ProjectLab/KellerXLine/KellerXLineSample.csproj new file mode 100644 index 00000000..a4f13fa4 --- /dev/null +++ b/Source/ProjectLab/KellerXLine/KellerXLineSample.csproj @@ -0,0 +1,24 @@ + + + netstandard2.1 + true + Library + App + 10.0 + + + + + + + + + + + Always + + + PreserveNewest + + + \ No newline at end of file diff --git a/Source/ProjectLab/KellerXLine/MeadowApp.cs b/Source/ProjectLab/KellerXLine/MeadowApp.cs new file mode 100644 index 00000000..94c6255a --- /dev/null +++ b/Source/ProjectLab/KellerXLine/MeadowApp.cs @@ -0,0 +1,43 @@ +using Meadow; +using Meadow.Devices; +using Meadow.Foundation.Sensors.Environmental; +using System; +using System.Threading.Tasks; + +namespace KellerXLine_Sample; + +// Change ProjectLabCoreComputeApp to ProjectLabFeatherApp for ProjectLab v2 +public class MeadowApp : ProjectLabCoreComputeApp +{ + private IKellerTransducer sensor; + + public override Task Initialize() + { + Resolver.Log.Info("Initialize..."); + + Resolver.Log.Info($"Running on ProjectLab Hardware {Hardware.RevisionString}"); + + var client = Hardware.GetModbusRtuClient(KellerTransducer.DefaultBaudRate); + sensor = new KellerTransducer(client, KellerTransducer.DefaultModbusAddress); + + return base.Initialize(); + } + + public override async Task Run() + { + while (true) + { + try + { + var pressure = await sensor.ReadPressure(PressureChannel.P1); + Resolver.Log.Info($"Pressure: {pressure.Millibar} mbar"); + } + catch (Exception ex) + { + Resolver.Log.Info($"Error: {ex.Message}"); + } + + await Task.Delay(1000); + } + } +} \ No newline at end of file diff --git a/Source/ProjectLab/KellerXLine/app.build.yaml b/Source/ProjectLab/KellerXLine/app.build.yaml new file mode 100644 index 00000000..c74fbb0a --- /dev/null +++ b/Source/ProjectLab/KellerXLine/app.build.yaml @@ -0,0 +1,2 @@ +Deploy: + NoLink: [ ProjectLab ] \ No newline at end of file diff --git a/Source/ProjectLab/KellerXLine/app.config.yaml b/Source/ProjectLab/KellerXLine/app.config.yaml new file mode 100644 index 00000000..a4e0ac89 --- /dev/null +++ b/Source/ProjectLab/KellerXLine/app.config.yaml @@ -0,0 +1,21 @@ +# Uncomment additional options as needed. +# To learn more about these config options, including custom application configuration settings, check out the Application Settings Configuration documentation. +# http://developer.wildernesslabs.co/Meadow/Meadow.OS/Configuration/Application_Settings_Configuration/ + +# App lifecycle configuration. +Lifecycle: + + # Control whether Meadow will restart when an unhandled app exception occurs. Combine with Lifecycle > AppFailureRestartDelaySeconds to control restart timing. + RestartOnAppFailure: false + + # When app set to restart automatically on app failure, + AppFailureRestartDelaySeconds: 15 + +# Logging configuration. +Logging: + + # Adjust the level of logging detail. + LogLevel: + + # Trace, Debug, Information, Warning, or Error + Default: Trace \ No newline at end of file diff --git a/Source/ProjectLab/ModbusStepperMotor/MeadowApp.cs b/Source/ProjectLab/ModbusStepperMotor/MeadowApp.cs new file mode 100644 index 00000000..fe42b30b --- /dev/null +++ b/Source/ProjectLab/ModbusStepperMotor/MeadowApp.cs @@ -0,0 +1,41 @@ +using Meadow; +using Meadow.Devices; +using Meadow.Foundation.MotorControllers.StepperOnline; +using Meadow.Foundation.Motors.StepperOnline; +using System; +using System.Threading.Tasks; + +namespace StepperMotor_Sample; + +// Change ProjectLabCoreComputeApp to ProjectLabFeatherApp for ProjectLab v2 +public class MeadowApp : ProjectLabCoreComputeApp +{ + private F55B150_24GL_30S motor; + + public override Task Initialize() + { + Resolver.Log.Info("Initialize..."); + + Resolver.Log.Info($"Running on ProjectLab Hardware {Hardware.RevisionString}"); + + var client = Hardware.GetModbusRtuClient(BLD510B.DefaultBaudRate); + var controller = new BLD510B(client, BLD510B.DefaultModbusAddress); + motor = new F55B150_24GL_30S(controller); + motor.SetSpeed(new Meadow.Units.AngularVelocity(500, Meadow.Units.AngularVelocity.UnitType.RevolutionsPerMinute)); + + return base.Initialize(); + } + + public override async Task Run() + { + while (true) + { + await motor.RunFor(TimeSpan.FromSeconds(5), Meadow.Peripherals.RotationDirection.Clockwise); + await motor.Stop(); + await Task.Delay(1000); + await motor.RunFor(TimeSpan.FromSeconds(5), Meadow.Peripherals.RotationDirection.CounterClockwise); + await motor.Stop(); + await Task.Delay(1000); + } + } +} \ No newline at end of file diff --git a/Source/ProjectLab/ModbusStepperMotor/ModbusStepperMotor_Sample.csproj b/Source/ProjectLab/ModbusStepperMotor/ModbusStepperMotor_Sample.csproj new file mode 100644 index 00000000..43a14fe5 --- /dev/null +++ b/Source/ProjectLab/ModbusStepperMotor/ModbusStepperMotor_Sample.csproj @@ -0,0 +1,24 @@ + + + netstandard2.1 + true + Library + App + 10.0 + + + + + + + + + + + Always + + + PreserveNewest + + + \ No newline at end of file diff --git a/Source/ProjectLab/ModbusStepperMotor/app.build.yaml b/Source/ProjectLab/ModbusStepperMotor/app.build.yaml new file mode 100644 index 00000000..72e7ec6f --- /dev/null +++ b/Source/ProjectLab/ModbusStepperMotor/app.build.yaml @@ -0,0 +1,2 @@ +Deploy: + NoLink: [ ProjectLab, Motors.StepperOnline ] \ No newline at end of file diff --git a/Source/ProjectLab/ModbusStepperMotor/app.config.yaml b/Source/ProjectLab/ModbusStepperMotor/app.config.yaml new file mode 100644 index 00000000..a4e0ac89 --- /dev/null +++ b/Source/ProjectLab/ModbusStepperMotor/app.config.yaml @@ -0,0 +1,21 @@ +# Uncomment additional options as needed. +# To learn more about these config options, including custom application configuration settings, check out the Application Settings Configuration documentation. +# http://developer.wildernesslabs.co/Meadow/Meadow.OS/Configuration/Application_Settings_Configuration/ + +# App lifecycle configuration. +Lifecycle: + + # Control whether Meadow will restart when an unhandled app exception occurs. Combine with Lifecycle > AppFailureRestartDelaySeconds to control restart timing. + RestartOnAppFailure: false + + # When app set to restart automatically on app failure, + AppFailureRestartDelaySeconds: 15 + +# Logging configuration. +Logging: + + # Adjust the level of logging detail. + LogLevel: + + # Trace, Debug, Information, Warning, or Error + Default: Trace \ No newline at end of file