Visual basic and cfa-633

ejf28

New member
works perfectly.

Thank you so much cosmicvoid for all the help throught this forum. I should hopefully be good to go now. It's actually pretty fun being able to do stuff now. I have it set up to change the brightness, contrast, and fan speed among other things and I am happy that I am now able to do this. So once again thank you very much for all help and any annoyance I may have caused.
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 

ejf28

New member
Actually last question I promise..
How do i interpret the input of the temperature readings into actual temps?

I added a line for when temp readings are enabled otherwise it will display nothing for the data. When I use
Code:
    Packet = Packet & Convert.ToInt32(sp.ReadByte)
It returns:
Type: 130 Length: 4 Data: 04021 Crc: 171,48

Assuming that is correct, which it may not be, how does the 04021 correlate to a temperature?

Before it starts collecting this every increment I sent it the following to read just the first dow temp:
Code:
Dim crc As WORD
        data(0) = 19
        data(1) = 4
        data(2) = "001"
        data(3) = "000"
        data(4) = "000"
        data(5) = "000"


        crc = Get_Crc(data, 6)
        data(6) = crc.Lo
        data(7) = crc.Hi
        sp.Write(data, 0, data.Length)
which results in a return packet of
Type: 83 Length: 0 Crc: 216,246
before it actally receives the other ones, showing it was enabled.

From looking at the data sheet the return packet should be
Code:
The return packet will be:
type = 0x40 | 18
data_length = 9
data[0] = device index (0-31 valid)
data[1-8] = ROM ID of the device
so im guessing that the rom id is the temp in some other form?
 
Actually last question I promise..
Why should I believe this?:rolleyes:
How do i interpret the input of the temperature readings into actual temps?

I added a line for when temp readings are enabled otherwise it will display nothing for the data. When I use
Code:
    Packet = Packet & Convert.ToInt32(sp.ReadByte)
It returns:
Type: 130 Length: 4 Data: 04021 Crc: 171,48

Assuming that is correct, which it may not be, how does the 04021 correlate to a temperature?
Its not correct. The 4 data bytes are not just a 32 bit value, they represent 3 pieces of info. From the 633 data sheet:
Code:
0x82: Temperature Sensor Report
If any of the up to 32 temperature sensors is configured to report to the host, the CFA-633 will send Temperature Sensor
Reports for each selected sensor every second. See the command 19: Set Up Temperature Reporting (Pg. 26) below.
type = 0x82
data_length = 4
[B]data[0] is the index of the temperature sensor being reported:
0 = temperature sensor 1
1 = temperature sensor 2
. . .
31 = temperature sensor 32
data[1] is the MSB of Temperature_Sensor_Counts
data[2] is the LSB of Temperature_Sensor_Counts
data[3] is DOW_crc_status[/B]
So you need to convert the 2nd and 3rd data bytes to a Short variable.
Code:
Dim sensor as Byte
Dim Tcounts as Short
sensor = sp.ReadByte()   'sensor number being reported
Tcounts = sp.ReadByte() << 8   'MSB of temp count
Tcounts = Tcounts + sp.ReadByte()   'LSB of temp count
Before it starts collecting this every increment I sent it the following to read just the first dow temp:
Code:
Dim crc As WORD
        data(0) = 19
        data(1) = 4
        data(2) = "001"
        data(3) = "000"
        data(4) = "000"
        data(5) = "000"


        crc = Get_Crc(data, 6)
        data(6) = crc.Lo
        data(7) = crc.Hi
        sp.Write(data, 0, data.Length)
which results in a return packet of
Type: 83 Length: 0 Crc: 216,246
before it actally receives the other ones, showing it was enabled.
Command 19 sets up the automatic reporting. But in your code, it looks like you are assigning a string to a byte variable.
Code:
        data(2) = "001"
        data(3) = "000"
        data(4) = "000"
        data(5) = "000"
should be
        data(2) = 1
        data(3) = 0
        data(4) = 0
        data(5) = 0
From looking at the data sheet the return packet should be
Code:
The return packet will be:
type = 0x40 | 18
data_length = 9
data[0] = device index (0-31 valid)
data[1-8] = ROM ID of the device
so im guessing that the rom id is the temp in some other form?
No, this is just the unique address of the temp sensor, and its not really useful, as it has no relation to the temperature readings.

