RS232 library for .net core - serial-port

We are developing applications in .Net Core and one of them require to access a serial port.
As I learned that System.IO.Ports won't be implemented in .Net Core, I was looking for a nuget library that supplies that functionality, but couldn't get one compatible with .net core (VS Code is showing an error message).
Is there any alternative out there?
UPDATE: I found that the official SerialPort API is being taken into consideration for porting to .Net Core (see https://github.com/dotnet/corefx/issues/984)

I managed to compile https://github.com/jcurl/SerialPortStream for netstandard1.5 with some minor modifications.
Have a look at the pull request: https://github.com/jcurl/SerialPortStream/pull/13
Experimental nuget package: https://www.nuget.org/packages/SerialPortStreamCore/2.1.0

This is now fully cross platform in 2021.
Simply NuGet install either using the command line or your IDE : "System.IO.Ports"
The following example is a .NET 5 console mode program that compiles and works on both Linux and Windows.
using System;
using System.IO.Ports;
namespace PipelinesTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Reading a GPS Device on COM3");
SerialPort _port = new SerialPort("COM3", 4800, Parity.None, 8, StopBits.One);
_port.DataReceived += PortOnDataReceived;
_port.Open();
Console.WriteLine("Press Return to Exit");
Console.ReadLine();
_port.Close();
_port.DataReceived -= PortOnDataReceived;
Console.WriteLine("Ended");
}
private static void PortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = sender as SerialPort;
var line = port.ReadLine();
Console.WriteLine(line);
}
}
}
NOTE: I should have mentioned when I first answered this, you'll need to change the "COM3" in the constructor above to something like "/dev/ttys0" or similar for Linux. You MIGHT also have to run your app as Root/SU on a Linux machine, this is just the nature of Linux's multi user security unfortunately.

Related

Issue with Windows based application automation using WinAppDriver

