As per the title, I want to control a parallel (LPT) port using C# in Ubuntu.
Are there any inbuilt libraries in Mono that will allow me to do this?
Can anyone give any code examples of making this work?
Guide with basics, and c# code for use of inpout driver and api: http://www.codeproject.com/KB/cs/csppleds.aspx
More general collection of links, both technical as well as software related to the parallel port: http://www.lvr.com/parport.htm
I spend quite some time researching this, and never found a native mono library. It's easier for Serial Ports btw.
While I acknowledge Jesper's contribution, I felt his answer was incomplete and the initial answer gave only references to Windows code as pointed out by Dai. Jesper's follow up comment regarding P/Invoke did lead me down a path to finding the answer.
I have documented my complete solution, including code samples here: http://www.iaincarlin.com/ylsned/controlling-the-parallel-port-using-ubuntu-mono-and-c/ however, in a nutshell:
There appears to be no native library in C# Mono for accessing the parallel port
P/invoke is required and I had to create a native C++ application that I could invoke with DLLImport in order to access the LPT1 port.
The C++ application essentially provides a wrapper round the io library outb function that I can call from C#
My blog post above contains more details regarding the background to what I was doing. I could have used native C++ to do the same thing (in fact it would have been easier to port my existing Dos code over to Linux), but I wanted to experiment with Mono and this was a practical way to do so.
I need to acknowledge the post here: http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/ that provided the most helpful in getting this to work.
Related
Does anyone have experience using the Nordic NRF52840 with a C# app to act as a receiver?
I'm trying to communicate with an Onset InTemp thermometer and although I've had quite a bit of success with the native Windows 10 BLE, I'm having some problems. See: C#, BLE. Why does GetGattServicesAsync hang forever? Is there a work around such as turning off/on BLE? for details.
Nordic advertises that they only have C++, Python and Node.js libraries. See https://devzone.nordicsemi.com/f/nordic-q-a/65516/using-nrf52840-dongle-as-receiver-client-for-onset-thermometer for example. I need either a C# library OR perhaps some hints on how to build the C++ library with Visual Studio (there are no instructions that I can see) and setup the interop layer (method signature etc.). I think this would go much faster if I saw an example :). And I'm guessing this type of work has been done before.
Another possibility is to call the Node.js libraries from C# which I understand is possible but again, I'd need some initial examples. Probably more so as I've never used Node.js either on its own or from C#.
Thanks, Dave
I want to be able to change the default audio output device on Windows 7 programatically using C# (and probably some underlying Win32 API calls, as well). I've already done my homework, and I've heard a lot of mixed comments from different sources, so I wanted to ask this question again to get a straight answer. Is this actually possible (through any means)? If so, how would I go about doing this?
And please do not suggest a solution with "AutoIt" or some other similar program... this is a C#/.NET-specific question.
A little up-to-date answer, which is compatible with Windows 10.
This program is able to do it in a beautiful manner (hotkey to switch between pre-selected devices) and is written on C# :
SoundSwitch on GitHub
Some reverse engineering should get you there (for my use this program was just perfect as it is).
It makes use of that library (same author) which is in C++ :
AudioEndpointLibrary on GitHub
So if you know C++ (which I don't enough), you might go further in the analysis of how it works. Or just use the library like SoundSwitch does.
Just in case anyone stumbles across this thread in the future... here's some C++ code that'll do it by calling some undocumented Win32 APIs. This can be compiled into an EXE and then called silently from a .NET application, so you could build a .NET program around this code.
http://web.archive.org/web/20190317012739/http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
I am looking for a way to have as much control as it is possible over serial port in my c# application. The problem is that I need to communicate with a device that has no documentation except for an old c++ program written to control it. I've tried to use SerialPort class to communicate with the device but its behaviour is quite odd (I have to repeat some of the commands, some other commands dont work at all).
I would like to copy that unmanaged program's behaviour, however it seems to be impossible with serialport class, as it does not provide access to low-level functions and structures like DCB for example.
Are there any low-level wrappers for serial communication available for .net? Maybe i could use reflection to manipulate serialport innacessible members at runtime?
For those suggesting to look at the .NET SerialPort class; frequent Stack Overflow answer provider on Serial related issues, Ben Voigt, provides some excellent insights on why a wrapper around WinAPI would eventually turn out to be a much better idea than using the framework provided SerialPort:
Ben Voigt on .NET SerialPort
A must read.
He also refers to a WinAPI wrapper that he might reveal in future blog posts. If that would happen, it would answer the original question.
Also, there seems to be at least one commercial solution providing the requested functionality here.
Another edit:
Some more searching online has yielded this blog post apparently from the time when there was no SerialPort class in .NET... Source code is provided showing how to wrap Win32 API for the purpose.
EDIT
Some users have pointed out that the above mentioned MSDN blog post link is dead.
The title of the linked article was:
"Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications", written by John Hind andpublished October 2002 as I can tell from another MSDN article referring to it:
Unfortunately, Microsoft seems to only serve editions of their magazine down to 2003:
FWIW, I found a half working online version elsewhere...
Unfortunetely, the SerialPort class is an incomplete wrapper. I have found the only way to gain access to the underlying DCB is through reflection.
The only other option would be to re-write SerialPort and make it complete. I have not seen any such implementation freely available (yet).
Here is an example where I used reflection to gain access to RTS_CONTROL_TOGGLE:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/45a89532-b01c-4ef8-aa46-532882cec004
The SerialPort class is a very thin wrapper around the Win32 serial port API, hard to see how another wrapper could improve your life. There are wrappers available from the .NET 1.x days, it didn't support serial ports. Here is one from MSDN magazine.
But you're just as likely to have the same problems. One way that written commands could get lost is by the device throwing away received bytes (or losing them) when it has turned off the RTS signal off. You fix that by setting the Handshake property to RequestToSend.
One way that things can go wrong with reading commands is to get the Read() call wrong. It will return an arbitrary number of bytes, as many as are available in the receive buffer. Pay attention to the return value, it tells you how many bytes were actually read. The only guarantee is that it will at least be 1 and never more than count.
The SysInternals' PortMon utility can help you troubleshoot communications, it gives you a raw view of what the device driver sees. Compare with, say, Hyperterminal or another known-good program.
Working with a serial port through PInvoke works fine. We're doing this in a Silverlight application, but it should work the same in a normal .NET app. The answer to Serial Communication with Silverlight 5 (COM port) provides a basic wrapper for a which could easily be adapted to your needs. It uses the normal WinAPI method so you get full access to all serial port functionality, like you would in a C++ application.
P.S. If you don't already, use PortMon to monitor what's happening on the line.
You may be able to copy the old C++ code into a managed C++ class, which you could then use just like any normal .NET class from your C# code. This may be the fastest way to reach your goal.
We have several legacy components that interact with COM ports, USB etc.
I would like to create a .NET program that would emulate a COM port and log the traffic, relaying it to a WCF service endpoint somewhere or directly into a database. Maybe also wrapping a real COM port kind of like the decorator pattern.
I have looked around and I have found Sourceforge project Com0Com, but it's pretty old API and in c++.
I realize that I can solve this specific problem by creating a line printer driver and never really interacting with the COM ports registered in the system. Some links to that would also be highly appreciated.
Has anybody done this? How do you create system resources in .NET?
You would have to write a driver, that's how Com0Com works. If these components run in-process, you could hijack the Windows API functions, Microsoft's Detours for example.
Either solution requires C/C++, you can't write this code in a managed language. Although detouring could be technically possible, just very hard to get right. You can buy a solution though, your requirements are not uncommon, albeit it dated. Dated enough that finding one might be a bit tricky.
Has anybody done this?
To add to what nobugz said:
When I wrote a COM port emulator, I did it starting from the sample serial port driver in the DDK (serial.sys).
When I wrote a COM port wrapper/logger, I did it starting from the sample parallel port filter driver in the DDK (parport.sys).
Does anyone have a good solution for integrating some C# code into a java application?
The code is small, so I could re-write in java, but I would rather reuse the code if possible. Don't repeat yourself, etc.
Also, I know I can expose the C# as a web service or whatever, but it has some security/encryption stuff in there, so I would rather keep it tightly integrated if possible.
Edit: It's going to be on a server-based app, so "downloading" another runtime is irrelevant.
You would use the Java Native Interface to call your C# code compiled into a DLL.
If its a small amount of C#, it would be much easier to port it to Java. If its a lot, this might be a good way to do it.
Here is a highlevel overview of it:
http://en.wikipedia.org/wiki/Java_Native_Interface
Your other option would be to create a COM assembly from the C# code and use J-Interop to invoke it.
http://sourceforge.net/projects/j-interop/
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
If it's short, I think you're better off re-writing the code in java. Downloading one 50Mb runtime is bad enough.
There is an IL to Java Bytecode compiler GrassHopper which may be of use to you. I've never tried it though.
I'd look at rewriting your code in Java though
EDIT: Note that Grasshopper seems to be no longer available.
We used JNBridge for this, and it worked great. It handles Java->.NET and vice versa, all in-proc.
If you do not want to rewrite hadle it as an Inter-process communication and choose one of following:
Named pipes
Sockets
SOAP
I would rewrite it if it's not too much trouble.
The web service would work, but it seems like that would be a lot of overhead just to reuse a little code.
http://www.infoq.com/articles/in-process-java-net-integration suggests running CLR and JVM in the same process space and passing calls back and forth. It sounds very efficient. I'm going to give it a try and integrate it into Jace if it works well.
If it is a piece of code that is exposable as a command line utility, I just make the other host language use a system call to execute the utility.
If your C# app needs to call Java, compile a special Java main that takes appropriate command line args and returns text output.
It the oldest, simplest method.
You can call your c# classes (compiled in a dll) via a bridging library, various libraries are available, every one with his characteristics. JNBridge generate proxy classes that you can call to manage the code in java classes. JCOBridge let you load your c# classes and use it from java using the invoke mechanism, also javonet let you import java classes and call java code using the invoke mechanism. All the explored solutions are commercial solutions that let you call java code from .NET and vice-versa with graphical user interface integration and other amenities.
Links:
jnbridge java-.NET bridge Developer and Deployment license schema with 30 day free trial
jcobridge java-.NET bridge Developer and Deployment license schema with unlimited Trial
javonet java-.NET bridge Research and Professional license schema with 30-day unlimited Trial after sign-up