本文介绍了如何将结果从摄氏温度转换为华氏温度并显示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有以下代码,可以读取串口上的2个温度流。



如何修改代码,所以我可以按一个按钮,将读数从摄氏度改为华氏度。



感谢您的帮助。



Nevjc



Hi All,
I have the folowing code that reads 2 temperature streams on a serial port.

How can I modify the code so I can press a button and change the reading from centigrade to fahrenheit.

Thank you for your assistance.

Nevjc

Imports System.Drawing.Drawing2D
Imports System.IO.Ports

Public Class Form1
    Dim bmp1 As Bitmap = New Bitmap(165, 550)
    Dim temperature1 As Double = 10
    Dim temperature2 As Double = 10
    Dim WithEvents sp As New SerialPort

    Private Sub pictherm1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pictherm1.Paint
        Dim g As Graphics = e.Graphics
        g.Clear(Color.White)
        ' fill temperature1
        If temperature1 > 100 Then temperature1 = 100
        g.FillRectangle(Brushes.Green, 48, 525 - CInt(temperature1 * 5), 10, CInt(temperature1 * 5))
        ' fill temperature2
        If temperature2 > 100 Then temperature2 = 100
        g.FillRectangle(Brushes.Green, 108, 525 - CInt(temperature2 * 5), 10, CInt(temperature2 * 5))
        'draw scale
        g.DrawLine(Pens.Black, 60, 525, 60, 25)
        g.DrawLine(Pens.Black, 105, 525, 105, 25)
        ' minor ticks
        For i As Integer = 25 To 525 Step 5
            g.DrawLine(Pens.Black, 50, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 115, i)
        Next
        'major ticks
        Dim f As Font = New Font("Verdana", 10, FontStyle.Regular)
        Dim scale As Integer
        For i As Integer = 525 To 25 Step -25
            g.DrawLine(Pens.Black, 45, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 120, i)
            scale = (525 - i) / 5
            g.DrawString(Str(scale), f, Brushes.Black, 65, i - 8)
        Next
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            sp.BaudRate = "9600"
            sp.PortName = "COM5"
            sp.Open()
            If sp.IsOpen Then
                btnConnect.Visible = False
                btnDisconnect.Visible = True
            End If
        Catch
            sp.Close()
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If sp.IsOpen() Then
            MessageBox.Show("Press Hold Before Closing")
            e.Cancel = True
        End If
    End Sub

    Delegate Sub myMethodDelegate1(ByVal [text] As String)
    Dim myDelegate1 As New myMethodDelegate1(AddressOf ProcessReading1)

    Private Sub SerialPort_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
        Dim str1 As String = sp.ReadLine
        Dim firststr As String = Mid(str1, 1, 4)
        BeginInvoke(myDelegate1, firststr)
    End Sub

    Sub ProcessReading1(ByVal input As String)
        If IsNumeric(input) Then
            temperature1 = CDbl(input)
            lblLeft.Text = CDbl(temperature1)
            pictherm1.Refresh()
        End If
    End Sub

    Delegate Sub myMethodDelegate2(ByVal [text] As String)
    Dim myDelegate2 As New myMethodDelegate2(AddressOf ProcessReading2)

    Private Sub SerialPort_DataReceived2(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
        Dim str2 As String = sp.ReadLine
        Dim secondstr As String = Mid(str2, 6, 5)
        BeginInvoke(myDelegate2, secondstr)

    End Sub

    Sub ProcessReading2(ByVal input As String)
        If IsNumeric(input) Then
            temperature2 = CDbl(input)
            lblRight.Text = CDbl(temperature2)
            lblTotal.Text = CDbl(temperature1 + temperature2)
            lblDiff.Text = CDbl(temperature1 - temperature2)
            pictherm1.Refresh()
        End If
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            sp.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            Exit Sub
        Catch
            MessageBox.Show("Some kind of problem.")
        End Try
    End Sub

End Class

推荐答案

这篇关于如何将结果从摄氏温度转换为华氏温度并显示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 17:54