Re: Why is this simple addition throwing an overflow exception?



The overflow is occuring when you are attempting to convert the check sum
into a byte. The addition is working, but the total is > 255, which causes
an overflow inside the CByte function. I suspect you really want the
following:

SndCkSum = CByte((SndMin + SndSec + SndT1 + SndT2 + SndPer + SndT1f +
SndT2f) mod 256)

Mike Ober.


"Mike" <nomtrxspam@xxxxxxxxxxx> wrote in message
news:ghbhm2hbj3d3l416b1l6veur33s7rb68pr@xxxxxxxxxx
Hello All,
Please, if anyone can point me to the problem, I'd sure appreciate it!
I am very new to VB programming and not a programmer to begin with.
This is part of a Visual Basic 2005 Express Edition program to control a
remote basketball
scoreboard display unit.

All I'm trying to do is add 5 byte variables and store the result in an
integer
variable. I added a Try/Catch block to take look at things.
This exception occurs only when the clock runs down to 00:00 and when one
of either
SndT1 or SndT2 are some non zero value. The value of 15 for SndMin is due
to the program
automatically starting a halftime clock that starts at 15:00.

CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer <--- The offending
line of code
CkSum = 15 + 0 + 99 + 99 + 130 = 343 <-- Values are from
the texbox in the catch block
The catch block shows CkSum = 200, but 200 is just the value left from the
last addition that worked.


The variable declrations and the entire sub where the exception is
originating were pasted
below from the program code.

TIA
Mike

This is the output displayed in the textbox by the catch block.

CkSum = 200 SndT1f = 9 HornTime = 1 SndT2f = 9 SndPer = 130
SndT1 = 99 SndT2 = 99 SndMin = 15
SndSec = 0 ex = System.OverflowException: Arithmetic operation resulted
in an overflow.
at ScoreBoard.Form1.SendPacket() in C:\Documents and
Settings\Administrator\My Documents\Visual Studio
2005\Projects\ScoreBoard1\ScoreBoard\Form1.vb:line 552


These are are variable declarations involved

Public Class Form1

Inherits System.Windows.Forms.Form


Dim SndMin, SndSec, SndT1, SndT2, SndPer As Byte
Dim SndT1f, SndT2f, SndHrn, SndCkSum As Byte
Dim CkSum as Integer
Dim PacketData() As Byte = {&H55, &HCC, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Public ClockMode As Byte = 0
Public Period As Byte = 1
Public HornTime As Byte = 2
Public T1score As Byte = 0
Public T2score As Byte = 0
Public T1Fouls As Byte = 0
Public T2Fouls As Byte = 0
Public Mins As Byte = 0
Public Secs As Byte = 0


This is the sub in which the overflow exception occurs

'This routine builds a data packet to send to the remote display
assembly

Private Sub SendPacket()
SndT1 = T1score : SndT2 = T2score : SndPer = Period : SndHrn =
HornTime
SndMin = Mins : SndSec = Secs : SndT1f = T1Fouls : SndT2f =
T2Fouls
If ClockMode = 1 Then ' See if a timeout has been called
SndPer = CByte(SndPer Or &H80) ' Turn on the flash LED flag
bit
End If
Try
If ExtDisp = True Then ' Prepare a packet if using an
extended display

'Calculate a simple checksum to add to the end of the
packet

The following line will also cause an exception if I change ExtDisp to
True.

SndCkSum = CByte(SndMin + SndSec + SndT1 + SndT2 + SndPer
+ SndT1f + SndT2f)

' Load the 7 data bytes and the checksum byte into the
data packet array
' PacketData(0) and PacketData (1) are initialized to &H55 and &Hcc
' respectively to form a constant 2 byte header for the packet

PacketData(2) = SndMin : PacketData(3) = SndSec :
PacketData(4) = SndT1
PacketData(5) = SndT2 : PacketData(6) = SndPer :
PacketData(7) = SndT1f
PacketData(8) = SndT2f : PacketData(9) = SndCkSum

Else ' Prepare a packet for a standard display

'Calculate a simple checksum to add to the end of the data
packet

This is the line causing the error.
Line 552
********--> CkSum = SndMin + SndSec + SndT1 + SndT2 + SndPer

SndCkSum = CByte(CkSum) ' Explicitly convert the
checksum to a byte

' Load the 5 data bytes and the checksum byte into the
packet array
' PacketData(0) and PacketData (1) are initialized to &H55 and &Hcc
' respectively to form a constant 2 byte header for the packet

PacketData(2) = SndMin : PacketData(3) = SndSec :
PacketData(4) = SndT1
PacketData(5) = SndT2 : PacketData(6) = SndPer :
PacketData(7) = SndCkSum
End If

' Send the data packet to the display

Call SendSerialData(PacketData)

' Reset the horn on flag if it was set

If HornTime > &H7F Then HornTime = CByte(HornTime - &H80)

Catch ex As OverflowException
Dim Str As String
TextBox3.Visible = True
Str = "CkSum = " + CkSum.ToString + " SndT1f = " +
SndT1f.ToString + " " _
+ "HornTime = " + HornTime.ToString + " SndT2f = " +
SndT2f.ToString + " " _
+ "SndPer = " + SndPer.ToString + " " _
+ "SndT1 = " + SndT1.ToString + " SndT2 = " + SndT2.ToString
+ " " _
+ "SndMin = " + SndMin.ToString + " SndSec = " +
SndSec.ToString + _
" ex = " + ex.ToString + " "
TextBox3.Text = Str
Timer1.Stop()
End Try
End Sub




"The scientist is possessed by the sense of universal
causation...His religious feeling takes the form of
rapturous amazement at the harmony of natural law,
which reveals the intelligence of such superiority
that, compared with it, systematic thinking and acting
of human beings is an utterly insignificant reflection."
Albert Einstein (theoretical physicist)



.



Relevant Pages

  • Why is this simple addition throwing an overflow exception?
    ... 'This routine builds a data packet to send to the remote display assembly ... ' Load the 7 data bytes and the checksum byte into the data packet array ...
    (microsoft.public.dotnet.languages.vb)
  • Re: UDP 4 transefer file
    ... > package number or checksum. ... Bytes 0 through 3, packet sequence number. ... Data packet, ... Acknowledgement packet, ackwnowledges receipt of packets ...
    (comp.unix.programmer)
  • Entirely ignoring TCP and UDP checksum in kernel level
    ... - We have an old network concentrator device in our WAN, ... TCP SYN ACK reply from 192.168.1.1 ... The client from which we send telnet requests to the device gets a packet ... The network concentrator computes the TCP checksum with the source address ...
    (Linux-Kernel)
  • [UNIX] 13 Remote Ethereal Buffer Overflows (BGP, EIGRP, IGAP, IRDA, ISUP, NetFlow, PGM, TCAP and UCP
    ... expect in a protocol analyzer, and several features not seen in any other ... convincing someone to load a malicious packet capture file into Ethereal. ... NetFlow v9 Dissector Template Caching Overflow ... In both cases remote code execution exploitation is possible. ...
    (Securiteam)
  • RE: Strange kernel happenings
    ... > packet has arrived intact. ... the ip header check sum covers the ip header only. ... The checksum in the IP header covers the ip header only, ... ICMP has a checksum, which covers the entire ICMP message. ...
    (Incidents)