一个程序,是将电机的电流和功率实时显示在Picturebox中,现在情况是开启定时器后可以实时显示曲线但电机速度马上降到零速,去掉开启语句后电机可以正常运转。问题很奇怪,请大家帮忙分析下相关代码如下
public Bitmap mybitmap;
private void DrawLines(Color color, PictureBox PictureBox3, Point[] ptlist)//picturebox3
{
mybitmap = new Bitmap(PictureBox3.Width, PictureBox3.Height);
Graphics doublebufferg = Graphics.FromImage(mybitmap);
doublebufferg.Clear(Color.Black); Rectangle rect = new Rectangle(0, 0, PictureBox3.Width, PictureBox3.Height);
doublebufferg.FillRectangle(new SolidBrush(Color.Black), rect); Point temp = new Point();
for (int j = 0; j < PictureBox3.Width / 5 - 1; j++)
{
temp = ptlist[j + 1];
ptlist[j] = new Point(temp.X - 5, temp.Y);
} Point lastpt = new Point();
lastpt.X = PictureBox3.Width;
lastpt.Y = PictureBox3.Height - Convert.ToInt32(mcurrent) * PictureBox3.Height / 100;
ptlist[PictureBox3.Width / 5 - 1] = lastpt;
doublebufferg.DrawLines(new Pen(color, 1), ptlist); Graphics g = PictureBox3.CreateGraphics(); g.Clear(Color.Black);
g.DrawImage(mybitmap, 0, 0);
} public Bitmap mybitmap1;
private void DrawLines1(Color color, PictureBox PictureBox4, Point[] ptlist1)//picturebox4
{
mybitmap1 = new Bitmap(PictureBox4.Width, PictureBox4.Height);
Graphics doublebufferg = Graphics.FromImage(mybitmap1);
doublebufferg.Clear(Color.Black); Rectangle rect = new Rectangle(0, 0, PictureBox4.Width, PictureBox4.Height);
doublebufferg.FillRectangle(new SolidBrush(Color.Black), rect); Point temp1 = new Point();
for (int j = 0; j < PictureBox4.Width / 5 - 1; j++)
{
temp1 = ptlist1[j + 1];
ptlist1[j] = new Point(temp1.X - 5, temp1.Y);
} Point lastpt1 = new Point();
lastpt1.X = PictureBox4.Width;
lastpt1.Y = pictureBox4.Height - Convert.ToInt32(cpower) * PictureBox4.Height / 20000;
ptlist1[PictureBox4.Width / 5 - 1] = lastpt1;
doublebufferg.DrawLines(new Pen(color, 1), ptlist1); Graphics g = PictureBox4.CreateGraphics(); g.Clear(Color.Black);
g.DrawImage(mybitmap1, 0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false; //关闭线程间检查以解决线程间操作无效问题
if (!serialPort1.IsOpen)
serialPort1.Open(); //设置控件的样式和行为,以减少图像的闪烁
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.UpdateStyles(); //实例化数组
ptlist = new Point[this.pictureBox3.Width / 5];
Point pt;
for (int i = 0; i < this.pictureBox3.Width / 5; i++)
{
pt = new Point();
pt.X = 5 * i;//5个像素绘制一个点
pt.Y = pictureBox3.Height - Convert.ToInt32(mcurrent) * this.pictureBox3.Height / 100;
ptlist[i] = pt;
} ptlist1 = new Point[this.pictureBox4.Width / 5];
Point pt1;
for (int i = 0; i < this.pictureBox4.Width / 5; i++)
{
pt1 = new Point();
pt1.X = 5 * i;//5个像素绘制一个点
pt1.Y = Convert.ToInt32(cpower) * this.pictureBox4.Height / 20000;
ptlist1[i] = pt1;
}
}
定时器中语句如下
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Interval = 100; DrawLines(Color.Yellow, pictureBox3, ptlist); //new add
DrawLines1(Color.Yellow, pictureBox4, ptlist1); }
开机按键增加 this.timer1.Interval = 100;
this.timer1.Enabled = true;后出现上述问题
public Bitmap mybitmap;
private void DrawLines(Color color, PictureBox PictureBox3, Point[] ptlist)//picturebox3
{
mybitmap = new Bitmap(PictureBox3.Width, PictureBox3.Height);
Graphics doublebufferg = Graphics.FromImage(mybitmap);
doublebufferg.Clear(Color.Black); Rectangle rect = new Rectangle(0, 0, PictureBox3.Width, PictureBox3.Height);
doublebufferg.FillRectangle(new SolidBrush(Color.Black), rect); Point temp = new Point();
for (int j = 0; j < PictureBox3.Width / 5 - 1; j++)
{
temp = ptlist[j + 1];
ptlist[j] = new Point(temp.X - 5, temp.Y);
} Point lastpt = new Point();
lastpt.X = PictureBox3.Width;
lastpt.Y = PictureBox3.Height - Convert.ToInt32(mcurrent) * PictureBox3.Height / 100;
ptlist[PictureBox3.Width / 5 - 1] = lastpt;
doublebufferg.DrawLines(new Pen(color, 1), ptlist); Graphics g = PictureBox3.CreateGraphics(); g.Clear(Color.Black);
g.DrawImage(mybitmap, 0, 0);
} public Bitmap mybitmap1;
private void DrawLines1(Color color, PictureBox PictureBox4, Point[] ptlist1)//picturebox4
{
mybitmap1 = new Bitmap(PictureBox4.Width, PictureBox4.Height);
Graphics doublebufferg = Graphics.FromImage(mybitmap1);
doublebufferg.Clear(Color.Black); Rectangle rect = new Rectangle(0, 0, PictureBox4.Width, PictureBox4.Height);
doublebufferg.FillRectangle(new SolidBrush(Color.Black), rect); Point temp1 = new Point();
for (int j = 0; j < PictureBox4.Width / 5 - 1; j++)
{
temp1 = ptlist1[j + 1];
ptlist1[j] = new Point(temp1.X - 5, temp1.Y);
} Point lastpt1 = new Point();
lastpt1.X = PictureBox4.Width;
lastpt1.Y = pictureBox4.Height - Convert.ToInt32(cpower) * PictureBox4.Height / 20000;
ptlist1[PictureBox4.Width / 5 - 1] = lastpt1;
doublebufferg.DrawLines(new Pen(color, 1), ptlist1); Graphics g = PictureBox4.CreateGraphics(); g.Clear(Color.Black);
g.DrawImage(mybitmap1, 0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false; //关闭线程间检查以解决线程间操作无效问题
if (!serialPort1.IsOpen)
serialPort1.Open(); //设置控件的样式和行为,以减少图像的闪烁
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.UpdateStyles(); //实例化数组
ptlist = new Point[this.pictureBox3.Width / 5];
Point pt;
for (int i = 0; i < this.pictureBox3.Width / 5; i++)
{
pt = new Point();
pt.X = 5 * i;//5个像素绘制一个点
pt.Y = pictureBox3.Height - Convert.ToInt32(mcurrent) * this.pictureBox3.Height / 100;
ptlist[i] = pt;
} ptlist1 = new Point[this.pictureBox4.Width / 5];
Point pt1;
for (int i = 0; i < this.pictureBox4.Width / 5; i++)
{
pt1 = new Point();
pt1.X = 5 * i;//5个像素绘制一个点
pt1.Y = Convert.ToInt32(cpower) * this.pictureBox4.Height / 20000;
ptlist1[i] = pt1;
}
}
定时器中语句如下
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Interval = 100; DrawLines(Color.Yellow, pictureBox3, ptlist); //new add
DrawLines1(Color.Yellow, pictureBox4, ptlist1); }
开机按键增加 this.timer1.Interval = 100;
this.timer1.Enabled = true;后出现上述问题
public Bitmap mybitmap;
public void DrawLines(Color color, PictureBox picbox3, Point[] ptlist)
{
mybitmap = new Bitmap(picbox3.Width, picbox3.Height);//设定位图大小
Graphics doublebufferg = Graphics.FromImage(mybitmap);//从位图上获取“画布”
doublebufferg.Clear(Color.Black);//用背景色刷新 Rectangle rect = new Rectangle(0, 0, picbox3.Width, picbox3.Height);
doublebufferg.FillRectangle(new SolidBrush(Color.Black), rect); //要显示的实时曲线部分
Point temp = new Point();
for (int j = 0; j < picbox3.Width / 5 - 1; j++)
{
temp = ptlist[j + 1];
ptlist[j] = new Point(temp.X - 5, temp.Y);
} Point lastpt = new Point();
lastpt.X = picbox3.Width;
lastpt.Y = mcurrent * picbox.Height /100;
ptlist[picbox3.Width / 5 - 1] = lastpt;
doublebufferg.DrawLines(new Pen(color, 1), ptlist); //将缓冲中的位图绘制到我们的窗体上
Graphics g = picbox3.CreateGraphics();//创建 PictureBox窗体的画布 g.Clear(Color.Black);
g.DrawImage(mybitmap, 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Interval = 100; DrawLines(Color.Yellow, pictureBox3, ptlist); //new add
DrawLines1(Color.Yellow, pictureBox4, ptlist1); int cc;
cc = SendBufferout[pu++]; if (cc > 20000)
{
this.timer1.Enabled = false;
Byte[] Txdata = { 0xff, 0x04, 0x31, 0xcc };
this.serialPort1.Write(Txdata, 0, 4);
}
else
{
SendBuffer[0] = TAG_CMD;
SendBuffer[1] = 0x07;
SendBuffer[2] = CMD_SET_PARAM_VALUE;
SendBuffer[3] = PARAM_TARGET_SPEED;
SendBuffer[4] = (byte)(cc & 0xff);
SendBuffer[5] = (byte)(cc >> 8);
SendBuffer[6] = (byte)(checksum(6));
this.serialPort1.Write(SendBuffer, 0, SendBuffer[1]);
}
}
现在问题是drawlines和下面的数组判断发送都要占用timer1_tick 要怎么样将它们分开呢