New topic: Out of bounds exception -- memory block
<http://forums.realsoftware.com/viewtopic.php?t=31404> Page 1 of 1 [ 14 posts ] Previous topic | Next topic Author Message mb52505 Post subject: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 1:31 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 Whenever I run this, all is fine -- it displays a 255 in my statictext box. But -- whenever I uncomment byte 9 or beyond, I get an outofbounds exception thrown. I tried to resize the memory block? BUT when I -- dim data as memoryblock(12) I had a syntax error. Also tried data.size=12 and no go. But then it may not have anything to do with size?, what is my problem exactly? THANKS for any help. Code: dim data as MemoryBlock data = serial1.readall byte1 = data.byte(0) byte2 = data.byte(1) byte3 = data.byte(2) byte4 = data.byte(3) byte5 = data.byte(4) byte6 = data.byte(5) byte7 = data.byte(6) byte8 = data.byte(7) 'byte9 = data.byte(8) 'byte10 = data.byte(9) 'byte11 = data.byte(10) 'byte12 = data.byte(11) portopen = false serial1.close if byte2 = 255 then statictext1.text = str(byte2) else statictext1.text = "not found" end if if serial1.open then portopen = true end if Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 1:33 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 I should say also, I get (as I should) a 255 and not found in my text box that alternate. Top timhare Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 1:43 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6882 Location: Portland, OR USA You're not getting all the data in one go. You're expecting 12 bytes, but you didn't get them all. This is common and normal. You'll get another DataAvailable event in quick succession with the rest. One way to handle it is if you know you're looking for a fixed length of data, then check BytesAvailable to see if you have enough. If not, simply return. The next DataAvailable that fires will deal with the packet. Also, be aware that you might get more than 12 bytes - the current packet plus the beginning of the next packet, if you're receiving several packets in succession. If you want to read just the first 12 bytes and leave the rest, you can call me.Read(12). Tim Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:14 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 That's another issue that had me REALLY confused and you just cleared it up. Readall ? Yeah but how many? In VBNET I specify a specific amount like serial1.read (array, 0, 20) //20bytes starting at 0 Just tried instead -- serial1.read(15) and still got the exception. So, let me ask -- if I use "readall" what can I expect (reliably) from a data available packet? I can live with 7 or 8 bytes of data actually. Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:23 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 Also, f I uncomment byte9 or uncomment everything -- The outofbounds exception is ALWAYS thrown at byte9 (data.byte(8). Top timhare Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:24 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6882 Location: Portland, OR USA With ReadAll, you get however many bytes are in the buffer at the moment. You can check BytesAvailable, or you can check the Size of the memoryblock. Also, if you Read(12) and there are 7 bytes available, you only get those 7, so you still need to check. Tim Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:36 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 Ok Tim -- I follow you. What about lookahead? Can it come into play with this? I read where it reads bytes in succession without deleting them? -- so I could maybe get a constant unbroken stream? Thanks for your help -- I'll get a handle on this. Top timhare Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:37 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6882 Location: Portland, OR USA Lookahead could be very useful, especially when you have a variable size packet coming and the first few bytes indicate how many you should expect. Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 2:44 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 Thanks very much -- will look at lookahead first but I think I may also be overthinking this because if I can reliably get 5 or 6 bytes per event than I can find my 2 qualifiers and process what I need. And I can do that again and again. Next step -- breaking down my raw data bytes into bits. (this may be a duplicate post) Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 3:22 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 Tim -- Another mistake I was making was that I was looking at my bytes during the data available event. Better if I close the serialport first THEN process -- don't you agree? Like this (after serial1.close rather than before) -- Code: dim data as MemoryBlock data = serial1.readall portopen = false serial1.close byte1 = data.byte(0) //moved here + fewer bytes byte2 = data.byte(1) byte3 = data.byte(2) byte4 = data.byte(3) byte5 = data.byte(4) byte6 = data.byte(5) if byte1 = 170 and byte2 = 255 then statictext1.text = str(byte3) else statictext1.text = "not found" end if if serial1.open then portopen = true end if Top timhare Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 3:26 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6882 Location: Portland, OR USA I disagree. DataAvailable is the logical place for processing, unless you have a lot of processing to do, in which case the DataAvailable code should pass the data off to either a Timer or a Thread. And I don't see why you want to close and reopen the port. I'd leave it open unless there is some compelling reason to close it, in which case the above may not apply. Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 5:51 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 OK -- Put data available in there and it's running great -- the only difference I see when I close the port and reopen is that (obviously) it slows the processing down. And I know that "somewhere" in my code I want to close and reopen the port for reliability sake? The software will be running 24/7 365 days a year so don't you think it may be a good idea somewhere to do a "turn off/turn on" for the port? So far I have this (working great) (it displays the data integer right after my 2 qualifiers) -- Code: dim data as MemoryBlock If serial1.bytesavailable >= 6 then data = serial1.readall 'portopen = false 'serial1.close byte1 = data.byte(0) byte2 = data.byte(1) byte3 = data.byte(2) byte4 = data.byte(3) byte5 = data.byte(4) byte6 = data.byte(5) end if if byte1 = 170 and byte2 = 255 then statictext1.text = str(byte3) else statictext1.text = "not found" end if 'if serial1.open then ' portopen = true 'end if Top mb52505 Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 5:51 pm Joined: Thu Oct 08, 2009 8:59 am Posts: 37 I mean "bytes available" Top timhare Post subject: Re: Out of bounds exception -- memory blockPosted: Thu Dec 03, 2009 5:58 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6882 Location: Portland, OR USA Quote:The software will be running 24/7 365 days a year so don't you think it may be a good idea somewhere to do a "turn off/turn on" for the port? Only if this is a USB/Serial adapter or something. But you'll have more issues than just opening/closing the port. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 14 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
