简体   繁体   中英

Execute a shell command in C# to serial port mono

Im trying to send a string over an serial port using C#.

I've accomplished to send this string via the terminal on my RaspBerry pi ( Raspbian) using the following command:

echo "TestMode On" > /dev/ttyAMC0

This works! but this is in the terminal only. What I would like to do is execute this specific command using C# What my current approach is is this :

public bool DoSomething(){
        MySerial = new SerialPort("/dev/ttyACM0");
        MySerial.Open();
        MySerial.ReadTimeout = 400;
        string s = "echo \"TestMode On\"\r";
        Log.Information(s);
        return SendData(s);
}
private bool SendData(string command)
    {
        MySerial.Write(command);
        return true;
    }

I'm Using Serilog to log information and in that console window I can see that the string really is: echo "TestMode On"

What am I missing here?.

Thanks in advance,

Herm L.

Echo sends a newline by default and you would not include the quotes:

This line:

string s = "echo \"TestMode On\"\r";

Becomes:

string s = "TestMode On\n";

The solution in this case is to change the encoding on the serial port from ASCII to UTF8. Apperantly this makes a difference here. Thanks everyone for their time and efforts!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM