receiving 2 16bit numbers 
Author Message
 receiving 2 16bit numbers

Hi,
I have managed to achieve this but it falls over sometimes when I assign the
incomming four bytes to the two VB double variables.  The error message is
"overflow".

Being new to this type of programming (RS232 data capture) I have cobbled it
up in "my own" way.  What is the official way - it seems so hard to find
specific stuff in the VB 6 manual.

My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set
to an interrupt when 4 bytes are received. The hardware can send any byte
value for all four bytes. Basically, I want to receive the two16 bit numbers
from the PIC microcontroller into two real numbers in VB - a and c.

After a lot of messing about I came up with what cannot be the right
solution (?) but it works except for the frequent overflows. The overflows
occur on the lines starting a=a+256  and c=c+256.

    Case comEvReceive 'Received RThreshold # of chars Rthreshold =4
        'receive 2 - 16-bit words
        UStimes = MSComm1.Input
        If UStimes <> "" Then
            a = Asc(Mid(UStimes, 1, 1))
            a = a + 256 * Asc(Mid(UStimes, 2, 2))
            c = Asc(Mid(UStimes, 3, 3))
            c = c + 256 * Asc(Mid(UStimes, 4, 4))
        End If
        DoEvents
        draw_graph
        UStimes = ""
    'Case comEvSend ' There are SThreshold number of characters in the TX
buffer
End Select

It works but there must be a "proper" method to capture the four bytes into
two integers. In the above it has been necessary to capture them into
doubles.

any help would be appreciated

Spencer



Mon, 03 Jun 2002 03:00:00 GMT  
 receiving 2 16bit numbers
VB handles all intermediate variables as Ints unless told otherwise.
Cast the intermediate as a Long, and your problem will go away.

Also, your code appears to be specifying the incorrect "length"
parameter for the Mid function. It should look like this:

            a = Asc(Mid(UStimes, 1, 1))
            a = a + (256& * Asc(Mid(UStimes, 2, 1)))
            c = Asc(Mid(UStimes, 3, 1))
            c = c + (256& * Asc(Mid(UStimes, 4, 1)))

Enjoy!


Quote:
>Hi,
>I have managed to achieve this but it falls over sometimes when I assign
the
>incomming four bytes to the two VB double variables.  The error message is
>"overflow".

>Being new to this type of programming (RS232 data capture) I have cobbled
it
>up in "my own" way.  What is the official way - it seems so hard to find
>specific stuff in the VB 6 manual.

>My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is
set
>to an interrupt when 4 bytes are received. The hardware can send any byte
>value for all four bytes. Basically, I want to receive the two16 bit
numbers
>from the PIC microcontroller into two real numbers in VB - a and c.

>After a lot of messing about I came up with what cannot be the right
>solution (?) but it works except for the frequent overflows. The overflows
>occur on the lines starting a=a+256  and c=c+256.

>    Case comEvReceive 'Received RThreshold # of chars Rthreshold =4
>        'receive 2 - 16-bit words
>        UStimes = MSComm1.Input
>        If UStimes <> "" Then
>            a = Asc(Mid(UStimes, 1, 1))
>            a = a + 256 * Asc(Mid(UStimes, 2, 2))
>            c = Asc(Mid(UStimes, 3, 3))
>            c = c + 256 * Asc(Mid(UStimes, 4, 4))
>        End If
>        DoEvents
>        draw_graph
>        UStimes = ""
>    'Case comEvSend ' There are SThreshold number of characters in the TX
>buffer
>End Select

>It works but there must be a "proper" method to capture the four bytes into
>two integers. In the above it has been necessary to capture them into
>doubles.

>any help would be appreciated

>Spencer



Mon, 03 Jun 2002 03:00:00 GMT  
 receiving 2 16bit numbers
Hi,
I have managed to achieve this but it falls over sometimes when I assign the
incomming four bytes to the two VB double variables.  The error message is
"overflow".

Being new to this type of programming (RS232 data capture) I have cobbled it
up in "my own" way.  What is the official way - it seems so hard to find
specific stuff in the VB 6 manual.