To convert the Short value received in the sensor reports, you can use the algorithm given in the data sheet on page 19. You will have to convert the Short to a floating point variable first.
Code:
Dim Degc As Double
Dim Degf As Double

Degc = Convert.ToDouble(Tcounts) / 16.0
Degf = (Degc * 9.0) / 5.0 + 32.0
 

ejf28

New member
Unable to cast object of type 'WORD' to type 'System.IConvertible'.

i captured the msb and lsb in an earlier method and put them into tcounts as a hi and low
Code:
'data(0) = temp sensor number
            'data(1) = msb of temp sensor
            'data(2) = lsb of temp sensor
            'data(3) = dow crc status
            Dim Tcounts As WORD
            Dim Degc As Double

            Tcounts.Hi = data(1)   'MSB of temp count
            Tcounts.Lo = data(2)  'LSB of temp count

            Degc = Convert.ToDouble(Tcounts)
when it gets to Degc = convert.todouble(Tcounts) it errors because it can't convert a type word to a double
 

ejf28

New member
Here is my method so far anyways:

Code:
        Dim Type As Byte
        Dim Length As Byte
        Dim Packet As String
        Dim data(sp.ReadBufferSize) As Byte

        Type = sp.ReadByte()    'get packet type
        Length = sp.ReadByte()    'get data length
        Packet = "Type: " & Type & " Length: " & Length & " "

        Dim i As Integer
        If Length <> 0 Then        'if any data in the packet

            Packet = Packet & " Data: "
            For i = 0 To Length - 1    'read the data bytes
                If tempsensor = True Then
                    data(i) = sp.ReadByte
                Else
                    Packet = Packet & Convert.ToChar(sp.ReadByte())
                End If

            Next i
            Packet = Packet & " "
        End If

        Packet = Packet & "Crc: " & sp.ReadByte()    'the first crc byte
        Packet = Packet & "," & sp.ReadByte()        '2nd crc byte

        'now this packet is done, the next character (if any) in the ReadBuffer is
        'part of a new packet

        'textforrtb = Packet

        If tempsensor = True Then
            'data has data
            'data(0) = temp sensor number
            'data(1) = msb of temp sensor
            'data(2) = lsb of temp sensor
            'data(3) = dow crc status
            Dim Tcounts As WORD
            Dim Degc As Double

            Tcounts.Hi = data(1)   'MSB of temp count
            Tcounts.Lo = data(2)  'LSB of temp count


            Degc = Convert.ToDouble(Tcounts)

            textforrtb = Packet & " msb: " & Tcounts.Hi & " lsb: " & Tcounts.Lo
            'textforrtb = Packet & " temp sensor: " & data(0) & " msb: " & data(1) & " lsb: " & data(2) & " dowcrcstatus: " & data(3)


        Else
            textforrtb = Packet
        End If
 
I don't understand why you still have such a large data() array; you only need a maximum of 256 bytes:

Dim data(256) As Byte

As for the temperature data and conversion, go back and re-read my previous post because I edited it when I realized that the 2-byte data from the temp sensor is signed, so using a WORD variable (unsigned) was wrong. You would have gotten bogus results for temps less than 0°C. The code now has Tcounts As Short, and different byte handling.

Also, you didn't copy the conversion algorithm correctly.
 

ejf28

