I've two problem when working with serial port (COM) which I cannot find any reason for it:
1-I've wrote a program in c# (wpf) to receive data from a micro-controller(micro sends some data continuously with an interval ,e.g every 100 ms) , when I run my program in visual studio it receives data but there are some delays in receiving the data; it receives the first three data then a delay and then some other data and continues this scenario without any discipline. I've tried both release mode and debug mode but no change. but when I go to Debug folder or Release folder and run the .exe from there every thing is ok and receives data continuously), I'm really confused!!
2-for connecting to micro I used usb-to-COM converter(cable with one side usb and other side COM port) . sometimes between the running of the program I disconnect the connection by pulling out the usb from my laptop(yet micro is sending) and when I connect it again to my laptop, mouse , app windows,... all move and I cannot control anything!
A USB serial port cable is actually a device (usually classified as a USB to UART bridge device, there's actually a UART in one of the cable connectors). By pulling the USB cable from your laptop, you're effectively removing the serial port device from your system, meaning your COM port no longer exists. If you do this while your program is running, I'd expect the program to crash. A connection over a serial port is a file stream connected to \\.\COM#
'#' is just a placeholder, it could be COM4, COM5, etc. depending on how many other serial port devices are active on the system. What you're effectively doing is similar to disconnecting a harddrive while your program has a file open on that drive. I would not expect it to end gracefully.
As for the data reception, serial ports are very slow by today's standards while computer programs are very fast by serial port standards. Also, while serial port input can be implemented as a blocking call, it will eventually time out. When it times out you're probably reading a 0-byte if you don't check for a time-out condition.
All I can provide you with without knowing more detail of what it is exactly you're attempting to accomplish and how you're attempting to accomplish it.
Related
Does anyone know how do I check if a device is connected to a serial port? I need a sort of ping functionality so that I can ping the device and attempt to connect to it only if the ping succeeds. For instance if I don't have the COM cable plugged in, I can display the connection failed message much faster.
It just isn't quite the same problem as having to use Ping to find if a server that's located a thousand miles away is online. With serial ports you just look at the other end of the cable, you rarely have to walk more than a few feet.
There's also nothing similar to having a stateful connection like TCP. All you can really do is look at the hardware handshake signals. The SerialPort.DsrHolding gives you the state of the Data Set Ready signal. A properly implemented serial device uses that signal to say that it is powered-on. CtsHolding is an additional signal, it says that it is ready for you to send data. They normally have to be both turned on before you consider sending anything.
These handshake signals are not always properly implemented. You may well have a problem if the serial port is actually emulated by a BlueTooth or USB device driver for example. Pretty common these days with very little consistency in how closely these drivers emulate a real serial port. You'll have to try.
I am using visual studio 2008 and developing a windows forms application in C# using .net 3.5
I am doing serial communication and on a button click the application getting a list of all the comm ports available. I then scan for the device i am looking for by opening each port in turn, sending my custom handshake request command and waiting the reply for some number of seconds.
It all works fine on my PC (and on other 8 PCs I've tested it on) but when i run the program on a laptop my program seems to see all the comm ports and sends a requires but my device never receives it. I've tried using the hardware comm port and the usb-to-serial converters. I've also tested it on several laptops form different manufacturers. All of the them behave exactly the same.
Has anyone ran into similar problems?
UPDATE 1
The laptops seem to be able to receive data from the device, but still can not transmit. And of course no problems whatsoever with desktops.
UPDATE 2
A laptop can receive and transmit data.
Therefore that narrows the problem down to my C# software that can do serial communication on a desktop but can not do same thing on a laptop.
UPDATE 3
Solved it. The problem was caused by built in modems of my test laptops. The com port reserved by the modem (typically COM3) does not appear in the device manager, but can be seen in the windows regestry. So whenever my serial port object would ask for a list of com ports, it would get the modem port, and these modems choked on my handshake requests during the port scan.
As usual everything is stupid and simple.
Sounds like your device is picky about the voltage levels. RS-232 doesn't nail that down very well, allowing anywhere between +/- 5V to +/- 28V. Most devices are happiest with +/- 12V, a standard voltage available in desktop machines. Laptops however are always on the low end, +/- 5V typ. Not much you can do about it.
To test this theory, eliminate the possibility that this is induced by your code and make sure it works with a null modem. Connect TxD to RxD so that you receive what you send. Then use Hyperterminal or Putty. If the latter fails to see the device as well then it's a hardware problem.
I am looking for something to test my app. The app is a weight logger, built in .net 3.5 - it is connected by rs232 to an electronic weight, but the problem is that I don't have it at home.
I wanted to emulate the traffic and data with my app, but I have a problem: I can't get 2 apps on 1 port.
Please help.
These are the tools you need:
com0com: Creates two virtual COM Ports which are connected to each other
Just connect your receiver application with the first one and your mock weight application with the second one.
Serial Port Monitor: If you need to sniff into some serial port connection to find out how another application communicates with some serial device.
Update
The storing and filtering is just to specific to your concrete project, so that you wouldn't find anything "out of the box". But you have two possibilities to store the data.
Within your application implement some kind of wrapper class for your serial port. Every read and write access to the serial connection goes through this class. Then this class sees everything and can dump the whole traffic into a file or somewhere else. Additionally you could also implement some kind of filter mechanism.
Instead of putting the dump and filter logic into your program, write it as an application. This application connects to two COM ports. The first is the real serial port. The other is a virtual from the com0com driver. Your application that normally communicates with your serial device will now be connected to the second com0com driver. So you're able to dump and filter anything that flows through your serial port.
I need to Determine the serial port name connected to other machine using c#.
This is just not the way serial ports work. It is not a bus, like USB or PCI, where you can plug something in and the operating system will do the ah-ha, new hardware! discovery automatically. Serial ports are very primitive, dating from the stone age of computer hardware.
It takes a human to plug a serial port device connector. With some luck, the connector will have a label which says what COM port number is assigned to the connector. Although that luck is hard to come by these days. She'll then tell a program to establish a connection on that particular COM port. Hyperterminal is the canonical implementation of such a program on Windows.
You cannot realistically open every COM port that might be available. That prevents another program from using another COM port. You'll prevent a modem from getting used for example. Part of the stone age legacy is that only one program can open a COM port, every other program will be locked out.
So, provide your program with a UI that lets the user select the COM port(s). Save the selection in your config data, it is very likely that the device is still connected to the same port when it starts back up. You can use WMI and the Win32_SerialPort class to provide a better description for the COM port (more than just the number). Some USB serial port emulators may set the Description property to something recognizable.
SerialPort.GetPortNames() enumerates the available COM port numbers. A basic sanity test is to check the SerialPort.DsrHolding property, it should be true when the serial port device is plugged in and powered-up.
A serial port doesn't report any connection state. You can open all available serial ports on your computer (if no other application already opened it) regardless if it is connected to something or not.
To find out if a serial port is connected to another machine, you have to open up all the available port, send your initialization data and listen if something correct comes back.
Imagine you have a good old serial modem connected to your pc. To find this out you have to open up all the available ports and send a 'AT' over the wire. If a 'OK' comes back you found a modem (maybe additional tasks are necessary to check if you found the right one [maybe there is more than one device connected to your pc]).
What i just missed out: Don't forget to configure the serial port! Don't set only baudrate and stop bits. Set all settings to the values you need (even if you use default settings). Cause these settings will be saved also if you close and reopen again. All settings are still valid unless you change them. Now imagine you have some other application on your pc that also opens up a serial port and changes the settings for some uncommon feature (e.g. XOnOff). If you don't set it back on your initialization phase
you'll never be able to get a working connection!
Update
Listening to all the available ports is quite easy:
First you need a list of all com ports.
Then you create for each one a own thread (or backgroundworker)
And each thread handles its given SerialPort
That's it.
Serial communication doesn't have anything compared to that of IP which has an address and port sent with every packet. The only data that is sent over a serial cable is the bytes you send yourself.
If you control both ends you can send the port number as a part of your own protocol.
I have an application that listens to a piece of hardware on a USB to Serial converter. My application should monitor more than one serial port at the same time.
I loop the serial ports I need to listen to, and create a thread for each port. In the thread I have my data handing routine.
When I assign one port, it runs flawlessly. When I listen to the other one, it also works. When I open both ports however, the second port always throws an UnauthorizedAccessException when calling serialPort.Open(). It does not matter in what order I open the ports, the second one always fails.
I listen to the ports using serialPort.ReadLine() in a while loop.
Can .NET open more than one port at the same time? Can I listen to both? Or should I use another (thread safe?) way to access my serial port events?
The exception has a very specific meaning, it tells you that somebody already has the port opened. Who could that be? Triple-check four times that you are really using a different port name when you open the 2nd one.
Next step is to take the USB emulator to the parking lot and run over it with your car several times so it can no longer drive a programmer nuts. Get one from another manufacturer that uses a different device driver supplier.