My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set
to an interrupt when 4 bytes are received. The hardware can send any byte
value for all four bytes. Basically, I want to receive the two16 bit numbers
from the PIC microcontrolle
r into two real numbers in VB - a and c.

After a lot of messing about I came up with what cannot be the right solution
(?) but it works except for the frequent overflows. The overflows occur on
the lines starting a=a+256  and c=c+256.

    Case comEvReceive 'Received RThreshold # of chars Rthreshold =4
        'receive 2 - 16-bit words
        UStimes = MSComm1.Input
        If UStimes <> "" Then
            a = Asc(Mid(UStimes, 1, 1))
            a = a + 256 * Asc(Mid(UStimes, 2, 2))
            c = Asc(Mid(UStimes, 3, 3))
            c = c + 256 * Asc(Mid(UStimes, 4, 4))
        End If
        DoEvents
        draw_graph
        UStimes = ""
    'Case comEvSend ' There are SThreshold number of characters in the TX
buffer
End Select

It works but there must be a "proper" method to capture the four bytes into
two integers. In the above it has been necessary to capture them into
doubles.

any help would be appreciated

Spencer



Mon, 03 Jun 2002 03:00:00 GMT  
 receiving 2 16bit numbers
VB handles all intermediate variables as Ints unless told otherwise. Cast the
intermediate as a Long, and your problem will go away.

Also, your code appears to be specifying the incorrect "length" parameter for
the Mid function. It should look like this:

            a = Asc(Mid(UStimes, 1, 1))
            a = a + (256& * Asc(Mid(UStimes, 2, 1)))
            c = Asc(Mid(UStimes, 3, 1))
            c = c + (256& * Asc(Mid(UStimes, 4, 1)))

Enjoy!


Quote:
>Hi,
>I have managed to achieve this but it falls over sometimes when I assign
the
>incomming four bytes to the two VB double variables.  The error message is
>"overflow".

>Being new to this type of programming (RS232 data capture) I have cobbled
it
>up in "my own" way.  What is the official way - it seems so hard to find
>specific stuff in the VB 6 manual.

>My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is
set
>to an interrupt when 4 bytes are received. The hardware can send any byte
>value for all four bytes. Basically, I want to receive the two16 bit
numbers
>from the PIC microcontroller into two real numbers in VB - a and c.

>After a lot of messing about I came up with what cannot be the right
>solution (?) but it works except for the frequent overflows. The overflows
>occur on the lines starting a=a+256  and c=c+256.

>    Case comEvReceive 'Received RThreshold # of chars Rthreshold =4
>        'receive 2 - 16-bit words
>        UStimes = MSComm1.Input
>        If UStimes <> "" Then
>            a = Asc(Mid(UStimes, 1, 1))
>            a = a + 256 * Asc(Mid(UStimes, 2, 2))
>            c = Asc(Mid(UStimes, 3, 3))
>            c = c + 256 * Asc(Mid(UStimes, 4, 4))
>        End If
>        DoEvents
>        draw_graph
>        UStimes = ""
>    'Case comEvSend ' There are SThreshold number of characters in the TX
>buffer
>End Select

>It works but there must be a "proper" method to capture the four bytes into
>two integers. In the above it has been necessary to capture them into
>doubles.

>any help would be appreciated

>Spencer



Mon, 03 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Total number of bytes received?

2. ADO is having problems with receiving numbers from Oracle DB

3. VB4 - 16BIT Sheridan Calendar Widgets (16bit OCX)

4. 16bit SourceSafe problems w/VB4.0-16bit - too many file handles open

5. VB3 16bit to VB4 16bit

6. Receiving notification on Send, Receive and Delete

7. socket.receive fails to receive

8. Auto Number in a Number Field

9. Automatic numbering via VBA module numbers out of sequence

10. Exporting a 3 decimal number to text file produces trunc 2 decimal text number

11. Convert text and numbers to numbers

12. different numbers, which is lowest number

 

 
Powered by phpBB® Forum Software