Serial port ping functionality - c#

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.

Related

Can we configure baud rate in socket class for ethernet communication

I need to configure the baud rate of the ethernet port of my pc as 100Mbps. But after searching the net, I couldn't find a way to change the baud rate in the socket in C# code. Is it possible to set the baud rate for the Ethernet port in C#.
I actually need to communicate between pc and a micro controller. So if, in the micro controller, the baud rate of the Ethernet port is set to 100Mbps, then will the baud rate of the Ethernet port in PC be also set to 100Mbps automatically?
Thank You
I think you've got terms and technologies mixed up a bit. It sounds like you're used to working with serial connections (like RS232 or RS422) where both end-points have to be configured in the same manner.
IP networks don't operate that way. On a fairly modern switch, each connected device can have it's own link speed. So the computer can be connected with 1GBps and another computer can have 100MBit, all dependent on what that computers interface can handle.
The network protocol you use should then handle throttling, resending of lost packets and such (like TCP does). If you use UDP you'll have to either not care about lost packets, or implement such mechanisms on your own.
If you connect your computer directly to the micro controller, then yes - the port speed of the computer is probably going to be 100MBit as well. But there is no guarantee. And even if there was, how would you know that the micro controller would actually be able to handle all data sent to it at that maximum theoretical speed?

Strange problems with serial port comunicaiton

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.

Send data from the server to a GPS device

this is my first question here, after searching and reading through many places I have not choice but to ask.
I have a C# application that receives data from a GPS device and stored in a database, I need now is to send a string that contains commands which the device should be interpreted.
This device connects to the Internet over GPRS, I haven't idea how to send this packet over TCP over GPRS because the IP is dynamic.
I hope any suggestions or ideas on how to solve this.
Best regards.
You should do it the other way around - the device should poll the server for instructions. Just make sure the server is accessible.
As you have specified that you have developed a C# application to read the GPS data through GPRS that means you are running C# application on a public/static IP.
Nwo as soon as you get a request from the GPRS client, you get the DHCP IP address of the remote endpoint too.
Once you have that endpoint and socket open, you can transmit any data to the GPS device back.
Assuming that your GPS device is having some microcontroller to drive the GSM/GPRS modem.
I'm going to make an assumption here that the 'Device' is some kind of mobile phone connected to a standard GSM network?
If it is, then the short version of the story is "Forget it" even if the IP wasn't dynamic, you simply wouldn't be talking to the IP of your device, you'd actually be talking to the IP address of your providers GGSN, and for a standard consumer connection this is going to be where the buck stops.
Now that said, IF you have the budget, and all your devices (Assuming multiple ones) are with the same carrier, then you can approach the carrier and request a dedicated APN (Access point name) essentially what this is , is the mobile network equivalent of a DNS record, or at least similar enough to use that analogy anyway.
When you set up your data connection on your device you may recall having to enter something like 'pp.vodafone.com' or 'INGhub411.o2-uk.inbound' we'll this is your actual APN, and if you have a custom one they your devices can be set up so that the IP the presents itself at the GGSN actually has a static route back to the individual device in the suppliers GSM network.
As a general rule of thumb however on consumer grade connections this is not enabled and hence there is no ingress available to the individual device.
As zmbq says, the ONLY option you have is for the device to keep polling the server on a regular basis, and yes unfortunately that is going to be very unforgiving on the battery.
of course there is one other way of approaching this, and that's to have the device open a socket directly to your server then keep that socket open. Once the socket is open, 2 way communication can be performed across the link, unfortunately your going to also have to write all the code to manage this connection including, but not limited to monitoring the connection to make sure it's still open and re-establishing it if it's not, something which is incredibly important on a mobile device.

Access Serial Port VIA USB

I want to access a scale hooked up via a RS232 Serial Port. The machine I'm writing the C# code on does not have a serial port, so I was going to use a USB to Serial Port dongle. Will anything need to be changed with my code?
No, they emulate a serial port at the driver level. Your code, nor the SerialPort class, won't know the difference. The only temptation you'll have to resist is unplugging the USB cable while the port is opened. That works about as well as unplugging a flash memory stick while Windows is writing to it. Your customer will do it anyway, but they usually get bored with it after a couple of times.

How to Determine the serial port name connected to other machine using .NET?

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.

Categories