1 Attachment(s)
[RESOLVED] Getting voltage from RS232 & allow dial voltmeter to point according to voltage level
Hello. I need some guidance regarding the following:
Basically, my circuit board is connected to a power supply(those laboratory DC power supply used in schools) and the circuit is connected to the computer via a RS232 Serial cable port.
Thru the serial cable port, a voltage level is sensed and is displayed on the "Voltage Level in Volts(V)" -- See attach image
With the help and suggestions from people in VB forum, i am able to display a Dial Voltmeter with a needle. -- Coding for the dial voltmeter with needle is also attached for reference, just in case if it is needed.
>> But currently, the dial voltmeter's needle does not point according to what is shown on the textbox.
>> Therefore, i need guidance on how to allow the dial voltmeter's needle to point according to what is shown on the textbox.
If any of the above is unclear, please inform me.
Thanks in advance. :)
[[ Dial Voltmeter with needle ]]
Code:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
'define a square to contain the dial (assumed: picturebox1.width>height)
Dim square As New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Width)
'centre the pie slice vertically (the pie slice is drawn centred in the square)
Dim verticalMargin As Integer = (PictureBox1.Width - PictureBox1.Height) \ 2
e.Graphics.TranslateTransform(0, verticalMargin)
'draw the pie slice
Dim startAngle As Single = -dialAngle / 2 - 90
e.Graphics.DrawPie(Pens.Black, square, startAngle, dialAngle)
e.Graphics.FillPie(Brushes.White, square, startAngle, dialAngle)
e.Graphics.ResetTransform()
'Calculate the needle angle
Dim needleAngle As Single = CSng(volts * (dialAngle / 2) / maximumVolts)
'Draw the dial graduations and numbering
Dim font As New Font(Me.Font.FontFamily, 11, FontStyle.Regular)
Dim pivot As New Point(PictureBox1.Width \ 2, PictureBox1.Height \ 2 + 2 * verticalMargin - 10)
Dim innerPoint1 As New Point(PictureBox1.Width \ 2, verticalMargin + 25)
Dim innerPoint2 As New Point(PictureBox1.Width \ 2, verticalMargin + 20)
Dim outerPoint As New Point(PictureBox1.Width \ 2, verticalMargin + 5)
For mark As Integer = -50 To 50
Dim markAngle As Single = dialAngle * mark / 100
Using mtx As New Drawing2D.Matrix
mtx.RotateAt(markAngle, pivot)
e.Graphics.Transform = mtx
End Using
If mark Mod 10 = 0 Then
'draw long line
e.Graphics.DrawLine(Pens.Gray, innerPoint1, outerPoint)
'draw numeral
e.Graphics.DrawString(mark / 10.ToString, font, _
Brushes.Black, innerPoint1.X - 5, innerPoint1.Y + 1)
Else
'draw short line
e.Graphics.DrawLine(Pens.Gray, innerPoint2, outerPoint)
End If
e.Graphics.ResetTransform()
Next
'Add label text
font = New Font(Me.Font.FontFamily, 10, FontStyle.Regular)
e.Graphics.DrawString("Volts", font, _
Brushes.Black, innerPoint2.X - 23, innerPoint2.Y + 35)
font.Dispose()
Dim tip As New Point(PictureBox1.Width \ 2, verticalMargin + 10)
Using pn As New Pen(Color.Red, 2)
pn.StartCap = Drawing2D.LineCap.RoundAnchor
Using mtx As New Drawing2D.Matrix
mtx.RotateAt(needleAngle, pivot)
e.Graphics.Transform = mtx
End Using
e.Graphics.DrawLine(pn, pivot, tip)
End Using
End Sub
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
Hi Catherine,
The only line that matters in what you posted is this one:
Code:
Dim needleAngle As Single = CSng(volts * (dialAngle / 2) / maximumVolts)
dialAngle and maximumVolts are constants, so the question boils down to this: how do you get the value for the variable volts from the RS232 port?
Apart from that it's a matter of regular coding and graphics. I expect I can help you if there are still problems in that area.
all the best, BB
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
just a thought, are you refreshing the paint event?
in the textbox change event (or some suitable place in the code) call picturebox1.refresh()
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
@boops boops
I just realized, I may have missed out something again ! =x
I've forgotten to mention that my circuit board is also connected to a small solar panel, and so the voltage level displayed in the textbox is the voltage being sensed from the solar panel. [hope you understand what i mean :) ]
Regarding the question " how do you get the value for the variable volts from the RS232 port? ". Does the above help to answer that ? Because i dont really understand what you mean. (maybe i'm poor in understanding :blush::blush:) But i hope it helps. =)
@Megalith
If i'm not wrong, yes , i'm refreshing the paint event. =)
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
try this
Code:
volts= testbox.text
Dim needleAngle As Single = CSng(volts * (dialAngle / 2) / maximumVolts)
instead of testbox.text, give the name of the text box where the voltage is displayed
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
Quote:
Originally Posted by
catherine0136
I just realized, I may have missed out something again ! =x
I've forgotten to mention that my circuit board is also connected to a small solar panel, and so the voltage level displayed in the textbox is the voltage being sensed from the solar panel. [hope you understand what i mean :)
Maybe I don't understand what you mean. Are you already getting readings from the solar panel appearing in the textbox? Or do you mean that's what you want to happen? BB
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
@BB
Yes, i am getting the readings from the solar panel and the voltage will be appearing in the textbox, it is not what i want to happen.
Therefore, i need to allow the voltage needle to point according to the voltage level shown in the textbox.
Hope my explanation is clearer now. =)
@makdu
Thanks for your suggestion. Will try it out when i get back to the lab. =)
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
Catherine, try with this if i understand what U wannt
VB.Net Code:
'1. On top of your code outside all methods put global variable
Dim needleAngle As Double = 0
'2. In TextBox TextChanged event put this (so it will always run whenever textbox changes)
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'Calculate new needleangle
'but first test TextBox to see if its a CORECT number!!!
Dim num As Double = 0
If Double.TryParse(TextBox1.Text, num) = True Then 'it's double so continue with if then
num = CDbl(TextBox1.Text.Text)
End If
needleAngle = CSng(num * (dialAngle / 2) / maximumVolts)
'Invalidate picbox1 to redraw itself with new value of 'needleangle'
PictureBox1.Invalidate()
End Sub
This is globaly how would I do it. U need more details inside but its ok to start with...
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
@ Zelijko: the Double.TryParse will actually put the double value in your variable (num) if the conversion was successful, so you don't really need that num = CDbl() part. What you would need is a Else block that would handle the situation if the conversion failed.
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
roger... :thumb: U are right.
I hope that catherine0136 will get the idea out of this example, and with little cleanup ;) she can make it work...
Re: Getting voltage from RS232 & allow dial voltmeter to point according to voltage l
Quote:
Originally Posted by
catherine0136
@BB
Yes, i am getting the readings from the solar panel and the voltage will be appearing in the textbox, it is not what i want to happen.
Therefore, i need to allow the voltage needle to point according to the voltage level shown in the textbox.
Hope my explanation is clearer now. =)
We're getting there (I hope:)). When you say the voltage "will be appearing in the text box", do you mean someone at the school is going to show you how to do it? [EDIT: Looking back to your first message in this thread, you say the voltage already appears in the text box. Then there's really no problem!]
Once the voltage shows in the textbox, it's easy. You just set the value of volts to the value in the textbox. You could possibly do it the way makdu suggests, but I'd prefer this: put the following lines in the TextChanged event sub of the textbox.
Code:
Single.TryParse(TextBox1.Text, volts)
Me.Refresh
You can get the code for the TextChanged sub by double-clicking on the textbox.
cheers, BB
1 Attachment(s)
Re: [RESOLVED] Getting voltage from RS232 & allow dial voltmeter to point according t
Thanks everyone for your help and time in responding to my thread. :)
I managed to solve it with the help of everyone's suggestions ! :thumb:
This was actually what i did.
I added a timer { also known as "timer3", and TextBoxVoltage is the box which displays the voltage}
and included the following code:
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Me.Text = "Solar Cell : " & volts.ToString("0.00") & "volts"
Single.TryParse(TextBoxVoltage.Text, volts)
Me.Refresh()
End Sub
Attached image is the outcome of what you'll see on the screen. =)
I tried putting the above coding into the TextChanged event , (assuming voltage level in the textbox is varying between 2.5 to 2.8V ), the needle will keep returning back to 0 and back to 2.5 V to 2.8 V and continue back to 0again.
Therefore i tried by putting at timer3, and it works the way i want it to be. The voltage needle will not return back to zero and will only move according to the varying voltage shown in the textbox.
[This thread has been marked as resolved]
Once again, thanks everyone for your help. :)
1 Attachment(s)
Re: [RESOLVED] Getting voltage from RS232 & allow dial voltmeter to point according t
I do have one more question.
I would like to allow the lines of the dial voltmeter reading to look like the attached image.
Like for example, within the range of 1 to 2 , only the lines at exactly 1 and 2 are long lines. I would also like the middle of 1 to 2, which is 1.5, to have a long line too for easier reading.
I'm not sure if the above mentioned is easily understood. If theres a need for me to re-phrased my question to make it clearer, please inform me. Thanks. =)
If i'm not wrong i'll have to do something within this code as shown below?
I add tried modifying Mod 10 to Mod 5 , and mark/10 to mark/5, but it didnt work well. the numberings are not right. (as shown on attached image)
Code:
If mark Mod 10 = 0 Then
'draw long line
e.Graphics.DrawLine(Pens.Gray, innerPoint1, outerPoint)
'draw numeral
e.Graphics.DrawString(mark / 10.ToString, font, _
Brushes.Black, innerPoint1.X - 5, innerPoint1.Y + 1)
Else
'draw short line
e.Graphics.DrawLine(Pens.Gray, innerPoint2, outerPoint)
End If
Re: [RESOLVED] Getting voltage from RS232 & allow dial voltmeter to point according t
Sorry, please kindly ignore the last post that i've posted, (Today 12:01 PM ), as i've figured out the solution for it already !! :)
So this thread is offically resolved. :thumb: