I have application to communication with device. Device is connected through serial COM port. My app can comunicate with device.
I need some method / event, that can scan COM ports through running app. When I'll connect device to PC - method / event will print MessageBox with message "connected", or something like that.
I found something like this:
comPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
But it doesnt work.
I'm not sure if you are trying to auto-detect which port a device is connected to, or auto-detect whether a device is connected to a specific port.
In both cases though, the principle is the same:
you enumerate the the serial ports using: SerialPort.GetPortNames if you need to determine the port, or skip to the Step 2 if you already know the port
for each port in the collection you open a connection by creating a new SerialPort object for it, and by calling Open
for each open connection you attempt to write/read from the port the sequence of data that determines whether your device is attached
for each open connection if the data read times out then there is no device attached to the port; otherwise, if you get back what you expect you know your device is attached
for each port, close the connection using Close on the SerialPort object.
Performing the above at any given point will tell you whether your device is attached at that point, and which port it is attached to.
If you need to do presence detection continuously, then you will probably want to create a timer and perform this test periodically (every 30 seconds, or every 2 minutes - depending on the latency you are willing to accept).
NOTE
As others have indicated in the answers, you will want to run the serial port detection code asynchronously so as not to block your main application while scanning the ports. The scanning is guaranteed to take a while because of the time-outs of the ports that have no device attached.
Related
I am trying to read data from a serial device. It has an LED that turns on when I connect it to an actual serial port (on an old XP machine).
On my target machine however, I need to use a USB-serial adapter, and the LED does not turn on when connecting it.
It does turn on when I open the serial port with Putty or Hyperterminal though.
Now I have been trying to read it in my .Net project, and in order to do that, i run the example code from the MSDN documentation here :
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline%28v=vs.110%29.aspx
when I do, nothing gets read from the port, and the LED does not even turn on. I would expect it to turn on whenever I hit the
_serialPort.Open();
but it does not.
Is there a specific step, or configuration, or workaound, to ensure the serial port receives power when using USB adapters?
My serial device is a GlobalCaché GC-IRE.
The USB adapters I have been testing: 3 different ones, two no-names and one TrendNet TU-S9.
I have a program then when started looks for the serial ports available on the computer and if found would try to open those and listen to those for data.
As of now, I have a configuration file that has got all port settings along with the Port Number to listen to.
At first run I get the user to first enter the POrt No. on which the device is connected. That is recorded into the Config and is used from there one.
I would now like to change that so that when started, it scans the ports and gets the list of Serial Ports available. That part I can do with SerialPort.GetPortNames() method.
However, now I would like to ping each port and check which one has the device connected, open that port and keep listening to that port.
I can check with my device manufacturer to get the commands to ping and ACK back from device.
My Question to the experts is
Is it correct (archietecturally/technically) to keep the ports open all the time
Should that be done in a seperate thread and probably keep the thread for just serial port communications while main thread does all other jobs.
What if there are multiple devices connected to same PC. Should we keep seperate threads for each device ?
Any help or ideas would be greatly appreciated.
Thanks
Ashutosh
Is it correct (archietecturally/technically) to keep the ports open
all the time
It's normal to do that. Continually opening and closing ports is an expensive and wasteful operation. Sometimes is is used as a message framing protocol, ie. open/send/close, but a protocol that keeps the port open is surely going to perform better.
Should that be done in a seperate thread and probably keep the thread
for just serial port communications while main thread does all other
jobs.
It can be, yes. I have used such a design - create threads that attempt to open COM1-COM9 and, if the open succeeds, try to communicate with a peripheral. If the comms is successful, the handler thread messages the main thread with an object reference/pointer that can be used to communicate with the port, (and usually give user feedback, eg. check a checkbox and change its background colour from red to green).
What if there are multiple devices connected to same PC. Should we
keep seperate threads for each device ?
That's how I usually handle it, yes. I define a class that represents the port, it's protocol and state. Each port-handler thread then creates its own instance and so each port gets handled independently.
I have a device which sends data by com port on my computer.
I know how to simulate it, but the controller must be plugged in to simulate sending data (using Proteus)
Is it possible to simulate the com port sending data without having any external device plugged in?
For example: I want to write a C# program which opens the com port and waits for data, and another c# program which writes data on the same port.
The best way to do this is to use a software COM port emulator. I use com0com :
Null-modem emulator
This provides virtual NM COM port pairs on the system (ie: what is output to one is input to the other and vice-versa). The devices show up in Device Manager just like a real COM port so you interact with them in C# as though they were real hardware devices.
For simplicity's sake, get yourself a com port or null modem emulator. You'll get very far off track, and maybe waste a lot of time, trying to do this yourself.
See this post, too:
Faking an RS232 Serial Port
I have a micro-controller (Arduino) and wish to determine automatically to which port the micro-controller locks to. The idea is not to have to correct the COM port manually for every app on every computer. Can this be done ? Thanks !
Do you mean:
Micro-controller is an external hardware device
User plugs the device into a random serial port (COM port)
You want to know which COM port it's connected to
?
Also, you are talking about an old-style COM port, are you, not a USB?
Anyway if that is your question, the only way would be to:
Open each COM port on the machine and send data through it, to see (if the micro-controller sends back the expected resonse) whether it's talking to the expected device (e.g. if the device were a modem you might try sending "AT\r" and expect a modem to respond "OK", if there is a modem and if the modem is connected to that serial port).
And/or write a device driver or a service to do just this, instead of an application.
I am using a serial port communication C# windows application. I wrote a program to get the data from the port, to manipulate it and write through the serial port.
My question is in case the unit is not powered on, the power supply is disconnected, or the unit is not responding to commands for a long time, how will I know? In my program, I have written for normal flow of execution. But in case there is no response for long time, the program keeps on waiting for a long time. I used a timer but it doesn't work properly.
Can anyone help me please?
If you are using the "SerialPort" object in C#, then you can use the ReadTimeOut and WriteTimeOut Properties.
You've got two options:
If you have a device which should be generating data you can listen on the serial port for data from and if nothing is received for a set period you can assume it is offline,
Periodically send data to your device over the serial port and if you get an exception then you will know your device is offline.
One thing that might help is if the cable you are using is wired with DTR / DSR crossed, meaning that when your program starts you raise DTR and the other device see's DSR go high, and in turn raises it's DTR, which raises your DSR.
If you are using unknown cables and communicating with devices you don't have control over, then you will have to use .Timeout.