Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. flickers with draw plots from serial port ?

flickers with draw plots from serial port ?

Scheduled Pinned Locked Moved C#
questiongraphicsdesignhelp
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Honeyboy_20
    wrote on last edited by
    #1

    I am read plots from serial port and draw it .... I have problem with flickers ...and use DoubleBuffer property to solve the problem ... but flickers increase rather than the first .... what i should to solve problem .... what is the best design for Application to smooth drawing ?

    L 1 Reply Last reply
    0
    • H Honeyboy_20

      I am read plots from serial port and draw it .... I have problem with flickers ...and use DoubleBuffer property to solve the problem ... but flickers increase rather than the first .... what i should to solve problem .... what is the best design for Application to smooth drawing ?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      flickering typically means your paint job takes too long (say more than 10 msec), so it becomes visible to the human eye. Depending on what you have done several steps can be taken to improve it, maybe by a lot. Please explain more, and/or show the relevant code or pseudo-code. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      H 1 Reply Last reply
      0
      • L Luc Pattyn

        flickering typically means your paint job takes too long (say more than 10 msec), so it becomes visible to the human eye. Depending on what you have done several steps can be taken to improve it, maybe by a lot. Please explain more, and/or show the relevant code or pseudo-code. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        H Offline
        H Offline
        Honeyboy_20
        wrote on last edited by
        #3

        I am make method read data from serial port and check plot if it valid or not regarding as some specifications ... if it valid i catch it and store message in arraylist and then read from arraylist to draw plots.

        L RaviBeeR 2 Replies Last reply
        0
        • H Honeyboy_20

          I am make method read data from serial port and check plot if it valid or not regarding as some specifications ... if it valid i catch it and store message in arraylist and then read from arraylist to draw plots.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          That is completely insufficient to judge whether you will have a painting problem or not. There are a few categories: 1. Throughput How many bytes do you need from your serial port to complete a drawing? what is the baud rate? is the peripheral sending full throttle, or does it need extra time to generate the data? 2. Plot What do you need for plotting? is it some library that takes data points and generates an image, which you then paint? which library? how many data points? how big is the bitmap? 3. Organization How do you structure this? Are you using the DataReceived event? if so, how and when do you transfer control to the GUI thread for painting. Are you painting in the Paint handler? Feel free to provide extra information if you think it may be relevant. PS: take your time, I'm off now. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          H 1 Reply Last reply
          0
          • H Honeyboy_20

            I am make method read data from serial port and check plot if it valid or not regarding as some specifications ... if it valid i catch it and store message in arraylist and then read from arraylist to draw plots.

            RaviBeeR Offline
            RaviBeeR Offline
            RaviBee
            wrote on last edited by
            #5

            Honeyboy_20 wrote:

            if it valid i catch it and store message in arraylist and then read from arraylist to draw plots.

            This leads me to believe that your data read and drawing logic may be too closely coupled. Ideally they should be completely separate, executing in different threads. When valid data is received, the data reader should fire an event (subscribed to by the UI) that causes the UI to simply invalidate its canvas, which in turn causes it to grab a copy of the data which it uses for purposes of rendering. You'll need to synchronize access in the grab operation. Hope this helps. /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            H 1 Reply Last reply
            0
            • RaviBeeR RaviBee

              Honeyboy_20 wrote:

              if it valid i catch it and store message in arraylist and then read from arraylist to draw plots.

              This leads me to believe that your data read and drawing logic may be too closely coupled. Ideally they should be completely separate, executing in different threads. When valid data is received, the data reader should fire an event (subscribed to by the UI) that causes the UI to simply invalidate its canvas, which in turn causes it to grab a copy of the data which it uses for purposes of rendering. You'll need to synchronize access in the grab operation. Hope this helps. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              H Offline
              H Offline
              Honeyboy_20
              wrote on last edited by
              #6

              Here's the code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Forms; using System.Collections; using Microsoft.VisualBasic; namespace SerialPort16_5_2010 { class ReviceDataFromSeriaLPort { #region Identifiers public SerialPort serialPort1 = new SerialPort(); public TextBox txtDataRecived = new TextBox(); // To Store Message Plot as an Object from Serial and set it to MessageContents Class private double range; private double azmouth; public ArrayList msgSequence = new ArrayList(); #endregion #region Responsible for connect to serial port public void ConnectToSerialPort(string portname, Label lblMessage) { if (serialPort1.IsOpen) { serialPort1.Close(); } try { // Configure the various paramter of Serial Port serialPort1.PortName = portname; serialPort1.BaudRate = 9600; serialPort1.Parity = Parity.None; serialPort1.DataBits = 8; serialPort1.StopBits = StopBits.One; // Open Serial serialPort1.Open(); // Update Status of Serial Port lblMessage.Text = portname + " Connected"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region Responsible for Disconnect to serial port public void DisconnectToSerialPort(string portname, Label lblMessage) { try { serialPort1.Close(); lblMessage.Text = portname + " Disconnected"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region Delegate and subroutine to update the TextBox control //---Delegate and subroutine to update the TextBox control--- public delegate void myDelegate(); #endregion #region updateTextBox funtionality /* Read bytes by bytes * Check Message valid or invalid * Store bytes in array List */ int bytesLength=100; public void updateTextBox() { try {

              L 1 Reply Last reply
              0
              • L Luc Pattyn

                That is completely insufficient to judge whether you will have a painting problem or not. There are a few categories: 1. Throughput How many bytes do you need from your serial port to complete a drawing? what is the baud rate? is the peripheral sending full throttle, or does it need extra time to generate the data? 2. Plot What do you need for plotting? is it some library that takes data points and generates an image, which you then paint? which library? how many data points? how big is the bitmap? 3. Organization How do you structure this? Are you using the DataReceived event? if so, how and when do you transfer control to the GUI thread for painting. Are you painting in the Paint handler? Feel free to provide extra information if you think it may be relevant. PS: take your time, I'm off now. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                H Offline
                H Offline
                Honeyboy_20
                wrote on last edited by
                #7

                I am paste the code ... can you find the problem and help me ? I Hope

                1 Reply Last reply
                0
                • H Honeyboy_20

                  Here's the code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Forms; using System.Collections; using Microsoft.VisualBasic; namespace SerialPort16_5_2010 { class ReviceDataFromSeriaLPort { #region Identifiers public SerialPort serialPort1 = new SerialPort(); public TextBox txtDataRecived = new TextBox(); // To Store Message Plot as an Object from Serial and set it to MessageContents Class private double range; private double azmouth; public ArrayList msgSequence = new ArrayList(); #endregion #region Responsible for connect to serial port public void ConnectToSerialPort(string portname, Label lblMessage) { if (serialPort1.IsOpen) { serialPort1.Close(); } try { // Configure the various paramter of Serial Port serialPort1.PortName = portname; serialPort1.BaudRate = 9600; serialPort1.Parity = Parity.None; serialPort1.DataBits = 8; serialPort1.StopBits = StopBits.One; // Open Serial serialPort1.Open(); // Update Status of Serial Port lblMessage.Text = portname + " Connected"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region Responsible for Disconnect to serial port public void DisconnectToSerialPort(string portname, Label lblMessage) { try { serialPort1.Close(); lblMessage.Text = portname + " Disconnected"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region Delegate and subroutine to update the TextBox control //---Delegate and subroutine to update the TextBox control--- public delegate void myDelegate(); #endregion #region updateTextBox funtionality /* Read bytes by bytes * Check Message valid or invalid * Store bytes in array List */ int bytesLength=100; public void updateTextBox() { try {

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, I've been trying to read some of that code, which you made very hard by not using PRE tags. PRE tags exist for preserving formatting, mainly indentation, which facilitates seeing the structure of it all. So whatever you do in future, use PRE tags. You could even still edit your existing message and put PRE tags around its code. The code itself looks horrible too. Here are some comments, the list is not complete: 1. using Exception.Message rather than Exception.ToString() is stupid, as the latter offers much more information, making debugging a lot easier. 2. You have a DataReceived event handler (fine), but all it does is use Invoke to force a delegate to execute on the GUI thread, so it actually is your GUI thread that will read all bytes from the serial port and interpret them. That is not smart, as it has no relation with the GUI at all. You should deal with the serial data in a handler (or a separate thread) and only invoke towards the GUI thread at the very moment you want to update the GUI. 3. you read all serial port data one byte at a time; why not use SerialPort.Read(byte[] buffer, int offset, int count) to read all available data at once, then process it? 4. you have lots of unnecessary conversions, from bytes to strings to ints and back. As your data is binary (I assume so, since you are reading bytes right now), why not always deal with it as bytes; and possibly once turning it into a string for displaying it if necessary. 5. Get rid of those silly Conversion.Hex calls; they reek like Basic. If at all necessary, simply use byte.ToString("X2") 6. I skipped some code, then saw a Paint method, with lots of mistakes. A. use the Graphics you get from PaintEventArgs, don't create your own B. don't call Invalidate() or Refresh() in a Paint handler. It is terribly wrong. C. Avoid calling methods such as Math.Cos in a paint handler; you should have done that calculation beforehand and store it in your objects. D. Dispose of the garbage you create; e.g. the pen Conclusion: it is an abominable mess; I don't want to have another look at it, it should be binned and started all over. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] <

                  H 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, I've been trying to read some of that code, which you made very hard by not using PRE tags. PRE tags exist for preserving formatting, mainly indentation, which facilitates seeing the structure of it all. So whatever you do in future, use PRE tags. You could even still edit your existing message and put PRE tags around its code. The code itself looks horrible too. Here are some comments, the list is not complete: 1. using Exception.Message rather than Exception.ToString() is stupid, as the latter offers much more information, making debugging a lot easier. 2. You have a DataReceived event handler (fine), but all it does is use Invoke to force a delegate to execute on the GUI thread, so it actually is your GUI thread that will read all bytes from the serial port and interpret them. That is not smart, as it has no relation with the GUI at all. You should deal with the serial data in a handler (or a separate thread) and only invoke towards the GUI thread at the very moment you want to update the GUI. 3. you read all serial port data one byte at a time; why not use SerialPort.Read(byte[] buffer, int offset, int count) to read all available data at once, then process it? 4. you have lots of unnecessary conversions, from bytes to strings to ints and back. As your data is binary (I assume so, since you are reading bytes right now), why not always deal with it as bytes; and possibly once turning it into a string for displaying it if necessary. 5. Get rid of those silly Conversion.Hex calls; they reek like Basic. If at all necessary, simply use byte.ToString("X2") 6. I skipped some code, then saw a Paint method, with lots of mistakes. A. use the Graphics you get from PaintEventArgs, don't create your own B. don't call Invalidate() or Refresh() in a Paint handler. It is terribly wrong. C. Avoid calling methods such as Math.Cos in a paint handler; you should have done that calculation beforehand and store it in your objects. D. Dispose of the garbage you create; e.g. the pen Conclusion: it is an abominable mess; I don't want to have another look at it, it should be binned and started all over. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] <

                    H Offline
                    H Offline
                    Honeyboy_20
                    wrote on last edited by
                    #9

                    I have some comment on your reply .... First, you say : you read all serial port data one byte at a time; why not use SerialPort.Read(byte[] buffer, int offset, int count) to read all available data at once, then process it? I am check message from stream of bytes so I don't need to read all bytes then check it so I read byte by byte. Second , I need to draw each plot once I catch valid message and update it on the screen ... what is the alternative way to use anything rather than refresh()

                    L 1 Reply Last reply
                    0
                    • H Honeyboy_20

                      I have some comment on your reply .... First, you say : you read all serial port data one byte at a time; why not use SerialPort.Read(byte[] buffer, int offset, int count) to read all available data at once, then process it? I am check message from stream of bytes so I don't need to read all bytes then check it so I read byte by byte. Second , I need to draw each plot once I catch valid message and update it on the screen ... what is the alternative way to use anything rather than refresh()

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      1. I expect 10 ReadByte() to take longer than a single Read() reading 100 bytes. 2. Invalidate() and Refresh() don't belong inside a Paint handler; a Paint handler should paint, be swift and not cause other things to happen. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        1. I expect 10 ReadByte() to take longer than a single Read() reading 100 bytes. 2. Invalidate() and Refresh() don't belong inside a Paint handler; a Paint handler should paint, be swift and not cause other things to happen. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        A Offline
                        A Offline
                        Ahmed R El Bohoty
                        wrote on last edited by
                        #11

                        use onPaint event and it will solve your problem.

                        I hope to serve my religion (Islam) as much as possible and exert the effort in order to be a good model desire to achieve renaissance to our country. This mean that I would be one of the Geniuses in practical if Allah wishes. I think my Ambition will help me to do that. -------------------------------- Discover Other .... http://www.islamHouse.com

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups