Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Serial COM just echoes what I send instead of data stream. C#

I have a thermometer that has an RS232 connection, so I bought U-Port adapter to USB, per the manual you send it the string "Tcrlf" and it starts outputting data. Tried on PuTTy and it works like a charm.
I’m trying to automate some software with the data stream I am getting, however I am having problems communicating it, I have tried a few snippets from various tutorials around the webs but when I send it the same string via my app it just echoes it back and doesnt stream the data.

This is my Connect Button (after selecting COM port)

private void probeConnectBtn_Click(object sender, EventArgs e)
        {
            //Connect to the NIST-Reference Probe, using Omega HH42 settings:

            if (refProbeCOMPort.SelectedIndex == -1)
            {
                MessageBox.Show("No COM Port selected for NIST-Reference Probe");
                return;
            }
            if (!connectedreferenceProbePort.IsOpen)
            {
                connectedreferenceProbePort.DataBits = HH42DataBits;
                connectedreferenceProbePort.BaudRate = HH42BaudRate;
                connectedreferenceProbePort.PortName = HH42PortName;
                connectedreferenceProbePort.Parity = Parity.None;
                connectedreferenceProbePort.ReadTimeout = 600;
                connectedreferenceProbePort.WriteTimeout = 800;

                connectedreferenceProbePort.StopBits = StopBits.One;
                connectedreferenceProbePort.DataReceived += new SerialDataReceivedEventHandler(probeDataReceived);
            }

            try
            {
                Console.WriteLine("Attempting to open port");
                if (!connectedreferenceProbePort.IsOpen)
                {
                    connectedreferenceProbePort.Open();
                    if (connectedreferenceProbePort.IsOpen)
                    {
                        Console.WriteLine("Port Opened, sending RTS");
                        connectedreferenceProbePort.RtsEnable = true;
                        connectedreferenceProbePort.WriteLine("Tcrl");
                    }

                }
                else
                {
                    Console.WriteLine("Port is already open");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening/writing to Serial Port:: " + ex.Message, "Fatal Error!");
            }
        }

That’s the connect and "attempting" to start the stream, then I have the datareceived part:
(Per the HH42 manual, after receiving the RTS signal, it sends a ">" character meaning that it’s ready to listen).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void probeDataReceived(object sender, EventArgs e)
        {
            string dataReceived = "";
            Console.WriteLine("Data Incoming");
            connectedreferenceProbePort.DiscardOutBuffer();
            try
            {
                dataReceived = connectedreferenceProbePort.ReadExisting();
                Console.WriteLine("Recevied :" + dataReceived);
            } catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());  
            }

            if (dataReceived.Contains(">"))
            {
                Console.WriteLine("> Detected, attempting to write Tcrl");
                connectedreferenceProbePort.Write("Tcrl");

            }
        }

This is my output from the console and a screenshot of PuttY:enter image description here

Attempting to open port
Port Opened, sending RTS
Data Incoming
Recevied :
> Tcrl
> Detected, attempting to write Tcrl
Data Incoming
Recevied :Tc
Data Incoming
Recevied :rl

>Solution :

When you type "Tcrl" and press return in PuTTY, what PuTTY is actually sending are the bytes "T", "c", "r", "l", followed by a carriage return (CR) and a linefeed (NL).

I believe the manual is telling you to send "T", CR, LF, which in C# terms would be the string "T\r\n".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading