Cannot acquire serial port names in WinForms - c#

I am intend to design a GUI for my arduino project. However, I couldn’t get serial port names. I inserted the necessary library, and used SerialPort.GetPortNames() method. Then, I tried to add port names into ComboBox, also tried ListBox later, via foreach function. Eventually, I couldn’t see any port name neither in the program nor in the Item->(Collectibles). However, I used a button which shows port names in MessageBox, and I saw only COM3 and COM4.
I am sorry if I am confusing, thank you for yourr further comments.

Related

Renaming a selected Serial Port from COMX to COMY using C#

I can currently read and write to my serial ports on the machine, but I wish to be able to change from, for example, COM3 to COM4 within my program. I can't find a way to do this online - I've seen you can potentially use Windows Developer Kit but unsure as to whether that can be used with C#? Thanks.

Serial Port: Different answers from device

I have a little problem with translating my code. I'm trying to make some relays (set on a custom made PCB) to turn on and off while I press a button.
The problem is I made the code that's doing it (so it is turning on and off depending on my actions) but I also need to read the answer from the machine. The point is I had an older version of the code written in C#, and when sending the code through that program, I get the OK answer, while using the new version of the code written in C++, I get only the first letter of the answer.
I was following this using a serial port monitor to see what the machine sends back, and I had the same result (is not like my code only reads one letter). So, the point is: the C# code is getting the full answer ("AWOK11") while the C++ code gets only the first letter of it ("A").
C# Code done in SharpDevelop and C++ code done in Qt Creator.
C++ code:
QSerialPort portverf;
portverf.setBaudRate(QSerialPort::Baud9600);
portverf.setDataBits(QSerialPort::Data8);
portverf.setParity(QSerialPort::NoParity);
portverf.setStopBits(QSerialPort::OneStop);
portverf.setFlowControl(QSerialPort::NoFlowControl);
portverf.setPortName("COM41");
portverf.open();
port.write(command);
QString result = portverf.readAll();
Thanks in advance for helping!
The method QIODevice::readAll() reads all available data from the device. If there was only an A available, it will return an A. It won't wait for additional characters. Since you read right after writing, reading only the first character is to be expected.

Intercept and modify USB packets

The project is as follows;
I would like to create an application that will be able to see packets going to a USB device. When a certain bit of data that is known is sent to the device, the returning data will be intercepted and modified before the application requesting it, gets it. I have used a USB sniffer to see the packets being sent and I know exactly what bits need to be changed. My two questions are;
Is this a possible software solution?
Will this have to be a hardware solution?
Additional Information –
The USB device uses a FTDI245R chip for communication. I know the VID and PID of the device.
I have experience programming in vb.net and C# but I have never done anything with USB
I would like the application to be able to have a number entered and changed to hex data and that is what would be sent to the device. The number being entered would be changed frequently.
Any input is appreciated.
It sounds to me like you want an upper filter driver to the FTDI driver. I don't know what class or type of device you are using or if it has a vendor specific driver or not, but here is a sample that shows how to create an upper filter to a vendor specific driver. This example uses the OSRUSBFX2 device and sample driver in the WDK, you'll want to change the code to work and interface with the FTDI driver instead.

Checking external hardware through serial port

In my code, I'm setting the following values:
public SerialPort comPort = new SerialPort();
comPort.PortName = portName;
The PortName can be COM17, for example.
Now if I've add two devices through my GUI, both on COM17, one will eventually throw an exception (Access to COM17 denied). Now I'd like to check which device is hooked on that COM-port so if my application recognises it, it can send commands through the port, or if it's not recognized, it'll disable the option in my GUI.
So, is there a way to get unique information from the COM-port which I can save in my database when it's being installed?
EDIT: To clarify, if the wrong device is hooked to that COM port (other than when I installed), I get an error. I catch that error so I know it's not the right one. But I'd rather like to know which device that might be. Say I've got 2 drivers DLL's included, 2 different devices, and somebody switched them up. Then my application could check and see the correct names, and therefore conclude it's only a matter of changing the COM-ports of the installed objects, instead of just throwing an error and saying the user has to reinstall.
This is going to depend on your hardware. If the devices attached will give a predictable and consistent response to a particular signal message, then you can use that to identify them. I do the same thing to figure out which COM port a piece of proprietary hardware is attached to. I iterate through all the COM ports and send a message that I know will give a particular response from the hardware. Which ever port gives me the expected result is the one with the hardware.

Serial communication write

I have written a simple c# app having functionality of serial
communication. I am using it to read and write to a device. The device
recognizes string commands. I am successfuly able to read from device
using this app. But peoblem is in writing(sending) commands to device.
I am simply using
if (serialPort.IsOpen == true)
{
serialPort.Write("Command1");
}
But my device does not respondes to it.
To check, I tried sending same command using hyperterminal to my
device and my device recognizes it and works perfectly.
Can anybody guide me whats the accurate way to write or what
needs to be make sure for writing to serial port in c#. Is this encoding issue...
(Serial Read using this same app is working pretty fine !!)
Thank you for your time.
Moreover, there is no exception or error and in debug mode
this line executes.
This is a very common problem and invariably caused by leaving the Handshake property set to None. Serial port devices almost always pay attention to the handshake signals and ignore anything you send if the DTR signal isn't turned on. Which indicates that you are powered up and the data it receives isn't noise. Setting DtrEnable to true will be required, that's what HyperTerminal does as well.
And it won't send anything back when it thinks you are not ready to receive anything because the RTS signal isn't turned on. Setting RtsEnable to true will be required, that's what HyperTerminal does as well. Or just set the Handshake property correctly, Handshake.RequestToSend is the common requirement.
If you still have trouble then you can use SysInternals' PortMon to compare the serial port driver commands your program issues against the ones issued by HyperTerminal.
Hyperterminal likely appends a newline character to your input when you hit the Enter key to send information, which consists of a "line feed" and "carriage return".
Check the documentation for your hardware, but my guess is that the device you're attempting to send information to is looking for a carriage return, '\r', to signify the end of input. Like others in the comments are saying, append the appropriate character(s) to the end of your input.
Alternatively, you can use the SerialPort.WriteLine function to accomplish the same thing as appending a newline '\r\n' character to your input without explicitly modifying it.
if (serialPort.IsOpen)
serialPort.WriteLine("Command" + "\r\n");

Categories