channelaccess / ca_matlab

4 stars 2 forks source link

How to write strings? #11

Closed matlabuser closed 8 years ago

matlabuser commented 8 years ago

I am looking for an example of how to write/put strings.

Reading strings does work: a = channel.get() a = java.lang.String[]: 'CLOSED_ORBIT.txt'

However, when trying to write to the same channel an error is returned. channel.put('filename.txt') Java exception occurred: java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String; at ch.psi.jcae.impl.handler.StringArrayHandler.setValue(StringArrayHandler.java:20) at ch.psi.jcae.impl.DefaultChannel.setValueAsync(DefaultChannel.java:221) at ch.psi.jcae.impl.DefaultChannel.setValue(DefaultChannel.java:195) at ch.psi.jcae.impl.De

Typecasting the argument, i.e. java.lang.String('filename.txt'), doesn't help.

simongregorebner commented 8 years ago

According to what I see your channel seams to be a String Array channel. Try to use

channel.put(new java.lang.String[]{'filename.txt'})

If this does not work, can you please give me more information about the channel you try to set (DBR type, etc.).

matlabuser commented 8 years ago

Thanks for your immediate response. Here the result:

>>channel.put(new java.lang.String[]{'filename.txt'})
 channel.put(new java.lang.String[]{'filename.txt'})
                 |
Error: Unexpected MATLAB expression.

Alternatively using >>channel.put(java.lang.String('filename.txt')) results in the exact same error message as in my initial post.

PV Information

QDR:FILE-QDR-NAME
======================================
DESC: 
RTYP: stringin
TYPE: DBF_STRING
COUNT: 1
ACCESS: RW
IOC: xxx
VALUE: CLOSE_ORBIT.txt
ALARM: NO
simongregorebner commented 8 years ago

Sorry, a (Java) String array in Matlab is not created as I wrote. Can you please try this?

value = javaArray('java.lang.String', 1)
value(1) = java.lang.String('filename.txt');
channel.put(value)

Also, could you please post the code where you create the channel, eventually you can change something there to avoid having to pass a String array to the put method.

matlabuser commented 8 years ago

The above solution works. Thank you. The code that generates the channel is:

javaaddpath('_cafe\ca_matlab-1.0.0.jar')
import ch.psi.jcae.*
properties = java.util.Properties();
context = Context(properties);
channel = Channels.create(context, ChannelDescriptor('string[]','QDR:FILE-QDR-NAME'));
simongregorebner commented 8 years ago

As your channel is a String channel you ideally create the channels as follows:

javaaddpath('_cafe\ca_matlab-1.0.0.jar')
import ch.psi.jcae.*
properties = java.util.Properties();
context = Context(properties);
channel = Channels.create(context, ChannelDescriptor('string','QDR:FILE-QDR-NAME'));

Note: Type 'string' instead of 'string[]'

While doing so you immediately get a String while calling get() and you are able to set the String via channel.put('filename.txt')