New topic: 

another serial dataavailable question

<http://forums.realsoftware.com/viewtopic.php?t=32240>

       Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic         Author  
Message       arnoldino           Post subject: another serial dataavailable 
questionPosted: Fri Jan 29, 2010 1:12 am                        
Joined: Tue Oct 31, 2006 2:57 am
Posts: 69              hello,

i know this question was asked many times before, but i can't get my code 
working perfectly.
long strings doesn't come through. in average 4-5 times it gives me the right 
string, form 10 tries.

i don't know the string's length to be read, and i was trying this code:

Code:
dim m as MemoryBlock
dim c as Boolean=true
buff=""
do
  dim count As Integer
  // Attempt to process some data
  count  = len(me.LookAhead)
  // If we have a full packet, then count will be > 0
  if count > 0 then
  me.Poll
  buff=buff+me.Read( count )
  c=True
  else
  // We didn't process an entire packet, so we want
  // to bail out
  app.SleepCurrentThread(100)
  c=false
  end if
  
loop until  me.BytesAvailable<=0

if c Then
  ...
end if



i presume i'm missing something obvious.   
                            Top               timhare           Post subject: 
Re: another serial dataavailable questionPosted: Fri Jan 29, 2010 1:30 am       
                 
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 7182
Location: Portland, OR  USA              First of all, you shouldn't be looping 
inside DataAvailable.  If you don't get the entire string in one event, another 
event will fire when more comes in, so just wait.  And you should never Poll 
within the event.

If you don't know the length of string to expect, how do you know when you have 
the full string?  Do you expect an EndOfLine following the string?  Or some 
other delimiter?  What does the data look like?

Tim   
                            Top               arnoldino           Post subject: 
Re: another serial dataavailable questionPosted: Fri Jan 29, 2010 1:49 am       
                 
Joined: Tue Oct 31, 2006 2:57 am
Posts: 69              a strings should come in one wave, without any 
endofstring character. i should use one?
i tried with realterm software to send out the reading command and the device 
gives me back the correct string, anytime i try.

my code returns the string correct sometimes. sometimes not.

let me describe the hole situation

i have a window, which on open event starts a timer. the timer every 500 ms 
sends reading command for different attributes.
500 ms -> readDate
1000 ms -> readSerialNumber
1500 ms -> readLicenceNumber
..

for a read command the device gives back the readcommand and the data:
e.g ddddddddddddddd 

the first character is the command in ascii display and the other characters 
are the data. the data's lenght can be 0 to 64.

in the serial's DA event i have a celect case which looks the first byte and 
decides hoe to process the data.

the code in my earlier post is put in the DA event, after i observed that the 
strings are not complete.
using this code i achieved that sometimes it reads the correct string, 
sometimes no.


so do you suggest that i should use delimiter characters to detect the end of 
string? the lookahead.len is not enough? it makes sense, but where to put the 
loop?   
                            Top               timhare           Post subject: 
Re: another serial dataavailable questionPosted: Fri Jan 29, 2010 2:29 am       
                 
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 7182
Location: Portland, OR  USA              There is no loop.  Ever.  And yes, you 
should use some kind of delimiter and/or length indicator to tell you how much 
data to expect.  Or when you have all the data.

Each DataAvailable will have:
a) an entire response
b) part of a response
c) part of one response and part of another

Any given transmission from the device on the other end of the serial port 
could arrive in two or more DataAvailable events.  In the first event, if you 
determine you do not have an entire transmission, just return.  The next event 
(or one after it) will have the entire string.   
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 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]

Reply via email to