[Newbies] Timer in Morph

Yoshiki Ohshima yoshiki at vpri.org
Sat Feb 6 18:30:20 UTC 2010


At Fri, 5 Feb 2010 18:56:43 +0100,
Garret Raziel wrote:
> 
> and it works almost fine. But there is a bug. Time to time I receive only a part of value, for example I am receiving: 56 55
> 56 55 5 6 55 ... 102 103 101
> 103 100 102 1 1 0 102 102. Those one-digit values are truly false and it looks like the mistake in receiving. How can I
> synchonize sending and receiving? Or do I have to use other method of SerialPort? Thanks.

  It appears that data is sent in variable length format; i.e., a
value less than 10 is one byte, less than 100 is two bytes, and less
than 256 is three bytes.  If so, the receiving side cannot know truely
the beginning and end of data, unless both sides truly synchronous,
but Squeak cannot do it, especially through Morphic's #step method.

  The best way is to change the protocol so that either you send each
value in one byte, without rendering it to string, and Squeak side
read them in bulk but process them one by one.  Alternatively, you can
add an "end marker" (any character other than '0' to '9') at the end
of each data and have Squeak look for it.

  Let us say you take the first approach, things are quite simple; 
In your #step (or #readColor) method, write something like:

     | buffer value aSerialPort |
     aSerialPort := ...
     buffer := aSerialPort readByteArray.
     buffer size = 0 ifTrue: [^ self].
     s := buffer last.
     Transcript show: s; cr.
     s := s - 46 * (1/60).
     self color: (Color r:s g:s b:s).

(For changing color, doing so many times in one #step doesn't make
sense, as only the last value is visible so here you can discard other
values.)

  I didn't think you need to open the port every time you try to read
it.  But certainly it is easier to code that way.

-- Yoshiki


More information about the Beginners mailing list