I am checking for an alternative of my automation framework that is built in UFT.
I came accross WinAppDriver tool. Looks promising.
But currently getting an error when it tries to launch the application under test-
{"Status":13, "Value":{"error":"unknown error","message":"failed to locate opened application with appid:"XXXXXX.jnlp" and processId:XXXX"}}
The application that i am trying to automate is a Java swing based application.
I tried running a simple java code in IntelliJ just to check whether i can have a quick working sample POC-
private static WindowsDriver appSession = null;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“app”, “XXXX.jnlp”);
capabilities.setCapability(“platformName”,”Windows”);
capabilities.setCapability(“deviceName”, “WindowsPC”);
capabilities.setCapability(“ms:waitForAppLaunch”, “20”);
appSession = new WindowsDriver(new URL(“http://127.0.0.1:4723”), capabilities);
Please help.
Thanks.

How to setup ElmahCore for a Console Application?

I want to log errors for a console application using Elmah.I've found ElmahCore and elmah.io.core but I don't know how to setup any of them on a console app.I'm using .net core.
ELMAH (the open source project) doesn't work with .NET Core. ElmahCore has a lot of dependencies to ASP.NET Core, but if you really wanted to, you could do something like this:
class Program
{
static void Main(string[] args)
{
var log = new MemoryErrorLog();
log.Log(new Error(new Exception()));
var errors = new List<ErrorLogEntry>();
var result = log.GetErrors(0, 10, errors);
Console.WriteLine(result);
Console.WriteLine(errors);
Console.ReadLine();
}
}
You can replace MemoryErrorLog with a target logger of your choice.
The package named elmah.io.core is a deprecated package from elmah.io. elmah.io is (among other things) a commercial cloud version of ELMAH, where you store all of your errors in the cloud (list of differences between ELMAH and elmah.io). elmah.io works with .NET core through either the Elmah.Io.Client NuGet package or using one of the integrations for popular logging frameworks like Serilog and NLog.
I wouldn't recommend you to use ElmahCore for logging in a console application. It is created for ASP.NET Core. There are much better options for logging from a console application, like the mentioned logging frameworks.

Is there a universal config file for .Net Standard 2.0 Class Library?

I have a Class Library that I'm converting to a .Net Standard 2 class library in order to also use in ASP.Net Core 2.0 projects.
The library has always read from a config file items such as SMTP settings, connection strings etc.
In Web Projects it finds these values in web.config.
In Console/WinForms it finds these values in app.config.
Is there an equivalent config file for .Net Core 2.0 projects that "just works" like the previous examples?
I assume the answer is no, but looking for best way to handle this given the library is used across the organization, so maintaining backwards compatibility is important.
Turns out System.Configuration.ConfigurationManager was added back in .NETStandard 2.0.
Just pull it in from nuget and compile the .NETStandard 2.0 class library project.
Then, the library will work across projects using standard config files:
Net Core 2.0 projects use app.config
Web projects work from web.config
Console and Windows apps work with app.config
.Net Core revised configuration approach greatly.
You don't call ConfigurationManager.AppSettings["someSetting"] anymore whenever you need value for some setting. Instead you load configuration on application startup with ConfigurationBuilder. There could be multiple configuration sources (json or/and xml configuration file, environment variables, command line, Azure Key Vault, ...).
Then you build your configuration and pass strongly typed setting objects wrapped into IOption<T> to consuming classes.
Here is a basic idea of how it works:
// Application boostrapping
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("AppSettings.json");
var configuration = configurationBuilder.Build();
// IServiceCollection services
services.AddOptions();
services.Configure<SomeSettings>(configuration.GetSection("SomeSection"));
// Strongly typed settings
public class SomeSettings
{
public string SomeHost { get; set; }
public int SomePort { get; set; }
}
// Settings consumer
public class SomeClient : ISomeClient
{
public SomeClient(IOptions<SomeSettings> someSettings)
{
var host = someSettings.Value.SomeHost;
var port = someSettings.Value.SomePort;
}
}
// AppSettings.json
{
"SomeSection": {
"SomeHost": "localhost",
"SomePort": 25
}
}
For more details check article Configure an ASP.NET Core App.
I'm afraid that it will be difficult (trying to avoid word 'impossible') to maintain backward compatibility.

Is it possible to execute java application (which is dependent on many 3rd party libs) from C# code using tools like IKVM/JNI4Net?

I want to call java API from c# code. My Java API is a jar file bundled with several 3rd party libraries. I am trying to use IKVM and JNI4Net. I am able to call few java functions but when code has dependency over 3rd party libs, it shows error: NoClassDefFoundError' occurred in dll. My question is it possible to execute java application (which is dependent on many 3rd party libs) from C# code using such JNI based tools?
Without being a jni4net expert, I have some experience doing just that.
With jni4net, you need to use a BridgeSetup and call one of its AddClassPath() methods to build a classpath with your 3rd-party libraries.
For instance, something like this:
namespace My.Lib
{
using System;
using net.sf.jni4net;
public static class MyClass
{
public static void Init()
{
// create the setup and register the Java classpath
BridgeSetup bridgeSetup = new BridgeSetup(true);
bridgeSetup.AddClassPath("lib/myLib1.jar");
bridgeSetup.AddClassPath("lib/myLib2.jar");
// add others ...
// check the actual classpath we got
Console.WriteLine("registered classpath : ");
foreach (String s in bridgeSetup.JVMCLassPath) Console.WriteLine(s);
// now create the JVM
Bridge.CreateJVM(bridgeSetup);
}
}
}

automating asp.net testing

I have to test almost 20 asp.net web application everyday morning to ensure there is no issue in the web sites. so is ther any option to automate it ? There is data entry involved like entering username,password etc.
Note: I will not have the access to the code(only to the applicaiton URL).
Please suggest some option for this, so that we can avoid the manual effort involved in this. Thanks.
Regards,
Jebli.
You should look into web automation tools like WatiN or Selenium.
From the WatiN documentation:
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
From the Selenium Documentation:
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
class GoogleSuggest
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
System.Console.WriteLine("Page title is: " + driver.Title);
driver.Quit();
}
}
WatiN and Selenium are Open Source
VSTT 2010 is a good bet.
Example demos
How To: Functional Testing Automation Using Visual Studio 2010 - http://blogs.msdn.com/b/syedab/archive/2010/01/13/how-to-functional-testing-automation-using-visual-studio-2010.aspx
Data Driving Coded UI Tests - http://blogs.msdn.com/b/mathew_aniyan/archive/2009/03/17/data-driving-coded-ui-tests.aspx

Resources