Problems enabling dtr in my serial communication program - c#

I'm making a program which communicates with a serial port(RS232 mainly but in this instance I'm using a usb device). Right now I'm having a problem when enabling DTR.
private void CheckBox_DTR_CheckedChanged(object sender, EventArgs e)
{
if(COMport != null)
{
if (CheckBox_DTR.Checked)
{
COMport.DtrEnable = true;
}
else
{
COMport.DtrEnable = false;
}
}
}
In this part of my code I'm enabling DTR if checkbox gets checked. When I checked the pin voltage everything seems to be ok because voltage increases when I enable it. But there's the problem: when I enable DTR using other terminals the device throws out some info but when I do this with my program it doesn't send that info.
My program
Other Serial Terminal
As you can see other terminal has some additional info that device sends out upon enabling DTR. So I'm not completely sure what should I do to receive that info from the device(do I need some additional code or something)...

Generally, when using DTR you need to turn hardware handshaking on. I would try setting COMport.Handshake = Handshake.XOnXOff;.
According to MSDN here:
Data Terminal Ready (DTR) is typically enabled during XON/XOFF software handshaking and Request to Send/Clear to Send (RTS/CTS) hardware handshaking, and modem communications.

Related

C# SerialPort problems with two different versions of a device

I am trying to read data from a device connected via USB.
For creating the trigger the code looks like:
private SerialPort realTimePort;
realTimePort = new SerialPort();
realTimePort.BaudRate = 9600;
realTimePort.PortName = "COM4";
realTimePort.ReadTimeout = 5000;
realTimePort.ReadBufferSize = 32768;
realTimePort.ReceivedBytesThreshold = 1;
realTimePort.BaudRate = 9600;
realTimePort.ReadBufferSize = 4096;
realTimePort.ParityReplace = 63;
realTimePort.Parity = Parity.None;
realTimePort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(realTimePort_DataReceived);
realTimePort.Open();
To read the data, which was sent, the code looks like:
public void realTimePort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Do something with the data
}
With one version of the device everything works fine and the trigger starts, when data was sent, but with a newer software-version of the device realTimePort_DataReceived is never triggered.
At first i thought, that the problem might be, that the device never sends data, but then i tried to read the data with "Tera Term" and there i can see exactly, what i am expecting. I also compared the data with "Tera Term", which was sent of the devices with the different software-versions, but it is exactly the same string.
Do you have any ideas, why the event is triggered with the older software-version and not with the newer one?
An employee of the manufacturer of the device already gave me the specification of the SerialPort, because i had this problem, but it didn't help me.
It is hard to reproduce the issue as i am not aware what type of device you are using and what type of data is sends here are some tips you can evaluate a quick check list to ensure the correct data receiving.
1. Play with RTS or DTR port flags for new version device
Basically some new versions of hardware uses flags of SerialPort e.g. DTR (Data Terminal Ready) indicates that your code is ready to receive, and RTS (Request to Send) a request to device to actually send data. for older hardware types it was not mandatory to use these flags although in modern devices its still not but just a standard practice so you should experiment & try enabling these by your code e.g.
realTimePort.RtsEnable = true; //enable this mode
realTimePort.DtrEnable = true; //and also this one
2. Try to read device error stream
It is possible that your new version hardware is sendind data over error stream, the tool you was using utilizes both streams for data read so you can subscrive to error event like.
realTimePort.ErrorReceived += new SerialErrorReceivedEventHandler(sPort_ErrorReceived);
private static void sPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
//process your error, it will give you further hint about why part of your question.
}

FTDI FTD2xx_NET detects unknown device. CyclePort fails

I want to detect a connected usb device (FT232R USB UART, Virtual com port driver). This usually worked fine, but due to an windows update my device is detected as FTDI_DEVICE_UNKNOWN. Strange behaviour is, that when I first plug in my device and then start the software, everything works fine. If I first start the software and then plug in the device, it it not detected. I tried to use CyclePort to reconnect the device, but this fails with status FT_OTHER_ERROR. The device is detected and displayed in both cases in the windows device manager. Drivers were already updated and also installed manually.
Any ideas as to what is causing this?
Here is my code:
List<string> pcdSerials = new List<string>();
FTDI ftdi = new FTDI();
UInt32 ftdiDeviceCount = 0;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
ftStatus = ftdi.GetNumberOfDevices(ref ftdiDeviceCount);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
return pcdSerials;
}
FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
ftStatus = ftdi.GetDeviceList(ftdiDeviceList);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
return pcdSerials;
}
foreach (var info in ftdiDeviceList)
{
if (info.Type == FTDI.FT_DEVICE.FT_DEVICE_UNKNOWN)
{
if (ftdiDeviceList.Count() == 1)
{
// The effect of this function is the same as disconnecting then reconnecting the device from USB.
// Possible use of this function is in situations where a fatal error has occurred and it is difficult, or not possible, to recover without unplugging and replugging the USB cable. This function can also be used after re-programming the EEPROM to force the FTDI device to read the new EEPROM contents which previously required a physical disconnect-reconnect.
ftStatus = ftdi.CyclePort();
if (ftStatus == FTDI.FT_STATUS.FT_OK)
{
// Port has been cycled. Close the handle.
ftStatus = ftdi.Close();
}
else
{
// FT_CyclePort FAILED!
}
break;
}
}
if (info.Description != "FT232R USB UART")
continue;
if (info.Type != FTDI.FT_DEVICE.FT_DEVICE_232R)
continue;
pcdSerials.Add(info.SerialNumber);
}
return pcdSerials;
This NOT an answer, but a comment. Being new to Stack overflow it wouldn't let me simply add a comment to your question, or I don;t know what I am doing. :)
I am having what sounds like the same issue. I wrote a wrapper class over 10 years ago that has worked fine until the new FTDI driver 2.12.36.1 was updated on client's computers. FTDI released driver version 2.12.36.2 on 6/17/2021 and I was hoping that it would resolve the issue but it did not.
What I have found is that calling FT_CreateDeviceInfoList does return the correct number of devices connected. Then I call FT_GetDeviceInfoDetail to obtain the device type, description, and FTDI serial number. The device type comes back as value 0x03 which is FT_Device_Unknown and the description and serial number appear to come back empty. I use the FTDI serial number to open the device by its serial number. Without a valid serial number, I cannot open the device. I have used this method as outlined as the preferred method by FTDI for years with no issues until now.
Hopefully someone else can chime in with an answer as I have a lot of customers who are unable to connect to my product with their PCs.
I am going to reach out to FTDI support to see if they can shed some light on this problem. I will post back here if I hear something back.
I came across the same problem just recently. If you run FT_prog you can replicate this by removing the dongle and putting it back. FT_prog takes a second or so to recognise the device again.
I put the checking code in a loop with a 1 second wait and it now finds the dongle on the second attempt reliably.

