Determine COM port to which a device locks - c#

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.

Related

How data is sent to a serial port which is being connected by a program

I am wondering that since the serial port can't be used by two programs at the same time, how my barcode reader(hardware device) is able to send data to my computer via serial port, while the port is being opened by my program, and monitored for the newly coming data.
Sure, only one process can open the port. On the other end of it sits the device driver, it actually talks to the hardware.
That hardware is a chip called an UART, Universal Asynchronous Receiver Transmitter. It connects to the wire you use to hook up the device. Any data that comes in through the RxD pin on the connector gets converted to bytes by the UART. The device driver responds to the chip's interrupt and copies the byte(s) from the UART's receive buffer into the driver's receive buffer. Ready to be consumed by a program, it calls ReadFile() to empty the buffer. Today it is often a USB device driver that emulates a serial port.
Serial port communications are very primitive, there is no notion of a logical connection and no agreed-upon protocol to label received data to belong to a specific connection or consumer. Nothing similar to UDP or TCP, the protocols that allows a network connection to be shared. Serial ports site at the very bottom of the OSI model, the physical layer. The driver therefore does not allow more than one program to open the port. It is first-come, first serve.

Is it possible to simulate com port sending and receiving data using only C# programming?

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

How to autodetect connection of serial COM port c#

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.

Device information on com port c#

I had such a problem: in the three com ports have devices with which I work in software. Not installed drivers. The problem is: How can I find out which of the ports is connected with which of device? Thanks in advance
You may not be able to identify the devices in the COM ports. There is no industry standard nor requirement for device names. Some protocols such as SCSI, ATAPI and USB have commands that you can use to identify the device. For standard RS232, there is no such requirement.
What kind of devices do you have to identify? Plotters, thumbdrives, tape drives, etc?
There is no reliable way to do this. You can poll the COM ports, but many devices don't even respond to commands they receive. A COM port could only have Tx and Gnd wired up, so the connected device might not even be able to respond. You have very little to work with when dealing with RS232.

question about PORT

I have written a program in C# that sends text to COMx.
I wish to test it using HyperTerminal, meaning I want to read the text that I send with my program. When I set the WAIT FOR CALL in HyperTerminal, my program is refused access to the given port.
How can I test my program? Is it possible?
The best answer I can give is that the serial port is locked by your program and hence HyperTerminal cannot access the port as it is in use by your program. The best thing is to use a null modem cable loopback device in which you can send data and it will get looped back, like what EricLaw suggests.
Hope this helps,
Best regards,
Tom.
tommieb75 and EricLaw's answers are both right. When an application opens a serial port, the port is opened exclusively and no other app can work with it.
In order to test my serial applications I always use com0com. You can create a pair of virtual linked serial ports and setup your app to write to one of them and the HyperTerminal to listen to the other one.
This tool has helped me countless times. I strongly recommend it.
Take your serial cable and solder in on your RX or TX side of the COM port. Now, connect this to another COM port on your PC. Tada - now you can monitor what you are sending or receiving on this port by listening in on the other. Don't terminate TX on this other COM port so you don't disturb this connection. I have a couple of these cables in the office for this very purpose. I don't trust Serial Port monitoring software.
Your best bet would probably be to put a null-modem DB9 cable from COM1 to COM2 and use COM2 to talk to COM1.

Categories

Resources