110,477
社区成员
发帖
与我相关
我的任务
分享
public partial class MainInterface : Form
{
bool maglevStatus = false;//悬浮状态
bool flagRPM0 = false;//转速为0标志位
string rec;
Form2 fr2 = new Form2();//实例化绘图窗口
Form3 fr3 = new Form3();//实例化测试窗体的对象,来访问其中的textbox
public MainInterface()
{
InitializeComponent();
}
private void MainInterface_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
//开启委托
sp1.DataReceived += new SerialDataReceivedEventHandler(DataReceive);
Init init = new Init(this);//创建一个Init对象
init.FormInit();//窗体初始化
#region 自动搜索串口
try
{
PublicVar.portName = SerialPort.GetPortNames();
cmbPort.Items.AddRange(PublicVar.portName);
cmbPort.SelectedItem = cmbPort.Items[0];
}
catch
{
MessageBox.Show("未搜索到串口");
}
if (cmbPort.Items.Count == 0)
{
btnOpenOrClose.Enabled = false;
}
else
{
btnOpenOrClose.Enabled = true;
}
#endregion
}
/// <summary>
/// 接收数据
/// 参数设置存在paraRec中,调试数据存在debugRec中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void DataReceive(object sender, SerialDataReceivedEventArgs e)
{
try
{
//sp1.ReadLine();
//sp1.ReadExisting
int count = sp1.BytesToRead;//实际接收到的字节数
byte[] buffer = new byte[count];
sp1.Read(buffer, 0, count);//把读取的数据存到缓冲字节数组中
if (buffer[0] == 0x5B && buffer[buffer.Length - 3] == 0x5D) //转义字符\r\n只算r和n的字节数
{
rec = Encoding.Default.GetString(buffer);
PublicVar.paraRec.Add(rec);//把接收到的参数设置的数据存到paraRec中
HandData(rec);//即时处理数据
sp1.DiscardInBuffer();
}
else
{
sp1.DiscardInBuffer();
}
}
catch { }
}
/// <summary>
/// 处理数据
/// </summary>
/// <param name="data">接收到的数据转换成的字符串</param>
void HandData(string data)
{
//handData处理之后的字符串数组
string[] handData = data.Split(new string[] { "[", " ", "]", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (handData[0] == "rpm")
{
rpmtxt.Text = handData[1];
}
else if (handData[0] == "maglev")
{
if (handData[1] == "1")
{
maglevStatus = true;//悬浮状态
magStaPic.Image = Properties.Resources.起浮;
}
else if (handData[1] == "0")
{
maglevStatus = false;//落下状态
magStaPic.Image = Properties.Resources.落下;
}
}
else if (handData[0] == "err")
{
errtxt.Text = handData[1];
}
else if (handData[0] == "x_shift")//将获取到的x y坐标存储到list中,超过500则覆盖
{
fr3.x_shift.Text = handData[1];//把值传入到调试界面
if (PublicVar.x_shift.Count > 500)
{
PublicVar.x_shift.RemoveAt(0);
PublicVar.x_shift.Add(handData[1]);
}
else
{
PublicVar.x_shift.Add(handData[1]);
}
}
else if (handData[0] == "y_shift")
{
fr3.y_shift.Text = handData[1];//把值传入到调试界面
if (PublicVar.y_shift.Count > 500)
{
PublicVar.y_shift.RemoveAt(0);
PublicVar.y_shift.Add(handData[1]);
}
else
{
PublicVar.y_shift.Add(handData[1]);
}
}
else if (handData[0] == "speed_input")
{
fr3.speed_input.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "speed_set")
{
fr3.speed_set.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "speed")
{
fr3.speed.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "speed_output")
{
fr3.speed_output.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "phase")
{
fr3.phase.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "iic")
{
fr3.iic.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "x_offset")
{
fr3.x_offset.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "y_offset")
{
fr3.phase.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "x_offset_feedback")
{
fr3.x_offset_feedback.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "y_offset_feedback")
{
fr3.y_offset_feedback.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "x_shift_average")
{
fr3.x_shift_average.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "y_shift_average")
{
fr3.y_shift_average.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "maglev_flag")
{
fr3.maglev_flag.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "maglev_protect")
{
fr3.maglev_protect.Text = handData[1];//把值传入到调试界面
}
else if (handData[0] == "pwm_protect_tick")
{
fr3.pwm_protect_tick.Text = handData[1];//把值传入到调试界面
}
}
control.BeginInvoke(....)
注册给 UI 线程去调度。UI 线程仅仅在必要的时候才会优化显示。对于你自己的频繁的数据改变,也可以对要显示的数据进行“抽稀”(也就是说区分开来你收到的数据与你要显示的数据),例如仅仅显示最近300毫秒以内最新的一条数据就够了。但是总之是说,事件回调、异步、减少不必要的显示动做,从细节进行设计,而不是以标题党的简单粗暴方式来设计程序。//在新线程中开启一个窗口
private void btnDebug_Click(object sender, EventArgs e)
{
if (fr2==null||fr2.IsDisposed)
{
fr2 = new Form2();
}
//Form2 fr2 = new Form2();
//fr2.Show();
Thread th1 = new Thread(ThreadPro);
th1.IsBackground = true;
th1.Start();
}
public void ThreadPro()
{
MethodInvoker methInvo = new MethodInvoker(ShowForm2);
BeginInvoke(methInvo);
}
public void ShowForm2()
{
fr2.Show();
}