您正在查看 "默认分类" 分类下的文章
2007年07月12日 星期四 下午 04:58
现自我介绍下:
熟悉.NET,会用vs2005,目前正自学cb,
单片机会51,熟悉MSP430,
画电路图就不说了,基本上就这些。
团队目标:
网站,软件,和硬件方面,现在已有一位美工。接一些外包项目。
队员要求:
敬业,有业余时间交流,有职业素养,至少有一方面擅长,软硬件都可
我的邮箱:feiyu_868@163.com
QQ群:2501269
有意的话,介绍下自己,越详细越好,也许我们就是队友,
资料可以放到我的博客,邮箱 |
2007年05月10日 星期四 上午 02:12
Imports System.IO.Ports
Public Class form1
Dim WithEvents serialPort As New IO.Ports.SerialPort
Public buffer1(4) As Byte
Public buffer0(4) As Byte
'Public buffer2(4) As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
Timer1.Enabled = True
Timer1.Interval = 1000
' Timer3.Enabled = True
' Timer3.Interval = 10
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = ComboBox1.Text
.BaudRate = ComboBox2.Text
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.DtrEnable() = True
End With
serialPort.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
RichTextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
Dim buffer As String
serialPort.Read(buffer1, 1, 4)
buffer = Hex(buffer1(1)) + Hex(buffer1(2)) + Hex(buffer1(3)) + Hex(buffer1(4))
Label1.Text = buffer1(1)
Label2.Text = buffer1(2)
Label3.Text = buffer1(3)
Label4.Text = buffer1(4)
RichTextBox1.AppendText(buffer)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
buffer0(1) = &HFF
buffer0(2) = &HFE
buffer0(3) = &HFD
buffer0(4) = &HFC
Try
serialPort.Write(buffer0, 1, 4)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub DrawGuidelines(ByVal PicBox As PictureBox)
Dim bmp As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bmp)
For i As Integer = 20 To 600 Step 20
gr.DrawLine(Pens.WhiteSmoke, 0, i, PicBox.Width, i)
Next i
PicBox.Image = bmp
End Sub
Private Sub _1stDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
DrawGuidelines(Me.picGraph)
End Sub
Private OldValue As Single = 0
Private NewValue As Single = 0
Dim Xmove As Integer = 10
Private Sub DrawGuidelinesAndChart(ByVal PicBox As PictureBox)
Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
For i As Integer = 20 To 600 Step 20
gr.DrawLine(Pens.WhiteSmoke, PicBox.Width - Xmove, i, PicBox.Width, i)
Next i
gr.DrawImage(PicBox.Image, -Xmove, 0)
gr.TranslateTransform(0, picGraph.Height)
'NewValue = SBUserValue.Value * 4
gr.DrawLine(Pens.Red, picGraph.Width - 1 - Xmove, -OldValue, picGraph.Width - 1, -NewValue)
OldValue = NewValue
lblValue.Text = NewValue
picGraph.Image = bm
gr.Dispose()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
For j As Integer = 1 To 4
NewValue = buffer1(j) / 2
DrawGuidelinesAndChart(picGraph)
buffer1(j) = 0
Next j
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
serialPort.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End
End Sub
' Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
' buffer2(1) = Int(Rnd() * 100)
' buffer2(2) = Int(Rnd() * 100)
'buffer2(3) = Int(Rnd() * 100)
' buffer2(4) = Int(Rnd() * 100)
'buffer1(5) = Int(Rnd() * 100)
'End Sub
End Class
|
2007年05月04日 星期五 上午 00:54
Introduction
There are some quite advanced features that can be incorporated into a dynamic chart, sometimes depending on where the data is coming from. In this article, we are going to be begin with a fairly simple demo. Then we will move on to one that is slightly more sophisticated and finally we will look at further enhancements that could be incorporated into this kind of graph.
So, let me set the scene. What do we want to do here? Well, the first dynamic line chart I plan to create is one that takes some simple user input and displays the values as they are chosen on the chart. As each new value is selected, the chart will be extended and redrawn to show the continuous changes that have been made. When the screen width is "full", the earliest values are lost from view as the graphic display continues to scroll from right to left.
One quick and easy demo is to create put a scrollbar on a form and use the changing scrollbar values as the data for the chart. Take a look at the sample form below.

