|
-
June 12th, 2010, 11:55 AM
#1
flickers with draw plots from serial port ?
I am read plots from serial port and draw it ....
I am make method to 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.
So 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 ....
Code:
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 {
//retrieve number of bytes in the buffer
bytesLength = serialPort1.BytesToRead;
//create a byte array to hold the awaiting data
byte[] comBuffer = new byte[bytesLength];
#region Read Byte by Byte and check it.
for (int BytesCounter = 0; BytesCounter < bytesLength; BytesCounter++)
{
int inputByte = serialPort1.ReadByte();
string ConvertByteToHex = Conversion.Hex(inputByte);
comBuffer[BytesCounter] = (byte)inputByte;
#region Check if First byte in stream of bytes == 99 if (ConvertByteToHex == "99")
{
// if there only one byte in message we will not process on it.
if (BytesCounter + 1 < bytesLength)
{
int messageLength = serialPort1.ReadByte();
comBuffer[BytesCounter + 1] = (byte)messageLength;
#region Check if messageLength > 4 .. Check 3 probarity of message.
if (messageLength > 4)
{
#region Check if messageLength == 4 if (messageLength == 4)
{
}
#endregion
#region Check if messageLength == 10 else if (messageLength == 10)
{
//to Check if message length which reads in second byte are like the exists in serial port.
if (BytesCounter + messageLength <= bytesLength)
{
for (int msgByteCounter = 2; msgByteCounter < messageLength; msgByteCounter++)
{
int tempRecivedByte = serialPort1.ReadByte();
comBuffer[msgByteCounter + BytesCounter] = (byte)tempRecivedByte;
if (msgByteCounter == messageLength - 1)
{
#region check if message end equal '0d'='13' if (tempRecivedByte == 13)
{
string range1 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 6]); //3
string range2 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 5]); //4
string contactRange = string.Concat(range2, range1);
range = Convert.ToInt32(contactRange, 16);
string azmouth1 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 4]); //5
string azmouth2 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 3]); //6
string contactazmouth = string.Concat(azmouth2, azmouth1);
azmouth = Convert.ToInt32(contactazmouth, 16);
}
#endregion }
}
//Add range and Azmouth to arrayList.
MessageContents msgcontents = new MessageContents(range, azmouth);
// msgcontents.MsgRang = range;
msgSequence.Add(msgcontents);
// msgcontents.Azmouth = azmouth;
//msgSequence.Add(msgcontents.Azmouth);
// Set the value of MsgSequence to be acessed when i want to draw.
// MsgSequence = msgSequence;
} // end of if satement Scope
}
}
#endregion
#region Check if messageLength == 12 else if (messageLength == 12)
{
//to Check if message length which reads in second byte are like the exists in serial port.
if (BytesCounter + messageLength <= bytesLength)
{
for (int msgByteCounter = 2; msgByteCounter < messageLength; msgByteCounter++)
{
int tempRecivedByte = serialPort1.ReadByte();
comBuffer[msgByteCounter + BytesCounter] = (byte)tempRecivedByte;
if (msgByteCounter == messageLength - 1)
{
//check if message end equal '0d'='13'
if (tempRecivedByte == 13)
{
}
}
}
}
}
#endregion
if (messageLength + BytesCounter < comBuffer.Length)
BytesCounter += messageLength - 1;
}
else {
// ignore message
}
#endregion }
#endregion // else
#region do not read if first bytes not equal 99 else {
}
#endregion }
#endregion }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region used to Recive Data In Run Time
public void DataRecivedInRunTime()
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataRecived);
}
#endregion
#region DataRecived Event
public void DataRecived(object sender, SerialDataReceivedEventArgs e)
{
//---invoke the delegate to retrieve the received data---
txtDataRecived.BeginInvoke(new myDelegate(updateTextBox));
}
#endregion }
}
and MessageContents class to set range and azimuth on it
class MessageContents
{
public double msgRang;
public double azmouth;
public MessageContents(double _msgRang,double _azmouth)
{
this.msgRang=_msgRang;
this.azmouth = _azmouth;
}
}
in the form
----------------------------
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.Collections;
using System.Drawing.Drawing2D;
using System.Threading;
namespace SerialPort16_5_2010
{
public partial class Form1 : Form
{
ReviceDataFromSeriaLPort rcvfrmSerial;
ArrayList sequenceSore10 = new ArrayList();
int x, y;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rcvfrmSerial = new ReviceDataFromSeriaLPort();
//Connect to my Serial Port
rcvfrmSerial.ConnectToSerialPort("COM1", lblMessage);
// Recive Data From Other Com
rcvfrmSerial.txtDataRecived = textBox1;
rcvfrmSerial.DataRecivedInRunTime();
}
private void myCustomPanel1_Paint_1(object sender, PaintEventArgs e)
{
Graphics g = Graphics.FromHwnd(myCustomPanel1.Handle);
Pen p = new Pen(new SolidBrush(Color.Red), 2.0f);
g.TranslateTransform(400, 300);
foreach (MessageContents item in rcvfrmSerial.msgSequence)
{
double range = item.msgRang / 100;
double azimouth = item.azmouth;
double range2 = item.msgRang + 1;
x = (int)(range * Math.Cos(azimouth * Math.PI / 180));
y = (int)(range * Math.Sin(azimouth * Math.PI / 180));
g.DrawEllipse(p, (int)x, (int)y, 4, 4);
//g.DrawLine(p, 0, 0, x, y);
this.Invalidate();
this.Refresh();
}
}
}
}
what is the best design for Application to smooth drawing ?
-
June 12th, 2010, 06:56 PM
#2
Re: flickers with draw plots from serial port ?
I did not make it through all of that code. Hard to read with no formatting but I would suspect that reading the data from the port 1 byte at a time is having an impact on your program speed. I would think it would be faster and smoother if you read all data available into an array and then if need be step through the array byte by byte for your plotting.
Always use [code][/code] tags when posting code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|