Receiving data via serial port 
Author Message
 Receiving data via serial port

(I did the obligatory google & groups.google search and couldn't find
the answers I was looking for. Apologies is this has been answered
before).

Hi all,

I am using RB 4.5 on a Pismo 400 (Mac OS X 10.2.6, 384 MB ram) to
receive binary data from a serial device. The device continuously spits
out binary data in 70-byte chunks at 9600 baud 8/N/1. My RB program
receives a data chunk, decodes the data, spits the data out into a 30
column listbox, looks for the next chunk, etc.

My problem: the program seems to 'drop' a few of the 70-byte chunks.
I'll get 3 or 4 chunks, a partial chunk, skip a chunk, then 3 or 4
chunks, etc. It's not very consistent - sometimes I'll get 20 chunks
before the program drops anything, sometimes I'll get 2 or 3 partial
chunks before I get a get a complete chunk. The skips seem to be
independent of processor load.

I'm reading the serial data into a memoryblock using the readall method,
although I've tried reading bytes() with no success.

Does anyone have any suggestions? I'm wondering if the RB program is
having a problem reading the data and updating the listbox at the same
time, although I have tried reading the data into a variable and filling
in the listbox once the data collection has stopped and that hasn't
helped.  

Code:

DataAvailable:
dim m as memoryblock
m.cstring(0)=me.readall
 window1.databox.addrow str(m.byte(0)) ' L
 window1.databox.cell(window1.databox.lastindex,1)=str(m.byte(1))
 window1.databox.cell(window1.databox.lastindex,2)=str(m.byte(2))
 window1.databox.cell(window1.databox.lastindex,3)=str(m.byte(3))
 window1.databox.cell(window1.databox.lastindex,4)=str(m.byte(4))
 window1.databox.cell(window1.databox.lastindex,5)=str(m.byte(5))

 count=5
 for i=6 to m.byte(4)+1 step 2
   count=count+1
 window1.databox.cell(window1.databox.lastindex,count)=str(m.short(i)/10)
 next
--
     Herb Singleton

     Sound & Vibration Measurements
      http://www.*-*-*.com/



Wed, 14 Dec 2005 10:39:05 GMT  
 Receiving data via serial port
Hi Herb,

That certainly ought to work; I'm not sure why it doesn't, so I'll just
grasp at some straws here...

Try doing your ReadAll (in the DataAvailable event) in a loop, until it
returns the empty string.  I'm thinking that perhaps in the time between
when you do the first one, and when you're done processing it, more data
gets read in but your DataAvailable event doesn't fire again because
you're already in it.

Umm.... OK, just one straw.  I hope it helps!

- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout         Check out the Mac Web Directory:     |