Form Controls
The silver grey area is a PictureBox. As the values in the ScrollBar change, so the graph represents the new values. I have used a Vertical ScrollBar here, but you can just as easily use a Horizontal ScrollBar. In either event, the coding logic will be the same.
Here are the steps to create the form.
- Add a ScrollBar control to the form. Set its Minimum property to 0, its Maximum to 104. (If you are wondering why I've picked that Maximum value, see the note at the bottom this page) . Set the LargeChange property to 5; leave the SmallChange property as 1. Change the control's name to SBUserValue to reflect its purpose - scroll bar to detect user's chosen values.
- Add a PictureBox and name it picGraph. Set its height to exactly 400. Why ? This is really just a way of keeping the scaling code as easy as possible. The user values are going to be numbers between 0 and 100, so it will be very easy to multiply the value by 4 to automatically scale it vertically on the picturebox graph. Real life almost certainly won't be this easy, but it will help us concentrate on the important drawing stuff for this first demonstration. Change the PictureBox'sname to picGraph and set its backcolor to Gainsborough (or any other color that you prefer)
- Finally,add a label to display the changing selection value. This label plays no part in the chart creation; I've included it so you can check that any given value is correctly represented on the chart itself. Name it lblValue.
- The remaining label controls shown on the screenshot above are entirely optional and are not referred to in any of the code that follows.
Initial Code
Place this Imports statement at the top of the form:
Imports
System.Drawing.Drawing2D
As you will know if you have read the earlier articles in this series, this allows us to use any of the Drawing.Drawing2D class methods and properties in code without having to keep writing out the fully qualified names each time.
In the next step we will build the code that does all the drawing in the chart, dynamically changing the display each time a new value is selected via the ScrollBar.
----------------------------------------------------------------------------------------------------------------
Note:
The reason for setting a Maximum value of 104 (when what we really want is 0 to 100 on the scale) is due to what seems to me to be a small foible with the ScrollBar control. If you set the Maximum to 100 and the Large Change to 5 then the highest value you will be able to physically obtain by moving the slider or clicking on the ScrollBar's arrow will be 96. There may be an official fix for this, but I find that offsetting the Maximum by a figure of (LargeChange - 1) seems to do the trick.
Basic Chart
Drawing the Chart Guidelines
The next step is to draw the horizontal guidelines on the chart. These are drawn as soon as the form is first loaded and (as you will see shortly) are redrawn each time the chart is updated with new values.
In this first example, we will use hard coded values in the procedure that draws the guidelines. This is only because it makes the code simpler and the explanations shorter. Later in the article we will use a more flexible, modular and more realistic approach .
In the Code Window for the form, add the following procedure:
Private Sub DrawGuidelines(ByVal PicBox As PictureBox)
' Create a bitmap to draw on and grab its Graphics Object
Dim bmp As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bmp)
' Draw guidelines on this graphics object .
For i As Integer = 40 To 400 Step 40
gr.DrawLine(Pens.WhiteSmoke, 0, i, PicBox.Width, i)
Next i
' Assign the bitmap back to the PictureBox as its Image.
PicBox.Image = bmp
End Sub
How It Works
Let's analyse what this code actually does. (If you have read the previous articles this will be familiar to you, but if you have jumped in here at Part 6 some explanation of what is going on will be useful, I'm sure).
The first two lines of code in the procedure:
Dim bmp As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bmp)
can be broken down as follows:
-
-
-
-
Dim bmp As New Bitmap: Creates a Bitmap object.
(PicBox.Width, PicBox.Height): Sets the size of this Bitmap to be the same size as the PictureBox on which we will draw the graph.
Dim gr As Graphics: Creates a Graphics Object.
(A Graphics object can be thought of as a kind of "canvas" that is laid over a control and onto which lines and shapes can be drawn).
= Graphics.FromImage(bmp): Specifically assign the Graphics object from the Bitmap that we have just created. We will draw the lines on this Graphics object
We've made a start. Let's check it out.
In the Form's Load event insert the following code which will call the above procedure and draw ten evenly spaced lines across the PictureBox:
Private Sub _1stDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Draw horizontal lines on the picturebox
DrawGuidelines(Me.picGraph)
End Sub
Try It Out
Try out the code so far. You should see something like this:
Charting the Values
The next part of the code is the key to the dynamism of the chart. Whenever the user changes the recorded value by scrolling the ScrollBar, here's what we will do:
We need to set up a few variables. The first one will represent the length of each segment of the graph - that is, the number of pixels used horizontally on the screen as each new part of the graph is added. Add the code below to the top of the form:
Because we are going to be drawing the graph of an ever changing set of values, we will need to be able to refer to the last used value as well as the current one. The two variables shown below will be used for this task:
Now for the actual procedure which produces the goods. It is named DrawGuidelinesAndChart:
Private Sub DrawGuidelinesAndChart(ByVal PicBox As PictureBox)
' Step 1
' Grab the current image (the latest version of the chart)
Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm) ' Step 2
' Tack missing guidelines to right hand side on the Graphics object.
For i As Integer = 40 To 400 Step 40
gr.DrawLine(Pens.WhiteSmoke, PicBox.Width - Xmove, i, PicBox.Width, i)
Next i
' Step 3
' Draw this grabbed image, placing it Xmove pixels to the left of the
' image area. (that is, the first few pixels will be invisible because
' they are now outside the picturebox's viewable area)
gr.DrawImage(PicBox.Image, -Xmove, 0)
' Note that we are still drawing on the Graphics object.
' No change will appear on the PictureBox yet.
' Step 4
' Plot the new value. That is, calculate where on the Y axis (the
' vertical plane) this new value should be marked.
' This calculation takes into account the scale of the min/max values
' range in relation to the total height of the PictureBox.
' In this example this is 4:1 (PictureBox Height is 400:Max value on the ScrollBar is 100)
' so we multiply the selected value by a scale of 4
NewValue = SBUserValue.Value * 4 ' Step 5
' Shift start point from top left to bottom left.
gr.TranslateTransform(0, picGraph.Height)
' Step 6
' Draw the next line segment on the Graphics object.
' Note that the NEGATIVE values of OldValue and NewValue
' are used for the Y positions.
' This will reverse the position of those points and make them
' appear in the PictureBox. If you don't do this, they will be
' drawn down below the bottom of the PictureBox and therefore
' out of view of the user.
gr.DrawLine(Pens.Black, _
picGraph.Width - 1 - Xmove, -OldValue, _
picGraph.Width - 1, -NewValue)
OldValue = NewValue
' Step 7
' Display the results by assigning this edited Bitmap as the
' Image of the PictureBox.
picGraph.Image = bm ' Step 8
' All done
gr.Dispose() ' For confirmation purposes, display the currently selected value in a label
lblValue.Text = SBUserValue.Value.ToString
End Sub
Using the Vertical ScrollBar
As mentioned earlier, the vertical ScrollBar used in this demonstration is just an easy way to generate a set of fast-changing values to show on the graph. What we need to do now is write some code that kicks in each time the value of the ScrollBar is changed by the user.
The place to do this in the ScrollBar's ValueChanged event.
Private Sub SBUserValue_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SBUserValue.ValueChanged
DrawGuidelinesAndChart(picGraph)
End Sub
Test It
Now run the project and move the ScrollBar. Each movement of the ScrollBar should result in a new value being plotted on the chart. You can check what value was last selected by viewing the value shown in the label.
As you'll see, the Vertical ScrollBar creates higher values as it is moved downwards; lower values as it is moved upwards. If you find this confusing, you can easily replace the Vertical ScrollBar with a Horizontal one. In that case, lower values are selected as you scroll to the left and higher values to the right, which you may think is more logical.

First Demonstration Summary
With relatively little code, we have created a useful dynamic line chart. You can replace the ScrollBar that I have used with any other means of taking values that would be relevant for your projects, whether this be user input, system produced information, external data, time based data, and so on.
Of course, you can't always guarantee that the values used will fall neatly into the 1 to 100 range we used for this first example, so we will shortly look at how we can edit the code to make it more flexible.
First though, I want to look at a couple of ways of producing some interesting alternative displays.
Private OldValue As Single = 0
Private NewValue As Single = 0
Dim Xmove As Integer = 3
- Take a 'snapshot' of the previous display by grabbing the current image from the PictureBox in which we are creating the chart.
- Extend the guidelines at the right hand side of the drawing. (You'll see why in a minute).
- 'Paste' the snapshot of the previous display back on to the Bitmap's Graphics Object, BUT place it slightly to the left so that some of this snapshot falls outside the viewable area of the PictureBox.
(There would an empty gap at the right hand side now if we hadn't extended the guidelines in the last step).
- Calculate the point on the Y (vertical axis) where the currently selected value should be marked.
Note that we have to scale this to make full use of the available height of the PictureBox. In our current example, where we have carefully set the PictureBox height to 400 and the ScrollBar maximum to 100, we therefore have a scale of 4:1 (that is, each increment of 1 in the ScrollBar value is represented by 4 vertical pixels in the PictureBox.)
- Move the start point from the default position at the Top Left of the PictureBox, so that the drawing will now start at the Bottom Left corner of the PictureBox.
The key thing to understand is that when you draw lines in a PictureBox, the point of origin - the Point with values (0,0)- is always in the top left hand corner.
Then, when you draw a line from that point - to Point (50, 100) for example - then the pen will move 50 pixels across and 100 pixels down.
This is fine for most drawing tasks, but in a chart we expect the line to move upwards as values increase, not downwards. Therefore we want our chart line to begin in the bottom left corner to represent a value of zero (Point 0,0) and increasing values to be represented by an upward moving line.
- Draw the next line segment. This line will extend from the point where the last value plotted ended to the point required to represent the current value.
Remembering what was said above about lines being drawn from top to bottom, we also have to flip the line so that it becomes drawn from bottom to top (otherwise it would be drawn in an area out of view).
In this example, this is achieved very simply by plotting the negative equivalents of the values.
- All the drawing tasks for the latest value are now complete, so we can assign our freshly redrawn bitmap as the Image property of the PictureBox.
This will update the display and the user will see the results of our actions above.
- Finally, good housekeeping - we dispose of the Graphics object as we have finished with it for this cycle of actions.
The following code will do the job:-
Basic Chart Special Effects
Shaded Graph
If you want a chart that looks like this:

then you can achieve this effect with a minor code tweak. Replace Step 6 in the DrawGuidelinesAndChart procedure with the code block below:
' Step 6
' Draw the next line segment on the Graphics object.
gr.DrawLine(Pens.Black, _
picGraph.Width - 1 - Xmove, -OldValue, _
picGraph.Width - 1, -NewValue)
OldValue = NewValue
' Draw another line segment, but this time leave the Y value of
' OldValue as positive.
gr.DrawLine(Pens.LightBlue, _
(picGraph.Width - 1) - Xmove, OldValue, _
picGraph.Width - 1, -NewValue)
OldValue = NewValue
As you can see, the only change is a second sub block which uses Drawline again to draw a second line segment on the Graphics object. The difference here is that the start point of this segment is the positive value of OldValue, which you will recall means that it is located down below the bottom of the PictureBox. So drawing this second line has the effect of creating a line that runs from the bottom of the PictureBox up until it meets the point where we plotted the negative value of NewValue.
Sounds complicated at first, I know, but if you step through it a line at a time I hope it will make sense.
摘自:http://iamben250.programfan.com
|
2007年04月27日 星期五 下午 12:09
等我把软件做出来后就共享了,也说说我的心的!  |
2007年04月26日 星期四 下午 12:54
2007年04月23日 星期一 下午 06:42
2007年04月23日 星期一 下午 02:07
这几天一直用vb2005做个关于serialport 的小软件,查了好几天了,也是对其一知半解,想深入了,却怎么也找不到,msdn上的资料如嚼干蜡,这些资料想再深入点噶然而止,真想找个高人帮帮忙! |
2007年04月22日 星期日 下午 02:46
|
| |