Introduction
its a Digital Thermometer application which reads reading from serial port which are sent there by a hardware which calculates temperature frequently and transmits the readings to pc at a baud rate of 9600 bps
<img src="DigitalThermometer/thermometer.png">
Using the code
First of all lets in the form load event initialise the serial port:
form1_load(object sender,eventargs e)
{
portname = "COM4"; parity = Parity.None; BaudRate = 9600; stopbits = StopBits.Two; databits = 8; port = new System.IO.Ports.SerialPort(portname);
port.Parity = parity;
port.BaudRate = BaudRate;
port.StopBits = stopbits;
port.DataBits = databits;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
}
<span class="Apple-style-span" style="font-family: Verdana; font-size: 16px; "> </span>
Next we write the code to collect readings from serial port, calculating temperature and then converting it to proper angle through which the arm is to be rotated
<span class="Apple-style-span" style="font-family: Verdana; font-size: 13px; white-space: normal; "><pre lang="cs">void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
port.Read(bt, 0, 1);
sum += Convert.ToDouble(bt[0]) *voltage*100/255;
count++;
if (count == 100)
{
InitialTemp = temp;
temp = sum / 100;
sum = 0;
count = 0;
double angle = 0;
if ((Math.Floor(temp) > 30 && Math.Floor(temp) <= 40) || (Math.Floor(temp) < 70 && Math.Floor(temp) > 60))
{
if (Math.Floor(temp) <= 40)
angle = -65 + 3 * ((temp - 1) % 10) + 3;
else
{
angle = 35 + 3 * ((temp - 1) % 10) + 3;
}
}
else if ((Math.Floor(temp) > 40 && Math.Floor(temp) <= 50) || (Math.Floor(temp) <= 60 && Math.Floor(temp) >= 50))
{
if (Math.Floor(temp) <= 50)
angle = -35 + 35 * ((temp - 1) % 10) / 10 + 3.5;
else
{
angle = 35 * ((temp - 1) % 10) / 10 + 3.5;
}
}
else
{
if (Math.Floor(temp) <= 30)
angle = -125 + 2 * temp;
else
{
angle = 65 + 2 * (temp - 70);
}
}
if (AnimationAllowed)
Animate(CurrentAngle, angle);
}
}
and then finally the Form1_Paint event which paints the form :
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (firsttime)
if (AnimationAllowed)
AnimationAllowed = false;
try
{
surface.FillRectangle(Brushes.LightSteelBlue, DrawingRectangle);
Image img = new Bitmap(Properties.Resources.speedometer, this.Size);
Graphics ImageGraphics = Graphics.FromImage(img);
Image hand = new Bitmap(Properties.Resources.MinuteHand);
surface.DrawImageUnscaled(img, new Point(0, 0));
surface.TranslateTransform(this.Width / 2f, this.Height / 2f);
surface.RotateTransform((float)CurrentAngle);
surface.DrawImage(hand, new Point(-10, -this.Height / 2 + 40));
string stringtemp = displaytemp.ToString(); stringtemp = stringtemp.Length > 5 ? stringtemp.Remove(5, stringtemp.Length - 5) : stringtemp;
Font fnt = new Font("Arial", 20);
SizeF siz = surface.MeasureString(stringtemp, fnt);
surface.ResetTransform();
LinearGradientBrush gd = new LinearGradientBrush(new Point(0,(int)siz.Height + 20), new Point((int)siz.Width,0), Color.Red, Color.Lavender);
surface.DrawString(stringtemp, fnt, gd, new PointF(DrawingRectangle.Width / 2 - siz.Width / 2, 70));
surface.DrawEllipse(Pens.LightGray, DrawingRectangle);
buff.Render();
if (firsttime)
{
firsttime = false;
AnimationAllowed = true;
}
}
catch (InvalidOperationException)
{
}
} Points of Interest
this tells how we can read serial port data and then use it in applications.
the hardware is also my self creation.
For Hardware details, leave me a message
History
Keep a running update of any changes or improvements you've
made here.