How to check if a Serial port is disconnected in c#

I want to check if a Serial port is disconnected or not after clicking a button in C# WinForms.
Eg:
private void btn_Click()
{
if("code to check device disconnected")
{
Do this;
}
else
{
Do this;
}}
PS: Is there any way to check that the device is disconnected when the application is running.
SerialPinchanged, ErrorRecieved, DataRecieved don't help.
Thank you in advance
It depends on the specifications of the target device connected to the COM port.
For example, if RTS/CTS or DTR/DSR are cross-connected, or if the device equivalent to a modem has a DCD or RI valid signal line, and the CTS/DSR/DCD/RI signal line shows valid connection information. , There may be a case where there is a command/response specification for health check or keep alive.
As an alternative, if the COM port is a USB Serial converter, you can check whether or not the device with the corresponding VID and PID exists, but make sure that the device is actually connected at the end of the converter. There is no guarantee.
It is simple logic that whenever SerialPort is disconnected, serialPort.IsOpen turns to false.
private void btn_Click()
{
if(!SerialPort.IsOpen)//If SerialPort is not open
{
Do this;
}
else//Else if SerialPort is open
{
Do this;
}
}

SerialPort.ReadLine() does not read data from USB COM port sent by Arduino

I'm sending textual data to COM15 (via micro USB) using Arduino. On my desktop, I'm trying to read the data from my C# application. However, when I run it, the console simply shows nothing and the program stucks at the line "string s = myPort.ReadLine()".
The following is my C# program:
static void Main(string[] args)
{
var myPort = new SerialPort("COM15", 115200);
myPort.Open();
while (true)
{
var s = myPort.ReadLine(); // execution stucks here waiting forever!
Console.WriteLine(s);
}
}
The following is the Arduino code (sends data to COM15):
int counter = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(" Data Loop = " + String(counter));
counter++;
delay(500);
}
The arduino serial monitor does show the data being received at COM15. I also tried other software that read COM ports and verified that the data is available at the port.
By adding the following line before myPort.Open() command, I managed to fix my issue and read from the COM successfully:
myPort.DtrEnable = true;
You may ask what is Dtr flag. Dtr stands for "Data Terminal Ready" and according to Wikipedia:
Data Terminal Ready (DTR) is a control signal in RS-232 serial
communications, transmitted from data terminal equipment (DTE), such
as a computer, to data communications equipment (DCE), for example a
modem, to indicate that the terminal is ready for communications and
the modem may initiate a communications channel.

C# NetworkAvailabilitychangedEvent

VS 2008
I am developing an application that has to detect whether a client has a network connection. i.e. LAN cable plugged in or wireless switched on. I have been using the code below.
I am using the NetworkAvailabilitychangedEvent to fire an event when their wireless is turned off or the cable has been pulled out. However, this only works if the user has only 3 connections present (LAN, Wireless, and loopbacks).
Microsoft: "The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface".
However, some client has more than 3 connections. One client had a bluetooth connection and someone else had some VMWare connections. On these client it failed to fire the event.
Is there anyway I can ignore all these connections I am not interested in listening out for, and just listen on the LAN and Wireless?
Many thanks for any advice,
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(OnNetworkChangedEvent);
}
private void OnNetworkChangedEvent(object sender, NetworkAvailabilityEventArgs e)
{
bool available = e.IsAvailable;
NetworkInterface[] networkConnections = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in networkConnections)
{
if (ni.Name == "Local Area Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("LAN disconnected: " + ni.Description);
}
}
else if (ni.Name == "Wireless Network Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("Wireless disconnected: " + ni.Description);
}
}
}
}
Perhaps you need to implement some sort of polling agent that runs on a seperate thread and regularly tries to contact something listening on a remote server (or servers?).
When it fails (or perhaps when a preset number of connection attempts fail) then fire an event. Same when it goes from a fail-succeed state.
Your application would subscribe to these events and take action accordingly.
Of course this may not be suitable for your scenario..

Categories

Resources