Search code examples
c#consoleserial-portfirmware.net-4.8

How to send command to RS232 serial port?


I recently got a sas expander card.

The one who gave card to me said:

It has a firmware on the chip and it can show sensor's temperature.

He wants me to develop a C# Console app to execute the firmware.

I didn't know the firmware source code looks like.

But it could be executed by PuTTy and it's connection was via RS232 Serial Port.

PuTTy connection setting:

enter image description here

enter image description here

After I click Open,press Enter and type command sys:

enter image description here enter image description here enter image description here

What I try in my C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;


namespace SerialPortExample
{
    class SerialPortProgram
    {
        // Create the serial port with basic settings
        [STAThread]
        static void Main()
        {
           
           
            SerialPort mySerialPort = new SerialPort("COM5");

            mySerialPort.BaudRate = 115200;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;
            mySerialPort.DtrEnable = true;
            mySerialPort.ReadTimeout = 2000;
            mySerialPort.WriteTimeout = 1000;
            mySerialPort.Open();
            if(mySerialPort.IsOpen)
            { 
                string str= "Enter";
                mySerialPort.Write(str);
 
            }
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            
            Console.ReadLine();
            
        }
        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
        }


    }
}

What my code execute:

enter image description here

What is the problem with my code?

How can I execute firmware via RS232 and interact like PuTTy have done?

My Console app is using .Net Framework 4.7.2.


Solution

  • You are sending the string Enter (like typing E, N, T, E, R). You should probably send the ⏎ Enter key, which is represented on a terminal with the ASCII code 13, i.e. \r in a C# string.

    Also you don't yet send any actual command. Try something like counters\r or sys\r.

    (You currently receive the Enter back as response because that's what you sent out, and the card echoes any incoming characters so it can be used like a shell without blind typing. There is no other response yet because from the card's perspective you basically started typing the (invalid) command Enter but haven't yet submitted it with the ⏎ Enter key.)

    Also I'd suggest adding the event listener for received data before sending any data, otherwise there would be a race condition in which the card could be responding before you even set up your listener and you would lose part of the data.


    Additional note: Using the DataRecieved event may not even be desirable in your case.

    According to docs:

    The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.

    This means that your DataReceived event may not even fire at all if there wasn't enough data sent in total yet (it may fire some time later with all the data at once) - but if you quit your program before that, you will never see.

    As suggested here (emphasis mine):

    Here is my general approach:

    Use event-driven (DataReceived) code for streaming data. That is, where data is delivered at regular intervals, without specific associated commands that originate from your application.

    Use polling for Command/Response protocols. These might involve a thread the you create to poll, but more frequently would be simple loops that may or may not block other operations until they complete.

    So, it would be recommended to use one of the Read methods instead (see docs). There is also ReadTo and ReadLine which you may find useful. You may choose to use ReadTo(" bp1 >") for instance.