简体   繁体   English

串行端口数据和C#中的表单刷新

[英]Serial Port Data and form refresh in c#

I want to draw rectangle. 我想画一个矩形。 Its width and height get from serial port. 它的宽度和高度来自串行端口。 And I want to refresh page for repainting when i get data. 而且我想刷新页面以在获取数据时重画。 my code is here; 我的代码在这里;

public partial class Form1 : Form
{
    string RxString;
    static int x = 1;
    SerialPort serialPort1 = new SerialPort();
    public Form1()
    {
        InitializeComponent();


    }

    private void Form1_Load(object sender, EventArgs e)
    {

        serialPort1.PortName = "COM3";
        serialPort1.BaudRate = 9600;

        serialPort1.Open();
        RxString = serialPort1.ReadLine();

        x = Convert.ToInt32(RxString);
        Graphics g = this.CreateGraphics();
        Pen pen = new Pen(Color.Black, 2);

        g.DrawRectangle(pen, 100, 100, x, x);

   }

But there is a error at line of (x = Convert.ToInt32(RxString);) 但是(x = Convert.ToInt32(RxString);)行有错误

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生类型为'System.FormatException'的第一次机会异常

What should I do? 我该怎么办? after that how can i refresh the form page for repaint image? 之后,我如何刷新表单页面以重绘图像?

Since your code example isn't complete, and you don't say what the string value that fails is, I can't answer the question about your exception. 由于您的代码示例不完整,并且您没有说失败的字符串值是什么,因此我无法回答有关您的异常的问题。 However, I can show you how to deal with the painting correctly. 但是,我可以向您展示如何正确处理绘画。

You need to maintain data in your class that keeps track of what you want to draw. 您需要维护班级中的数据,以跟踪要绘制的内容。 Then, you override the OnPaint() method (because here, you are trying to the object of the declaring class itself…in other scenarios, you might need to handle some other object's Paint event) where you draw, based on the data you got. 然后,您重写OnPaint()方法(因为在这里,您正在尝试声明类本身的对象……在其他情况下,您可能需要处理其他对象的Paint事件),根据获取的数据进行绘制。

Any time that data changes, you call Invalidate() to signal the need to redraw. 每当数据更改时,您都调用Invalidate()来表示需要重绘。

For example: 例如:

public partial class Form1 : Form
{
    string RxString;
    int x = 1;
    SerialPort serialPort1 = new SerialPort();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        serialPort1.PortName = "COM3";
        serialPort1.BaudRate = 9600;

        serialPort1.Open();

        // The next three lines of code, you'll do wherever you want
        // to receive data and update the displayed graphics. Naturally,
        // the call to serialPort1.ReadLine() might vary, depending on
        // how you are managing the I/O on the serial port.
        RxString = serialPort1.ReadLine();

        x = Convert.ToInt32(RxString);
        Invalidate();
   }

   protected override void OnPaint(PaintEventArgs e)
   {
        e.Graphics.DrawRectangle(Pens.Black, 100, 100, x, x);
   }
}

I made some other changes to your code to improve it: 我对您的代码进行了其他更改以改进它:

  1. The x member should not be static, as the rest of the code is also not static, and so you in theory could have multiple instances of this form, all trying to use the same x at the same time. x成员不应该是静态的,因为代码的其余部分也不是静态的,因此理论上您可以拥有这种形式的多个实例,所有实例都试图同时使用相同的x
  2. Use the stock Pens.Black object instead of creating one yourself. 使用库存的Pens.Black对象代替自己创建一个。

Also note that your original code failed to dispose the Graphics instance it created, which was a serious bug. 还要注意,您的原始代码无法处理其创建的Graphics实例,这是一个严重的错误。 It becomes moot in the above, because you don't actually need to create a Graphics instance. 在上面它变得毫无意义,因为您实际上不需要创建Graphics实例。 You just draw to the one passed to you (and don't dispose it…it's the caller's object, not yours). 您只需要吸引传递给您的对象即可(不要处理它……这是呼叫者的对象,而不是您的对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM