using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace 飞思卡尔_上位机
{
public partial class Form1 : Form
{
char Rdata;
double channelA, channelB, channelC, channelD;
bool isStart=false;
int pointx = 0;
float pointtempA = 0;
float pointtempB = 0;
float pointtempC = 0;
float pointtempD = 0;
Graphics pic; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
else
MessageBox.Show("串口已关闭");
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.SelectedItem != null && comboBox2.SelectedItem != null)
{
if (isStart)
{
isStart = false;
serialPort1.Close();
button1.Text = "打开串口";
timer1.Enabled = false;
}
else
{
isStart = true;
serialPort1.PortName = Convert.ToString(comboBox1.SelectedItem);
serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
serialPort1.Open();
button1.Text = "关闭串口";
timer1.Enabled = true;
}
}
else
{
MessageBox.Show("初始化设置不完整!", "提示", MessageBoxButtons.OK);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK);
}
} private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("COM1");
comboBox1.Items.Add("COM2");
comboBox1.Items.Add("COM3");
comboBox1.Items.Add("COM4");
comboBox1.Items.Add("COM5");
comboBox1.Items.Add("COM6");
comboBox1.Items.Add("COM7");
comboBox1.Items.Add("COM8");
comboBox1.Items.Add("COM9"); comboBox2.Items.Add("4800");
comboBox2.Items.Add("9600");
comboBox2.Items.Add("14400");
comboBox2.Items.Add("19200");
comboBox2.Items.Add("38400");
comboBox2.Items.Add("56000");
comboBox2.Items.Add("115200"); pic = this.pictureBox1.CreateGraphics();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (timer1.Enabled == true) timer1.Enabled = false;
if (serialPort1.IsOpen) serialPort1.Close();
Close();
} private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(Conduct_data)); } private void Conduct_data(object sender, EventArgs e)
{
string temp;
Rdata = Convert.ToChar(serialPort1.ReadByte()); if ((Rdata)=='!')
{
char[] box=new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte()))!='!')
{ box[i] = Rdata;
i++;
} temp = new string(box);
textBox1.Text =temp.Trim();
channelA = Convert.ToDouble(temp.Trim());
textBox5.Text = Convert.ToString(3.3 / 256 * channelA);
} if ((Rdata) == '@')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '@')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox2.Text = temp.Trim();
channelB = Convert.ToDouble(temp.Trim());
textBox6.Text = Convert.ToString(3.3 / 256 * channelB);
} if ((Rdata) == '#')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '#')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox3.Text = temp.Trim();
channelC = Convert.ToDouble(temp.Trim());
textBox7.Text = Convert.ToString(3.3 / 256 * channelC);
} if ((Rdata) == '$')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '$')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox4.Text = temp.Trim();
channelD = Convert.ToDouble(temp.Trim());
textBox8.Text = Convert.ToString(3.3 / 256 * channelD);
}
} private void timer1_Tick(object sender, EventArgs e)
{
pic.DrawLine(Pens.Green, pointx, pictureBox1.Height - ((pictureBox1.Height / 4) * pointtempD / 256), pointx + 1, pictureBox1.Height - ((pictureBox1.Height / 4) * (float)channelD / 256));
pic.DrawLine(Pens.Yellow, pointx, 3 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempC / 256), pointx + 1, 3 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelC / 256));
pic.DrawLine(Pens.Orange, pointx, 2 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempB / 256), pointx + 1, 2 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelB / 256));
pic.DrawLine(Pens.Red, pointx, pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempA / 256), pointx + 1, pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelA / 256)); pointtempA = (float)channelA;
pointtempB = (float)channelB;
pointtempC = (float)channelC;
pointtempD = (float)channelD;
pointx++;
if (pointx == 490)
{
pic.Clear(Color.White);
pointx = 0;
}
} }
}做个上位机但出现这种问题,这是什么原因,该怎么解决?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace 飞思卡尔_上位机
{
public partial class Form1 : Form
{
char Rdata;
double channelA, channelB, channelC, channelD;
bool isStart=false;
int pointx = 0;
float pointtempA = 0;
float pointtempB = 0;
float pointtempC = 0;
float pointtempD = 0;
Graphics pic; public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
else
MessageBox.Show("串口已关闭");
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.SelectedItem != null && comboBox2.SelectedItem != null)
{
if (isStart)
{
isStart = false;
serialPort1.Close();
button1.Text = "打开串口";
timer1.Enabled = false;
}
else
{
isStart = true;
serialPort1.PortName = Convert.ToString(comboBox1.SelectedItem);
serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
serialPort1.Open();
button1.Text = "关闭串口";
timer1.Enabled = true;
}
}
else
{
MessageBox.Show("初始化设置不完整!", "提示", MessageBoxButtons.OK);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK);
}
} private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("COM1");
comboBox1.Items.Add("COM2");
comboBox1.Items.Add("COM3");
comboBox1.Items.Add("COM4");
comboBox1.Items.Add("COM5");
comboBox1.Items.Add("COM6");
comboBox1.Items.Add("COM7");
comboBox1.Items.Add("COM8");
comboBox1.Items.Add("COM9"); comboBox2.Items.Add("4800");
comboBox2.Items.Add("9600");
comboBox2.Items.Add("14400");
comboBox2.Items.Add("19200");
comboBox2.Items.Add("38400");
comboBox2.Items.Add("56000");
comboBox2.Items.Add("115200"); pic = this.pictureBox1.CreateGraphics();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (timer1.Enabled == true) timer1.Enabled = false;
if (serialPort1.IsOpen) serialPort1.Close();
Close();
} private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(Conduct_data)); } private void Conduct_data(object sender, EventArgs e)
{
string temp;
Rdata = Convert.ToChar(serialPort1.ReadByte()); if ((Rdata)=='!')
{
char[] box=new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte()))!='!')
{ box[i] = Rdata;
i++;
} temp = new string(box);
textBox1.Text =temp.Trim();
channelA = Convert.ToDouble(temp.Trim());
textBox5.Text = Convert.ToString(3.3 / 256 * channelA);
} if ((Rdata) == '@')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '@')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox2.Text = temp.Trim();
channelB = Convert.ToDouble(temp.Trim());
textBox6.Text = Convert.ToString(3.3 / 256 * channelB);
} if ((Rdata) == '#')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '#')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox3.Text = temp.Trim();
channelC = Convert.ToDouble(temp.Trim());
textBox7.Text = Convert.ToString(3.3 / 256 * channelC);
} if ((Rdata) == '$')
{
char[] box = new char[5];
int i = 0;
while ((Rdata = Convert.ToChar(serialPort1.ReadByte())) != '$')
{ box[i] = Rdata;
i++;
}
temp = new string(box);
textBox4.Text = temp.Trim();
channelD = Convert.ToDouble(temp.Trim());
textBox8.Text = Convert.ToString(3.3 / 256 * channelD);
}
} private void timer1_Tick(object sender, EventArgs e)
{
pic.DrawLine(Pens.Green, pointx, pictureBox1.Height - ((pictureBox1.Height / 4) * pointtempD / 256), pointx + 1, pictureBox1.Height - ((pictureBox1.Height / 4) * (float)channelD / 256));
pic.DrawLine(Pens.Yellow, pointx, 3 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempC / 256), pointx + 1, 3 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelC / 256));
pic.DrawLine(Pens.Orange, pointx, 2 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempB / 256), pointx + 1, 2 * pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelB / 256));
pic.DrawLine(Pens.Red, pointx, pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * pointtempA / 256), pointx + 1, pictureBox1.Height / 4 - ((pictureBox1.Height / 4) * (float)channelA / 256)); pointtempA = (float)channelA;
pointtempB = (float)channelB;
pointtempC = (float)channelC;
pointtempD = (float)channelD;
pointx++;
if (pointx == 490)
{
pic.Clear(Color.White);
pointx = 0;
}
} }
}做个上位机但出现这种问题,这是什么原因,该怎么解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货