Recently, a test system was developed to read data from the power meter and display it on the PC software.
The computer interface is not skipped in this lecture. Here we mainly talk about the implementation of a class.
At first, the leader gave me a remote three-phase power meter and asked me to write a host computer. It was very simple and I will implement it in a moment.
Because it is a three-phase power meter, we first write a one-phase base class.
public class OnePhase { public float V { get; set; } public float A { get; set; } public float W { get; set; } public float VA { get; set; } public float PF { get; set; } }
Remote class
Public class pf9830 {public pf9830 () {p1 = new onephase (); P2 = new onephase (); P3 = new onephase (); P4 = new onephase ();} public onephase P1 {Get; set;} public onephase P2 {Get; set;} public onephase P3 {Get; set;} public onephase P4 {Get; set ;} public void getvalues (byte [] data) {/** explanation of serial port read value ** p1.v = ...... */}}
Implementation class
class Test { public Test() { PowerMeter = new PF9830(); Port = new SerialPort(); Port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived); } public PF9830 PowerMeter { get; set; } public SerialPort Port { get; set; } void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) { int length = Port.BytesToRead; byte[] data = new byte[length]; Port.Read(data, 0, length); PowerMeter.GetValues(data); } }
The work is completed quickly and runs properly. However, one day, the leader said that the test accuracy in the distance is too bad. to change to a power meter in the river for testing, it is best to have a drop-down list to select which instrument to use.
At the beginning, I wrote another class of YOKOGAWA, as shown below:
Public class wt230 {public wt230 () {p1 = new onephase (); P2 = new onephase (); P3 = new onephase (); P4 = new onephase ();} public onephase P1 {Get; set;} public onephase P2 {Get; set;} public onephase P3 {Get; set;} public onephase P4 {Get; set ;} public void getvalues (byte [] data) {/** explanation of serial port read value ** p1.v = ...... */}}
Class test {public Enum powermeters {remote, YOKOGAWA}; public test () {powermeter1 = new pf9830 (); powermeter2 = new wt230 (); Port = new SerialPort (); Port. datareceived + = new feature (port_datareceived);} public pf9830 powermeter1 {Get; set;} public wt230 powermeter2 {Get; set;} public SerialPort {Get; set ;} private powermeters flag; void port_datareceived (Object sender, serialdatareceivedeventargs e) {int length = port. bytestoread; byte [] DATA = new byte [length]; port. read (data, 0, length); Switch (FLAG) {Case powermeters. YOKOGAWA: powermeter2.getvalues (data); break; Case powermeters. remote: powermeter1.getvalues (data); break; default: Break ;}}}
Here we will find that the wt230 and pf9830 classes have a lot of the same Code, but the getvalues () method is not available.
There are two instances of the implementation class: powermeter1 and powermeter2, which may cause great problems in Ui interaction.
The preceding two problems can be easily solved by using abstract classes.
First, we need to abstract a Power Meter class:
Public abstract class powermeterbase {public powermeterbase () {p1 = new onephase (); P2 = new onephase (); P3 = new onephase (); P4 = new onephase ();} public onephase P1 {Get; set;} public onephase P2 {Get; set;} public onephase P3 {Get; set;} public onephase P4 {Get; set ;} public abstract void getvalues (byte [] data); // abstract method}
Then the class of the instance is far away from the river
Public class pf9830: powermeterbase {public override void getvalues (byte [] data) {/** explanation of serial port read value ** p1.v = ...... */}}
Public class wt230: powermeterbase {public override void getvalues (byte [] data) {/** explanation of serial port read value ** p1.v = ...... */}}
Now we have implemented a much simpler UI interaction class.
Class test {public Enum powermeters {remote, YOKOGAWA}; public test () {Port = new SerialPort (); Port. datareceived + = new serialdatareceivedeventhandler (port_datareceived);} public powermeterbase powermeter {Get; set;} // implement the public SerialPort {Get; set;} private powermeters flag; void port_datareceived (Object sender, serialdatareceivedeventargs e) {int length = port. bytestoread; byte [] DATA = new byte [length]; port. read (data, 0, length); Switch (FLAG) {Case powermeters. YOKOGAWA: powermeter = new wt230 (); powermeter. getvalues (data); break; Case powermeters. remote: powermeter = new pf9830 (); powermeter. getvalues (data); break; default: break ;}}}
Summary:
Power Meter (abstract class) wt230 (real class) pf9830 (real class)
Display (polymorphism) of different power meters)
C # Abstract class