Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Option Strict On
Imports System.IO.Ports

    Dim q As Queue(Of Byte) = New Queue(Of Byte)

    Private Sub SerialPort1_DataReceived(ByVal sender As Object,
        ByVal e As SerialDataReceivedEventArgs) _
        Handles SerialPort1.DataReceived
        While SerialPort1.BytesToRead > 0
            q.Enqueue(CByte(SerialPort1.ReadByte))
        End While
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        SyncLock q
            While q.Count > 0
                Dim B As Byte = q.Dequeue
                tbRx.Text &= Hex(B) & " "
            End While
        End SyncLock
    End Sub
    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStop.Click
        SerialPort1.Close()
        Timer1.Stop()
    End Sub

NOT FULL CODE
In tbRx if the temperature is something like 21.3 degrees celsius, I see a value of 4 BD. However I want another textbox giving the decimal value( from there i can then convert that to temperature).

What I have tried:

i have tried something like Textbox1.text = B
but that is hopelessly wrong.
Posted
Updated 3-Oct-16 22:30pm
v3

1 solution

It's not obvious from that what format the values coming from your temperature gauge are being sent in: 4 BD in it's simplest conversion 4BD Hex == 1213 Dec which implies it might be "degrees C plus 100 times 10" but from one sample you can't reliably infer that.

So talk to the manufacturer (or at least check their website for documentation) and see if they explain exactly what is being sent and how it should be converted.
We can't do that for you!
 
Share this answer
 
Comments
Member 12292743 4-Oct-16 4:28am    
okay, I want to know how to get 1213 in TExtbox1, then i can get the temperature from there
OriginalGriff 4-Oct-16 4:55am    
You receive the data as two bytes, yes?
So handle it as two bytes, rather than trying to convert them as two separate items.
Remember, a 16 bit number is two eight bit numbers:
int16 = (high byte * 256) + low byte
If you do that, then convert the int16 value to a string, you will get 1213 - but I doubt very much if that is the actual value you need: you need to look at the documentation for the actual device, guessing isn't going to be reliable here!
Member 12292743 4-Oct-16 4:57am    
yeah to get the actual value, subtract 1000 from 1213 and then divide by 10

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900