Login  Register

Re: Problem sending unsigned byte as string via "Serial Macro Extensions"

Posted by Michael Schmid on Jul 28, 2010; 5:19pm
URL: http://imagej.273.s1.nabble.com/Problem-sending-unsigned-byte-as-string-via-Serial-Macro-Extensions-tp3687446p3687447.html

Hi Graeme,

serial_ext.java uses String.getBytes() to convert the String into a  
sequence of bytes. From the Java Website:

public byte[] getBytes()
Encodes this String into a sequence of bytes using the platform's  
default charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the  
default charset is unspecified.
So I would suggest that you change the code in serial_ext.java,  
roughly like this:

public void write(String s) {
     byte[] bytes = new byte[s.length()];
     for (i=0; i<s.length(); i++)
         bytes[i] = (byte)(s.charAt(i));
     write(bytes);
}


Michael
________________________________________________________________

On 28 Jul 2010, at 18:08, Graeme Awcock wrote:

> I have been trying to output binary data (just an 8-bit unsigned  
> character)
> from the ImageJ macro language in a form that can be sent over a  
> serial
> link. I want to develop hardware that will use the data byte to  
> control a
> DAC, for example. The problem is that a range of 16 values are being
> filtered out and replaced by another value…
> I am using the "Serial Macro Extensions" as provided on the  
> ImageJWIKI to
> control the serial port.
> Mostly this works well, but something in the chain is 'filtering'  
> out values
> in the range 80-9F hex. Instead the serial data in that range is  
> forced to
> 3F hex (confirmed by hardware testing).
> I am aware that this range of binary values corresponds to a block of
> control codes in Unicode/ASCII, so this seems like a likely  
> motivation for
> the problem, but I need to get around it because I obviously want  
> to be able
> to send all 256 possible values for the byte over the serial link.
> I am using the built-in ImageJ macro function "fromCharCode(value)" to
> convert 'value' from hex to a string character. I do not know  
> whether this
> function is filtering the characters 80-9F hex, or if it is  
> happening within
> the serial macro extension code.
> Can anyone help. please?