Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

wpf - C# SerialPort Read Command Help/Interpretation

New to serial port communication, used to C#, so I'm using a WPF app to communicate.

I have a thermal controller that came with a setup app and a user manual.

I am tasked with writing an application that will send commands to raise/lower the temperature.

To start, I would like to simply read the set temperature.

In APPENDIX C of the manual there are examples of the commands available.
The Read command (INPUT1) example says to "send" *00010000000041^ and I will receive a similar string that I can use to interpret the set temperature of the controller.

My Problem
I do not know how to properly send this command or receive the result!
The MainWindow.xaml.cs instantiates a MainViewmodel object, and that's where my code lies.

The constructor:

public MainViewmodel( MainWindow mw ){
        TheMainWindow = mw;
        DisplayAllPorts();
        SP_COM5 = InitializePort( "COM5" );

        // ReadTemp() is an empty try/catch block right now. 
        ReadTemp( SP_COM5 );
}

The InitializePort method: this works, I tested by Opening and Closing the port.

public SerialPort InitializePort( string portName ){
            var baud = 9600;
            var parity = Parity.None;
            var dataBits = 8;
            var stopBits = StopBits.One;

            var port = new SerialPort(portName, baud, parity, dataBits, stopBits);

            port.ReadTimeout = 500;
            port.WriteTimeout = 500;
            port.DataReceived += SP_DataReceived;

            return port;
}

What I need to know is how to to send that Read command *00010000000041^.
Do I need to convert it into a byte array?
Will I receive a string back, or a series of bytes? I do understand a DataReceived event should be attached to the SerialPort object in question.
What is the correct SerialPort read method to use in this case?

How do you (personally) interpret the examples in the manual attached?

Thank you for any help!

question from:https://stackoverflow.com/questions/65944027/c-sharp-serialport-read-command-help-interpretation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what I understand from the manual, the code is an ASCII code;

*00010000000041^

If you take an ASCII table, you can find that corresponding HEX values for these:

'*' : 0x2A    
'0' : 0x30    
'1' : 0x32   
'4' : 0x34  
'^' : 0x5E

Combined this gives: 2a 30 30 30 31 30 30 30 30 30 30 30 30 34 31 5e Each pair of 2 represents one byte, so you have to send this array of bytes to the COM port.

You can get this array like so:

var bytes = Encoding.ASCII.GetBytes("*00010000000041^");

And then write to the serial port like so:

_serialPort.Write(bytes, 0, bytes.Length);

There is also a _serialPort.WriteLine that takes a string, you can try that and see if it gives the same result. I would suspect so.

To read the returned bytes, you can do the inverse in a SerialPort.Read(...) or in the DataReceived event (although I think you can only read a string there, it could be that this is already the right format then):

Encoding.ASCII.GetString(bytes);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...