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.
Related
I am using C# in Visual Studio 2019; however, this problem seems to occur using various terminal programs.
I am connected to a Shekel Merav 2000 Scale which has a serial connection.
I am using the .Net SerialPort class and using serialPort.DataReceived event.
If I use a USB to serial cable to connect my scale to the PC, everything works fine, the scale continuously streams the weight to me. If I connect the scale directly to the serial port on my PC, the port opens successfully, however nothing is received from the scale.
Is there anything special that the USB converter is doing that "kicks" the serial port into life.
Thanks so much!
I have tried using a program like Serial Port Monitor and it also does not receive data.
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.
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 have a device which i have connected to my laptop. This device connects to computer using serial port, however my laptop doesn't have a serial port so i have added a connector to the serial cable to convert it to USB. Now i can see it in Device manager
But here you can see, i can just see the information about the converter not about the device. Why is it so? How i would be able to know about the device?
But the device software shows that it has detected it.
My purpose to get the information is, i want to know the information of the device manufacturer and its ID.
For this i have used software like: Device Monitoring Studio and Advanced USB Port Monitor. But the information they provided is just about the Serial to USB converter not about the device.
Below is the snapshot of the information i am getting from these software:
Please help me finding the manufacturer of that device.
Main Aim: This will help me to loop through all the ports and check which one is the right port, so that i can recognize that my device is connected to which port.
Serial ports are an I/O port design that dates from the stone age of computing. Used for example back when Dennis Ritchie typed in the source of the first C compiler on a teletype. The electrical interface is very simple and the chip required to generate signals is not much more than a glorified shift register. In particular it doesn't has the dedicated embedded processor that a USB device needs. Nor has it any kind of protocol to have a device identify itself. Albeit that a standard exists for modems (the Hayes AT protocol).
Accordingly, serial devices are not plug-and-play devices. And there's no way for the computer to figure out what kind of device is on the other end of the cable.
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.