New member
not sure how the algorithm wasn't used correctly. The only difference I thought was that in my earilier method it saves the data bytes as a different byte array and once all the other stuff is done before it outputs it, it looks through those bytes and figures out the temps and such. Here is what I have now, no errors but temp readings def aren't correct:
Code:
        Dim Type As Byte
        Dim Length As Byte
        Dim Packet As String
        Dim data(256) As Byte

        Type = sp.ReadByte()    'get packet type
        Length = sp.ReadByte()    'get data length
        Packet = "Type: " & Type & " Length: " & Length & " "

        Dim i As Integer
        If Length <> 0 Then        'if any data in the packet

            Packet = Packet & " Data: "
            For i = 0 To Length - 1    'read the data bytes
                If tempsensor = True Then
                    data(i) = sp.ReadByte
                Else
                    Packet = Packet & Convert.ToChar(sp.ReadByte())
                End If

            Next i
            Packet = Packet & " "
        End If

        Packet = Packet & "Crc: " & sp.ReadByte()    'the first crc byte
        Packet = Packet & "," & sp.ReadByte()        '2nd crc byte

        'now this packet is done, the next character (if any) in the ReadBuffer is
        'part of a new packet

        'textforrtb = Packet

        If tempsensor = True Then
            'data has data
            'data(0) = temp sensor number
            'data(1) = msb of temp sensor
            'data(2) = lsb of temp sensor
            'data(3) = dow crc status
            Dim Tcounts As Short
            Dim Degc As Double


            Tcounts = data(1) << 8   'MSB of temp count
            Tcounts = Tcounts + data(2)   'LSB of temp count



            Degc = Convert.ToDouble(Tcounts) / 16.0

            textforrtb = Packet & " temp: " & Degc
 

        Else
            textforrtb = Packet
        End If
 
Your code looks OK now. Earlier, you had left off the "/ 16.0" on the Degc calculation. But you say it doesn't show a valid temperature?

I don't know how/where you set the state of "tempsensor" for your:
Code:
If tempsensor = True Then
   .....
End If
I think that this would achieve the same result:
Code:
If Type = &H82 Then   'temp report packet
    ....
End If
So, what are the 4 data bytes that are received in the temp reporting packet, and what is the value of Tcounts, and Degc? I don't understand why its not working.
 

ejf28

New member
that tempcontrol variable is just because im using the same sub for inputting other things as well, so if i click a button that gets the temp, it puts that value true, so that just in that case it saves the data to that byte array, however i guess it could do that all the time. I restructured it a little bit but same idea again, mainly so that it carries the data to the end at which point it can go through it depending on the case.

Code:
        Dim Type As Byte
        Dim Length As Byte
        Dim Packet As String
        Dim data(256) As Byte

        Type = sp.ReadByte()    'get packet type
        Length = sp.ReadByte()    'get data length
        Packet = "Type: " & Type & " Length: " & Length & " "

        Dim i As Integer
        If Length <> 0 Then        'if any data in the packet

            For i = 0 To Length - 1    'read the data bytes

                data(i) = sp.ReadByte

            Next i
            Packet = Packet & " "
        End If

        Packet = Packet & "Crc: " & sp.ReadByte()    'the first crc byte
        Packet = Packet & "," & sp.ReadByte() '2nd crc byte



        If tempsensor = True Then
            'data has data
            'data(0) = temp sensor number
            'data(1) = msb of temp sensor
            'data(2) = lsb of temp sensor
            'data(3) = dow crc status
            Dim Tcounts As Short
            Dim Degc As Double


            Tcounts = data(1) << 8   'MSB of temp count
            Tcounts = Tcounts + data(2)   'LSB of temp count



            Degc = Convert.ToDouble(Tcounts) / 16.0

            textforrtb = Packet & " temp: " & Degc


        Else
            Packet = Packet & " Data: "
            i = 0
            Do Until i = data.Length
                Packet = Packet & Convert.ToChar(data(i))
                i = i + 1
            Loop

            textforrtb = Packet
        End If
changing it to output the msb and lsb values gives:
Code:
Type: 130 Length: 4  Crc: 199,112 msb: 16 lsb 2
I wish i was running my computer that cold :)
Should be more around 28-34 degrees or so though
 
that tempcontrol variable is just because im using the same sub for inputting other things as well, so if i click a button that gets the temp, it puts that value true, so that just in that case it saves the data to that byte array, however i guess it could do that all the time.
I don't see anyplace where "tempsensor" get set to False, so once it is set, it stays set forever?? It would make more sense to use the "If Type = &H82" statement, because it would react correctly to automatically generated packets.

In this code, the loop will execute 256 times, which is the size of "data.Length".
Code:
            Do Until i = [b]data.Length[/b]
                Packet = Packet & Convert.ToChar(data(i))
                i = i + 1
            Loop
I think you want it to do this:
Code:
            Do Until i = Length
