How do you simulate a serial device? - c#

I am working on driver that talks to a device via a serial port in C#.
I do not always have the device available to do physical testing with. Is there a way I can simulate a device on a serial port so that it responds in an ideal manner?

Get a second COM port and use a Null-modem cable to connect the COM ports to allow two C# programs to talk to each other.

I used Com0Com for a while and wrote some simulator/emulator code.

What kind of driver? If it is the serial interface driver, then that gets quite tricky.
However, if your driver is an application level above the Windows device driver, then it's fairly easy to replace the i/o behavior by altering the string passed to CreateFile, or whatever layer on top of that C# uses.
== More ==
Since you use the .net library tools, this technique may be too yucky to bother. However the idea is to replace where, at some point, your code says open COM1: or whatever. Change that to be a file which has the simulated data, say `c:/com1testdata.txt'. Additional emulation code which recognizes the contents of the file for pauses and/or responses might be useful for some protocols. Data which is written to the port can be logged or ignored, depending on your requirements.

When working in a team designing some custom hardware I used a great terminal emulator called ZTerm (only available for Mac, I believe). It allows you to script responsees which enabled me to complete the software to our pre-agreed specification while the custom controller chips were still in the design iteration stage.

If you're running in a VM, you may be able to attach things to the serial port. QEMU, for instance, allows you to attach the virtual serial port to a TTY, which you can then interact with either manually or with a program running on the host.

Related

Create virtual hardware that listens & returns commands back to C# Application

I'm looking into developing software that would be able to listen for commands sent across the COM1 serial port, interpret them and send back unique data based off of that command.
ex) if the listener reads a command sent as 0x05, it would send back 0x10.
We currently have software in C# that sends commands across COM1 to our hardware products, but during development we are not guaranteed to always have hardware available to test on and ensure the GUI controls are functioning properly. This listener would help alleviate that issue. My coworker has been refining a python script that utilizes the Pyserial package which I plan on using to handle command protocol.
So ultimately, I am expecting my C# application to have COM1 open and sending commands while the listener program reads any data sent, interprets it and sends back the correct response. Is this the correct way of going about this?
com0com creates virtual com ports on the computer so you can run both programs on the same computer without multiple com ports and loopback cables. This could be handy when doing this kind of things.
Is this the correct way? It sounds to me like it would be easier to add a emulate mode to the C# software when it returns it's own answers without making a complex thing with serial ports and another piece of software that needs maintaining.

Get two devices to communicate over WiFi without going through IP (p2p over the WiFi layers only)

I need to get two machines to communicate via WiFi without using IP (I do not want to use IP sockets). The solution preferably should work with both WiFi modes (infrastructure such as regular WiFi and ad-hoc such as WiFi Direct). A C# sample code would be great please.
I have searched a lot and could not find any code similar to a socket program that sends and receives data between two machines (p2p) but using only WiFi without any IP.
Apple's Multi-Peer Connectivity framework supports setting up a Peer to Peer connection without the developer having to manage the IP connectivity directly, but it uses IP to deliver data and is limited to Apple devices.
It's possible to do this if you are willing to write the low-level c code to do it, but any solution which avoids IP will have to recreate significant portions of the protocol to be useful and would almost certainly require much more work than just using the IP features of the OS.
For very simple forms of communication between Wi-Fi stations you can use custom Action Frames and Information Elements, but those require very low-level access to the driver.

Windows 8 Runtime (metro) Serial communication using Tera term?

For my senior design project, we have developed a Windows Store App to control a 2-player tug-of-war style game (all in software) which is controlled by the energy levels of each players EEG signals (specifically alpha/beta bands, relating to concentration levels) which we are transmitting over Bluetooth LE. We are also thinking about controlling a physical component to the game with an STM32F0 Microcontroller.
Basically, every so often (on some event trigger) we want to send a value between -100 and 100 in 2's complement to the uC to control the direction that the motor will move and it's speed. All the research I have done has suggested that Windows Store Apps do not support serial communication at all, but I was hoping there was a not-too-difficult workaround. We have a USB to serial adapter which will be able to communicate with the uC's UART. Is it possible to use something like Tera Term, where the Store app could communicate with the terminal upon some event and send the data over the serial port? Could the Windows App open tera term and write values to it? The communication need only be in one direction.
I'm open to suggestions on alternatives (aside from writing some custom driver, which would be too complicated for the amount of time that we have remaining to finish the project). Should we just completely abandon the prospect of serial communication and look into something like zigbee or bluetooth? I know it's possible to communicate over serial port using System.IO.Ports in a WPF/Windows Forms application but that is not an option at this point, since we have already developed the entirety of the host application (minus this serial comm). Thanks!!
EDIT 1: I'm considering using a UDP socket as a means of sending the necessary data to a background Windows Forms App which should be able to communicate over a serial port.
I don't think that network access to localhost is allowed with store deployed windows store apps. However Bluetooth serial port profile may be a possibility, judging by a quick google revealing this.
However, given that WPF has access to a rich library of IO communications support, and it is mostly a superset of WinRT, a port may be easier than you think.

Getting info on USB-Serial converter

Hope you can help me on this.
I have a serial device, this device will then be connected to a USB-Serial converter, then the converter will be connected to my PC's usb port. The system will add another COM port to the Device Manager.
My question is, how can I possibly map the COM port number to the converter in C#? I can successfully enumerate available COM port in the system by doing the SerialPort.GetPortNames(), but not sure what COM port number this converter have.
thanks
ar
You can usually get some decent info out of a WMI query, although it requires the device driver to cooperate. Most do afaik. Run a query on the Win32_SerialPort class. You can use the WMI Code Creator tool to experiment with the query and auto-generate the C# code you need.
Don't count on being able to auto-select the device. You'll need a config option to allow the user to select the port. You can display the info you got from the query to help her pick the right one. Or ask her to unplug the device and plug it back in, the added COM port should be the right one.
We solved this different ways for different applications. We used explicit configuration for an instance where we had exactly one device of a particular type, but it wasn't clear what COM port it was going to be assigned until the system was configured. In another case, we had one USB cable that broke out into a hub with a bunch of converters on it, so we probed all the COM ports we could open successfully to look for our devices of interest.
A couple caveats with USB/serial converters on Windows -- if your device is something like a GPS unit that sends out data whenever it's powered on, Windows might detect it as an old serial mouse if it's plugged in during boot. Also, plugging into a different USB port is likely to chew up an additional COM port number (as well as break any explicit configuration you've done).
When i had the same problem (RFID reader), I checked each port if there was any data.
Probably it would be something in COM4 : COM9.
I know that it's not the best solution, but i used it on mobile device where i am sure about my COM connections...
Maybe in your driver's *.ini file there is some information about COM number.
If you are using an FTDI based solution for the USB<-> serial conversion, you can use the FT_PROG utility which is available from the FTDI web site to assign custom VID:PID pairs to the converter, which you can then query to identify which adapter is assigned to which virtual port.
Alternatively, you can make use of the FTDIChip-ID which is unique for each chip, details on this including code samples are located here: http://ftdichip.com/Support/SoftwareExamples/FTDIChip-ID.htm
You would have to look this up somewhere in the registry.
I can only assume this is a FTDI chip. If so, you may be able to get info from using their public API, which is included with the drivers.
If no other answers, I will check later tonite at home, as my JTAG debugger has the same chip.
Update:
Here is the registry key for my device's assigned COM port (under PortName)
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_0483&PID_5740\498C54823932\Device Parameters
I guess you can just enumerate thru the USB devices with the usbser driver.

Blocking Connections By IP

I need to able to block any and all connections to my pc from a specific IP address , i know this is possible with a firewall but i need to do this in c#. Any idea how (need code).
Update :
Its a generic C# app not asp.net , target platform is WinXp till Win7
Need more information... if you're talking socket communication, you can simply close the connection to a client as soon as it connects if the IP address is blocked, or process the Connection Request and evaluate there.
Edit: Simplest way for you would probably just be to interact with Windows Firewall API... here's how:
http://www.shafqatahmed.com/2008/01/controlling-win.html
Your question is unclear but I'll try to answer the best I can, within my understanding.
Do you want to control machines from connecting to any port on your machine? if so, you need to control the built-in windows firewall or find yourself a filter driver you can control. In order to write your own filter driver, you must leave the land of managed code, so I am guessing that's not an option.
To learn how to control the firewall, here's a link:
http://www.shafqatahmed.com/2008/01/controlling-win.html
more on google.
Do you want to control remote machines from connection to a port on your machines that your application owns? You cannot do that either (see #1 above). However you can take action after the connection, and close the connection if you don't like the remote IP (check the remote endpoint's IP).
two caveats with this approach:
It doesn't save you from a DoS attack.
You will need to be careful if you need ipv6 support (you can't just check the IPV4 address in that case)
HTH
A "firewall" in c#?
First you would have to access the network interface on a low level, eg.: http://msdn.microsoft.com/en-us/library/ms817945.aspx
Then you have to parse all incoming packets and maybe discard them.
It's not an easy task and I don't recommend you to write a driver and a firewall in C#, because the .NET Framework will be loaded every time you start your machine.
Also traffic parsing can be tricky... I implemented a router/traffic analyzer in C# some time ago and it took me about one year to gain the experience with network programming to gain the knowledge to do this.

Categories

Resources