如何从类文件中读取值到表单 - c#
我是使用C#的新手。我从在线获得这个程序用于串行通信,下面的代码片段是一个方法(属于class.cs),当缓冲区中有数据等待时将调用该方法。我需要自定义这个程序,即我想在我的form1中单独读取ByteToHex(comBuffer)或comBuffer变量的值。检查comBuffer的返回值后,我的程序中有一些需要相应运行的条件。请建议并展示实现它的方法。
I am very new for using C#. I got this program from online for serial communication and the below snippet is a method ( belongs to class.cs) that will be called when there is data waiting in the buffer. I need to customize this program i.e.i want to read the value of "ByteToHex(comBuffer)" or "comBuffer" variable alone in my form1. Upon checking the return value of "comBuffer" I have some conditions inside my program that needs to run accordingly. Please suggest and show a way to achieve it.
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//determine the mode the user selected (binary/string)
switch (CurrentTransmissionType)
{
//user chose string
case TransmissionType.Text:
//read data waiting in the buffer
string msg = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, msg + "\n");
break;
//user chose binary
case TransmissionType.Hex:
//retrieve number of bytes in the buffer
int bytes = comPort.BytesToRead;
//create a byte array to hold the awaiting data
byte[] comBuffer = new byte[bytes];
//read the data and store it
comPort.Read(comBuffer, 0, bytes);
//display the data to the user
DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
break;
default:
//read data waiting in the buffer
string str = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, str + "\n");
break;
}
}
我的尝试:
我浏览了一些文档,解释为类创建一些构造函数并在form1中使用相同的构造函数。但是我无法理解。
What I have tried:
I went through some documentations which explained to create some constructors for the class and use the same in form1. But I couldn't understand quite.
假设Windows窗体并假设您的方法和字段位于名为Class.cs的文件中并且有一个名为该方法所属的Class1。以下应该有效。
你的课程如下。
Assuming windows forms and also assuming that your method and field is in a file called Class.cs and there is a class called "Class1" that the method is part of. The following should work.
Your class would be like below.
public class Class1
{
public string comPort;
public Class1()
{
}
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//determine the mode the user selected (binary/string)
switch (CurrentTransmissionType)
{
//user chose string
case TransmissionType.Text:
//read data waiting in the buffer
string msg = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, msg + "\n");
break;
//user chose binary
case TransmissionType.Hex:
//retrieve number of bytes in the buffer
int bytes = comPort.BytesToRead;
//create a byte array to hold the awaiting data
byte[] comBuffer = new byte[bytes];
//read the data and store it
comPort.Read(comBuffer, 0, bytes);
//display the data to the user
DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
break;
default:
//read data waiting in the buffer
string str = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, str + "\n");
break;
}
}
}
构造函数是片段
The constructor is the snippet
public Class1()
{
}
然后你的form1.cs文件看起来像这样。
And then your form1.cs file would look like this.
public partial class Form1 : Form
{
public Class1 dc = new Class1();
public Form1()
{
InitializeComponent();
var tmp = dc.comPort;
}
}