Same for your other loop, further down.
changing it to output the msb and lsb values gives:
Code:
Type: 130 Length: 4  Crc: 199,112 msb: 16 lsb 2
You still didn't say what the value of the 4 data bytes, Tcount and Degc were.
 

ejf28

New member
Result is
Type: 130 Length: 4 Crc: 234,64 data: 0 2 2 14 0.25
that end data is:
Code:
            textforrtb = Packet & " data: " & data(0) & " " & data(1) & " " & data(2) & " " & data(3) & Tcounts & " " & Degc
Same code as before too.

The tempsensor true false thing is just when u click a button that says "Enable temp sensors" it puts that to true and sends the line of code to the cfa633 to start sending those temp packets. There is another button that says "Disable temp sensors" and that sends a different packet to stop receiving the temp packets. That is the only reason i am using it, it wont affect what it is receiving, the right part of the code is being accessed when it needs to be so cant be that variables fault
 
Last edited:
Result is
Type: 130 Length: 4 Crc: 234,64 data: 0 2 2 14 0.25
that end data is:
Code:
            textforrtb = Packet & " data: " & data(0) & " " & data(1) & " " & data(2) & " " & data(3) & Tcounts & " " & Degc
I am confused because there are 5 values listed instead of 6. I'm guessing that there is a space missing between data(3) and Tcounts. So it might be:
0, 2, 2, 1, 4, 0.25
If so, then Tcounts should be (2 << 8) + 2 = 514.
Degc should be 514.0 / 16.0 = 32.125°C.

I wonder why Tcounts is not being calculated correctly. Try this:
Code:
Tcounts = (data(1) * 256) + data(2)
or, as a last resort
Tcounts = (Convert.ToShort(data(1)) * 256) + Convert.ToShort(data(2))
 

ejf28

New member
Still no luck.

I tried the second method first
Code:
Tcounts = (Convert.ToShort(data(1)) * 256) + Convert.ToShort(data(2))
however vb cant convert it to a short like that so I did it this way which is the same thing

Code:
Dim Tcounts As Short
            Dim Degc As Double
            Dim msb As Short
            Dim lsb As Short
            msb = data(1) * 256
            lsb = data(2)
 
            Tcounts = msb + lsb


            Degc = Convert.ToDouble(Tcounts) / 16.0

            textforrtb = Packet & " data: " & Tcounts & " " & Degc
this gave results of:
Code:
Type: 130 Length: 4  Crc: 239,204 data: 1282 80.125
where the first number is tcounts and second degc, which aren't correct

Using the first way
Code:
Tcounts = (data(1) * 256) + data(2)
gives identical results
Code:
Type: 130 Length: 4  Crc: 239,204 data: 1282 80.125
I am going to try and look at what it is putting out for
what you said first:

If so, then Tcounts should be (2 << 8) + 2 = 514.
Degc should be 514.0 / 16.0 = 32.125°C.
 
Still no luck.... however vb cant convert it to a short like that ...
[rant]I think VB is a nasty, badly designed language, and this whole episode does a great deal to reinforce that opinion.[/rant]
Code:
Dim Tcounts As Integer
Dim msb As Integer
Dim lsb As Integer
Dim debug As String
[FONT=monospace]
[/FONT]msb = Convert.ToInt(data(1))
lsb = Convert.ToInt(data(2))
lsb = lsb AND &HFF
debug = "Data: " & data(1) & " " & data(2) & " msb: " & msb & " lsb: " & lsb
Tcounts = msb * 256
debug = debug & " Tc_Hi: " & Tcounts
Tcounts = Tcounts + lsb
debug = debug & " Tc: " & Tcounts
What does the above produce for the "debug" string?
 
It looks like that code "works", but the mystery is why is the value of data(1) = 26? Thats way out of range for the D1W temp sensor. The highest value that data(1) should ever be is 7 (the max for Tcounts would be 2000 [&h7D0]).

So it looks like somehow your data packet is corrupted. I don't suppose you are checking that the crc is correct. It looks like your earlier data() samples were good, back when the code wasn't handling it properly.

Not sure what to suggest, now.
 
USB LCD Displays - Graphic and Character LCDs with a Keypad

ejf28

New member
to check the crc isn't that just the fourth byte (the one after the msb and lsb), that is always 1 which i'd assume means its correct
 
Top