|
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
|
|
|
|
|
Here's the code
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.IO.Ports;<br />
using System.Windows.Forms;<br />
using System.Collections;<br />
using Microsoft.VisualBasic;<br />
<br />
<br />
namespace SerialPort16_5_2010<br />
{<br />
class ReviceDataFromSeriaLPort<br />
{<br />
#region Identifiers<br />
public SerialPort serialPort1 = new SerialPort();<br />
public TextBox txtDataRecived = new TextBox();<br />
<br />
private double range;<br />
private double azmouth;<br />
public ArrayList msgSequence = new ArrayList();<br />
#endregion<br />
<br />
#region Responsible for connect to serial port<br />
public void ConnectToSerialPort(string portname, Label lblMessage)<br />
{<br />
if (serialPort1.IsOpen)<br />
{<br />
serialPort1.Close();<br />
}<br />
try<br />
{<br />
<br />
serialPort1.PortName = portname;<br />
serialPort1.BaudRate = 9600;<br />
serialPort1.Parity = Parity.None;<br />
serialPort1.DataBits = 8;<br />
serialPort1.StopBits = StopBits.One;<br />
<br />
serialPort1.Open();<br />
<br />
lblMessage.Text = portname + " Connected";<br />
<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
#endregion<br />
<br />
#region Responsible for Disconnect to serial port<br />
public void DisconnectToSerialPort(string portname, Label lblMessage)<br />
{<br />
try<br />
{<br />
serialPort1.Close();<br />
lblMessage.Text = portname + " Disconnected";<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
<br />
}<br />
#endregion<br />
<br />
#region Delegate and subroutine to update the TextBox control<br />
public delegate void myDelegate();<br />
#endregion<br />
<br />
#region updateTextBox funtionality<br />
<br />
int bytesLength=100;<br />
public void updateTextBox()<br />
{<br />
try<br />
{<br />
bytesLength = serialPort1.BytesToRead;<br />
<br />
<br />
byte[] comBuffer = new byte[bytesLength];<br />
<br />
<br />
#region Read Byte by Byte and check it.<br />
<br />
for (int BytesCounter = 0; BytesCounter < bytesLength; BytesCounter++)<br />
{<br />
int inputByte = serialPort1.ReadByte();<br />
string ConvertByteToHex = Conversion.Hex(inputByte);<br />
comBuffer[BytesCounter] = (byte)inputByte;<br />
<br />
#region Check if First byte in stream of bytes == 99<br />
if (ConvertByteToHex == "99")<br />
{<br />
if (BytesCounter + 1 < bytesLength)<br />
{<br />
int messageLength = serialPort1.ReadByte();<br />
comBuffer[BytesCounter + 1] = (byte)messageLength;<br />
#region Check if messageLength > 4 .. Check 3 probarity of message.<br />
if (messageLength > 4)<br />
{<br />
#region Check if messageLength == 4<br />
if (messageLength == 4)<br />
{<br />
<br />
}<br />
#endregion<br />
<br />
#region Check if messageLength == 10<br />
else if (messageLength == 10)<br />
{<br />
if (BytesCounter + messageLength <= bytesLength)<br />
{<br />
for (int msgByteCounter = 2; msgByteCounter < messageLength; msgByteCounter++)<br />
{<br />
int tempRecivedByte = serialPort1.ReadByte();<br />
comBuffer[msgByteCounter + BytesCounter] = (byte)tempRecivedByte;<br />
if (msgByteCounter == messageLength - 1)<br />
{<br />
#region check if message end equal '0d'='13'<br />
if (tempRecivedByte == 13)<br />
{<br />
<br />
string range1 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 6]);
string range2 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 5]);
string contactRange = string.Concat(range2, range1);<br />
range = Convert.ToInt32(contactRange, 16);<br />
<br />
string azmouth1 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 4]);
string azmouth2 = Conversion.Hex(comBuffer[msgByteCounter + BytesCounter - 3]);
string contactazmouth = string.Concat(azmouth2, azmouth1);<br />
azmouth = Convert.ToInt32(contactazmouth, 16);<br />
<br />
}<br />
#endregion<br />
}<br />
}<br />
MessageContents msgcontents = new MessageContents(range, azmouth);<br />
msgSequence.Add(msgcontents);<br />
<br />
<br />
<br />
}
}<br />
}<br />
#endregion<br />
<br />
#region Check if messageLength == 12<br />
else if (messageLength == 12)<br />
{<br />
if (BytesCounter + messageLength <= bytesLength)<br />
{<br />
for (int msgByteCounter = 2; msgByteCounter < messageLength; msgByteCounter++)<br />
{<br />
int tempRecivedByte = serialPort1.ReadByte();<br />
comBuffer[msgByteCounter + BytesCounter] = (byte)tempRecivedByte;<br />
if (msgByteCounter == messageLength - 1)<br />
{<br />
if (tempRecivedByte == 13)<br />
{<br />
}<br />
}<br />
}<br />
}<br />
}<br />
#endregion<br />
<br />
if (messageLength + BytesCounter < comBuffer.Length)<br />
BytesCounter += messageLength - 1;<br />
}<br />
else<br />
{<br />
}<br />
<br />
#endregion<br />
}<br />
#endregion<br />
#region do not read if first bytes not equal 99<br />
else<br />
{<br />
<br />
}<br />
#endregion<br />
}<br />
#endregion<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
<br />
}<br />
}<br />
#endregion<br />
<br />
#region used to Recive Data In Run Time <br />
public void DataRecivedInRunTime()<br />
{<br />
<br />
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataRecived);<br />
<br />
}<br />
#endregion<br />
<br />
#region DataRecived Event<br />
public void DataRecived(object sender, SerialDataReceivedEventArgs e)<br />
{<br />
txtDataRecived.BeginInvoke(new myDelegate(updateTextBox));<br />
}<br />
#endregion<br />
}<br />
}<br />
<br />
and MessageContents class to set range and azimuth on it
<br />
class MessageContents<br />
{<br />
<br />
public double msgRang;<br />
public double azmouth;<br />
<br />
public MessageContents(double _msgRang,double _azmouth)<br />
{<br />
this.msgRang=_msgRang;<br />
this.azmouth = _azmouth;<br />
}<br />
}<br />
in the form
----------------------------
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Collections;<br />
using System.Drawing.Drawing2D;<br />
using System.Threading;<br />
<br />
namespace SerialPort16_5_2010<br />
{<br />
public partial class Form1 : Form<br />
{<br />
<br />
ReviceDataFromSeriaLPort rcvfrmSerial;<br />
ArrayList sequenceSore10 = new ArrayList();<br />
int x, y;<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
<br />
rcvfrmSerial = new ReviceDataFromSeriaLPort();<br />
rcvfrmSerial.ConnectToSerialPort("COM1", lblMessage);<br />
rcvfrmSerial.txtDataRecived = textBox1;<br />
rcvfrmSerial.DataRecivedInRunTime();<br />
}<br />
<br />
<br />
<br />
private void myCustomPanel1_Paint_1(object sender, PaintEventArgs e)<br />
{<br />
Graphics g = Graphics.FromHwnd(myCustomPanel1.Handle);<br />
Pen p = new Pen(new SolidBrush(Color.Red), 2.0f);<br />
<br />
g.TranslateTransform(400, 300);<br />
foreach (MessageContents item in rcvfrmSerial.msgSequence)<br />
{<br />
double range = item.msgRang / 100;<br />
double azimouth = item.azmouth;<br />
double range2 = item.msgRang + 1;<br />
x = (int)(range * Math.Cos(azimouth * Math.PI / 180));<br />
y = (int)(range * Math.Sin(azimouth * Math.PI / 180));<br />
g.DrawEllipse(p, (int)x, (int)y, 4, 4);<br />
this.Invalidate();<br />
this.Refresh();<br />
<br />
}<br />
}<br />
}<br />
}<br />
<br />
|
|
|
|
|
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]
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).
|
|
|
|
|
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()
|
|
|
|
|
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).
|
|
|
|
|
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
|
|
|
|
|
Hallo, I am looking for a tiny but great programm which merges pdf files. I used it some years ago for joining over 1000 of small pdf files into a big one. Unfortunately, I do not remember its name, but I remember it was written in C# and the source code was given as well. I cannot find it, nor any that great to do my job with many pdf files. Upon running it promted the user to make a few decisions using a simple GUI, almost a Message Box. I would very much apprexiate any help. Thanks.
|
|
|
|
|
This[^] may not be the exact answer you are looking for but it should help.
It's time for a new signature.
|
|
|
|
|
Not sure about the source code thing, but PDF metamorphosis is written in C#. Check if that is what you are looking for.
|
|
|
|
|
There are a couple of projects here on CP that looks like they might be helpful to you:
PDF Merge[^]
Simple .NET PDF Merger[^]
Good luck
BTW: Don't ask me how I got the links...
|
|
|
|
|
Hi all,
I need to get cookies which currently in use with one of my browsers (IE8), but IE doesn't log it into cookie text file.
I'm looking for cookies in "C:\Users\username\AppData\Roaming\Microsoft\Windows\Cookies" folder in username@hostname[i].txt file.
And in "C:\Users\username\AppData\Roaming\Microsoft\Windows\Cookies\Low" folder.
How I can do it?
Reading of non-persistent cookies is what I actualy need. Is there any way to do it?
thanks!
modified on Sunday, June 6, 2010 3:01 AM
|
|
|
|
|
This is what I have done to identify the vehicles from a given picture using Emgu CV (StructuringElementEx). Now I want to get the count of these identified vehicles.
If anyone know how to do that, it is a great help to me.
Thank You.
[img]http://i48.tinypic.com/2r2vza1.jpg[/img]
namespace ImagePrjct<br />
{<br />
public partial class Form1 : Form<br />
{<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
imageBox2.Image = new Image<Bgr, Byte>(@"C:\Users\Rukshan\Desktop\FYP_Update 2.1\New_Traffic_Pics\E4.jpg");<br />
}<br />
<br />
private void button1_Click(object sender, EventArgs e)<br />
{<br />
Image<Bgr, Byte> ori = new Image<Bgr, Byte> ( imageBox2.Image.Bitmap);<br />
Image<Gray, Byte> grayscale = ori.Convert<Gray, Byte>();<br />
imageBox1.Image = grayscale;<br />
}<br />
<br />
private void button2_Click(object sender, EventArgs e)<br />
{<br />
Image<Bgr, Byte> ori = new Image<Bgr, Byte> ( imageBox2.Image.Bitmap);<br />
Image<Gray, Byte> grayscale = ori.Convert<Gray, Byte>();<br />
Image<Gray, Byte> thresh = grayscale.ThresholdToZero(new Gray(210));
<br />
StructuringElementEx ex = new StructuringElementEx(8, 8, 1, 1, CV_ELEMENT_SHAPE.CV_SHAPE_RECT);<br />
<br />
thresh._MorphologyEx(ex, CV_MORPH_OP.CV_MOP_OPEN, 1);<br />
<br />
imageBox3.Image = thresh;<br />
}
|
|
|
|
|
I have a project that integrates RichTextBox tips from many articles across the web. Everything from XP Style borders, to WYSIWYG printing.
One thing missing is rendering of page breaks.
I can insert an RTF page break that is visible when loading the resulting rtf file into Word, or similar. However I can't find how to render it inside the RichTextBox.
I would like to render a nice dotted line at that point.
So far my searches have only turned up print rendering articles, which I already have.
Any suggestions or links to appropriate aticles would be welcome.
|
|
|
|
|
This is what I know about the matter, mostly from reading the RTF specification and from experimenting (I did quite some work on tables, perfectly readable in WordPad):
- there are hundreds of RTF commands;
- Word can create RTF documents, and will use a lot of the existing commands;
- WordPad can create RTF documents, and will use only few of the existing commands;
- WordPad can show a lot of RTF stuff it does not let you create interactively;
- RichTextBox shows RTF documents pretty much the same as WordPad does.
- RTF has a \page command
- WordPad shows it as an empty line
From that I would conclude:
- by default RTB hardly renders a page command
- you can of course add whatever text and formatting code you like whenever you also insert a page command; that is after all what Word does when it creates headers and footers for you.
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).
|
|
|
|
|
Thanks Luc
I am using the \page command and it works fine when printing or loading into Word, just no visual cue in the RichTextBox. I hadn't realised WordPad did nothing to indicate the page break. If Microsoft left it out of WordPad I don't feel so bad about not implementing it myself.
I guess the simplest solution will be for me to insert a horizontal line in place of any page breaks and replace them with real page breaks on save.
|
|
|
|
|
You're welcome.
The Man from U.N.C.L.E. wrote: insert a horizontal line ... and replace them
That sounds OK. Whatever you want to achieve, it probably can be done, but you'll have to manipulate the RTF string yourself.
FYI: as always, the document Word generates is overly complex; WordPad does a much better job, assuming it allows you to enter what you need. When working with tables, I looked at what Word does, read the RTF specs, and created it all in code, I didn't want hundreds of lines of initialization (and it gets worse in every new Office version).
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).
|
|
|
|
|
The structure of my listview is:
Select ID Name Age
--------------------------------------------------
Item[0]: checkbox 25 Richard 24
Item[1]: checkbox 26 Tom 20
Item[2]: checkbox 27 Alice 22
......many.....
Now, I want to get the name or age of current item when I make the item checked or unchecked. In other words, when I click a checkbox, I need to know the index of the item. If there is only one checked item, we can use "foreach" to get the index, but there are some already checked items.For example:
Item[0] and Item[1] are checked.
I click the checkbox of Item[2], How can I know what I clicked is Item[2]? (Here is not related to SelectedItem.)
Excuse me,My English is poor.
|
|
|
|
|
You need to handle the ItemCheck event of your ListView .
The event handler skeleton inserted for you by the IDE has an ItemCheckEventArgs which contains, amongst other things, the index of the checked item and its current state (checked/unchecked). You can use this information to decide what to do.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
I second that.
Are we on speed now? you actually beat me to it!
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).
|
|
|
|
|
Luc Pattyn wrote: Are we on speed now?
I do not imbibe noxious substances. Now you'll have to excuse me, I have a beer, a coffee and a cigarette waiting.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
So, no cacao/cocoa on Saturday?
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).
|
|
|
|
|
That's for just before beddie-byes.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
Any last order for cocoa? Now is the time.
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).
|
|
|
|
|
I can't have my cocoa until the nurse has bought me my tablets and she's late.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
I'm afraid she broke down, after having to bring you truck loads of beer, coffee and cigarettes all day long. Besides, the local pharmacies have long closed, and you shouldn't expect her to travel all the way to the other side of Wolverhampton, just to buy you some tablets. I suggest you have some more After Eights and call it a day. You can catch up on tablets on Monday.
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).
|
|
|
|
|