`------------------------------------------------------------------'



Wed, 14 Dec 2005 11:56:43 GMT  
 Receiving data via serial port
Here is some of my code I used to learn how to setup and read/write to
a serial port. I had some simular problems before creating a thread and
a timer to do the readall function. After implementing this, it
appeared to go away and I would get nice even data flow. I created this
from examples in the RB listserv archives.

Window1.Open:
Sub Open()
  dim x as integer
  For x=1 to system.serialportcount
    ListBox1.AddRow system.serialport(x-1).Name
  Next
End Sub

Window1.ListBox1.DoubleClick:
Sub DoubleClick()
  dim x as integer
  Serial1.Close
  Serial1.SerialPort = System.SerialPort(ListBox1.ListIndex) //assign
the port
  If Serial1.Open then
    MsgBox "The "+ListBox1.text+" was opened successfully."
  Else
    MsgBox "The "+ListBox1.text+" could not be opened."
  End if
End Sub

Window1.PushButton1.Action:
Sub Action()
  Serial1.Write EditField2.Text + Chr(13)
  EditField1.SelStart=Len(EditField1.Text) //advances the cursor
  EditField1.SelText= Serial1.ReadAll() //reads the text
End Sub

Window1.Timer1.Action:
Sub Action()
  dim d as DataAvailableThread
  If LenB(Serial1.LookAhead) > 2 then // is there more than 2 bytes of
data in the buffer
     d= New DataAvailableThread // if yes go and get it
    d.run
  End if
End Sub

DataAvailableThread.Run:
Sub Run()
  Window1.EditField1.SelText= Window1.Serial1.ReadAll() //reads the text
End Sub


Quote:

> (I did the obligatory google & groups.google search and couldn't find
> the answers I was looking for. Apologies is this has been answered
> before).

> Hi all,

> I am using RB 4.5 on a Pismo 400 (Mac OS X 10.2.6, 384 MB ram) to
> receive binary data from a serial device. The device continuously spits
> out binary data in 70-byte chunks at 9600 baud 8/N/1. My RB program
> receives a data chunk, decodes the data, spits the data out into a 30
> column listbox, looks for the next chunk, etc.

> My problem: the program seems to 'drop' a few of the 70-byte chunks.
> I'll get 3 or 4 chunks, a partial chunk, skip a chunk, then 3 or 4
> chunks, etc. It's not very consistent - sometimes I'll get 20 chunks
> before the program drops anything, sometimes I'll get 2 or 3 partial
> chunks before I get a get a complete chunk. The skips seem to be
> independent of processor load.

> I'm reading the serial data into a memoryblock using the readall method,
> although I've tried reading bytes() with no success.

> Does anyone have any suggestions? I'm wondering if the RB program is
> having a problem reading the data and updating the listbox at the same
> time, although I have tried reading the data into a variable and filling
> in the listbox once the data collection has stopped and that hasn't
> helped.  

> Code:

> DataAvailable:
> dim m as memoryblock
> m.cstring(0)=me.readall
>  window1.databox.addrow str(m.byte(0)) ' L
>  window1.databox.cell(window1.databox.lastindex,1)=str(m.byte(1))
>  window1.databox.cell(window1.databox.lastindex,2)=str(m.byte(2))
>  window1.databox.cell(window1.databox.lastindex,3)=str(m.byte(3))
>  window1.databox.cell(window1.databox.lastindex,4)=str(m.byte(4))
>  window1.databox.cell(window1.databox.lastindex,5)=str(m.byte(5))

>  count=5
>  for i=6 to m.byte(4)+1 step 2
>    count=count+1
>  window1.databox.cell(window1.databox.lastindex,count)=str(m.short(i)/10)
>  next



Wed, 14 Dec 2005 21:02:39 GMT  
 Receiving data via serial port
Joe & Jerry:

Thanks guys, I'll try your suggestions and let you know how I make out.

Herb



Thu, 15 Dec 2005 02:47:58 GMT  
 Receiving data via serial port


Quote:
> Here is some of my code I used to learn how to setup and read/write to
> a serial port. I had some simular problems before creating a thread and
> a timer to do the readall function. After implementing this, it
> appeared to go away and I would get nice even data flow. I created this
> from examples in the RB listserv archives.

I took your suggestion and built a thread to read the data on the serial
port.

My problem now is that the program works great when run under the
de{*filter*}, but the built application sutters worse then before. I'm
experimenting with the timer period to see if that helps.

Herb

--
     Herb Singleton

     Sound & Vibration Measurements
      http://www.*-*-*.com/



Thu, 15 Dec 2005 11:36:51 GMT  
 Receiving data via serial port
You might even play with the number of byte. In the example it is 2 or
more bytes available for the thread to look for. If your messages from
the device are a fixed amount, and I am just shooting in the dark here,
you might change it to the size of the message. But then again, I am
just a newby at this whole thing.

My project is to build a control interface for an AOR AR8600MkII
wideband receiver ( http://www.*-*-*.com/ ), Not sure whether to build the
interface to look like the radio front panel or spread out the
functionality to a generic interface. Both avenues have their tricky
parts which I am still trying to learn and work them into an interface
concept. Messages from the receiver are of varying length and trying to
think of a way to handle them.

It's going to be slow, but I'll get there some day....

Jerry


Quote:



> > Here is some of my code I used to learn how to setup and read/write to
> > a serial port. I had some simular problems before creating a thread and
> > a timer to do the readall function. After implementing this, it
> > appeared to go away and I would get nice even data flow. I created this
> > from examples in the RB listserv archives.

> I took your suggestion and built a thread to read the data on the serial
> port.

> My problem now is that the program works great when run under the
> de{*filter*}, but the built application sutters worse then before. I'm
> experimenting with the timer period to see if that helps.

> Herb



Thu, 15 Dec 2005 22:06:50 GMT  
 Receiving data via serial port


Quote:
> You might even play with the number of byte. In the example it is 2 or
> more bytes available for the thread to look for. If your messages from
> the device are a fixed amount, and I am just shooting in the dark here,
> you might change it to the size of the message. But then again, I am
> just a newby at this whole thing.

I got the built application to work by fiddleing around with the timer
period. Next: getting it work with Windws :)

--
     Herb Singleton

     Sound & Vibration Measurements
     http://www.cross-spectrum.com



Sun, 18 Dec 2005 05:11:31 GMT  
 Receiving data via serial port
Great! Hope it gives you another direction on which to attack the
problem you described. Best of luck!

Jerry


Quote:



> > You might even play with the number of byte. In the example it is 2 or
> > more bytes available for the thread to look for. If your messages from
> > the device are a fixed amount, and I am just shooting in the dark here,
> > you might change it to the size of the message. But then again, I am
> > just a newby at this whole thing.

> I got the built application to work by fiddleing around with the timer
> period. Next: getting it work with Windws :)



Sun, 18 Dec 2005 05:20:10 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Problem to received data via serial port (again)

2. data acquisition via serial port

3. Data acquisition via serial port (COM)

4. REQUEST: Serial port code for processing serial mouse data

5. sending and receiving from serial Port Com1 Com2

6. serial port buffer receive overflow????

7. Error 37 received when initializing serial port 9.

8. receiving binary data with a serial control?

9. Receiving Data From Comm Port

10. Command to receive data from port

11. Access serial (via USB) port in RealBasic - example source wanted

12. MONITORING SYSTEM VIA SERIAL PORT

 

 
Powered by phpBB® Forum Software