Testing polling for the Watlow F4SD; register #100

Signed-off-by: mharb <mharb@noreply.localhost>
This commit is contained in:
mharb 2025-06-21 20:05:15 -04:00
parent f4c511f02d
commit f35e60d9b1
3 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NModbus4.NetCore" Version="3.0.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36121.58 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusWatlowRamp", "ModbusWatlowRamp.csproj", "{138F9A2D-5D63-4303-87AD-9C131B93BA62}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{138F9A2D-5D63-4303-87AD-9C131B93BA62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{138F9A2D-5D63-4303-87AD-9C131B93BA62}.Debug|Any CPU.Build.0 = Debug|Any CPU
{138F9A2D-5D63-4303-87AD-9C131B93BA62}.Release|Any CPU.ActiveCfg = Release|Any CPU
{138F9A2D-5D63-4303-87AD-9C131B93BA62}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {986BB29F-AC51-4EA5-9A56-1400A2778A90}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,88 @@
using Modbus.Device; // NModbus namespace
using System.Net.Sockets;
internal class Program
{
// Configuration
private const string hostname = "127.0.0.1"; // Replace with your Watlow F4 IP
private const int port = 502;
private const byte slaveId = 0;
// Register 100 should be the first read-only input on the F4
// Value 99 or 100 should work depending on zero indexing
private const ushort loadRegister = 99;
private static void Main(string[] args)
{
TcpClient tcpClient = null;
ModbusIpMaster master = null;
try
{
// Initialize connection
tcpClient = new TcpClient(hostname, port);
master = ModbusIpMaster.CreateIp(tcpClient);
// Monitor continuously
while (true)
{
// Check for Escape key press
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape)
{
Console.WriteLine("Escape pressed. Exiting...");
break;
}
}
try
{
// Read load value (holding register)
ushort[] registers = master.ReadHoldingRegisters(slaveId, loadRegister, 1);
int load = registers[0];
// Display load info
DisplayLoadInfo(load);
// Pause before next read
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Initialization error: {ex.Message}");
}
finally
{
// Clean up
master?.Dispose();
tcpClient?.Close();
}
}
private static void DisplayLoadInfo(int load)
{
Console.Clear(); // Clears the console
// Assume load is 0-65535 for 0.00-100.00% (16-bit range)
int percent = (int)load / 65535 * 100;
int barLength = (int)(percent / 2); // Scale to 50 chars for console
Console.WriteLine($"Load Value: {load}");
Console.WriteLine($"Load Percentage: {percent:F2}%");
Console.Write("Bar Graph: [");
Console.Write(new string('=', barLength));
Console.Write(new string(' ', 50 - barLength));
Console.WriteLine("]");
Console.WriteLine("\nHold ESC to exit.");
}
}