From: Samuele P. <pe...@in...> - 2001-04-05 18:05:39
|
Hi. [Marc Koch] > Please help, ... what am I missing?!? something unrelated to serial port usage in java I think > class MySerialPortEventListener (comm.SerialPortEventListener): > def serialEvent (self, Event): > print Event > try: > IBuf = '--------' This assign a python string to IBuf (note: python string are immutable they cannot be used as buffers <wink>) ... > ILen = IStream.available() > while ILen > 0: > INum = IStream.read(IBuf) read take a byte[], now python strings are actually automatically converted to byte[] if they are passed to a method that takes such an argument but this conversion is done but making a copy of the string (to respect immutability), java has no const or in or out modifiers for arguments so jython in any case here could not detect that something wrong is possibly going on, he just tries to make its best creating a byte[] array containing just a copy of the string > print 'Read: OK', ILen, INum, IBuf the copied byte[] is probably modified in the right way, but IBuf is still a reference to the old string so ... > ILen = IStream.available() > except: > raise Solution: the jython idiom for allocating a mutable array that can be passed to java methods wanting a writeable buffer is: import jarray buf = jarray.zeros(<length>,'b') # 'b' stands for bytes # <length> is an expr ... For further details see the doc: http://www.jython.org/docs/jarray.html online or the same in your jython installation tree. regards. |