All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.epics.pvmanager.sample.MultipleChannelExamples Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager.sample;

import java.util.HashMap;
import java.util.Map;
import static org.epics.pvmanager.ExpressionLanguage.*;
import org.epics.pvmanager.PV;
import org.epics.pvmanager.PVManager;
import org.epics.pvmanager.PVReader;
import org.epics.pvmanager.PVReaderEvent;
import org.epics.pvmanager.PVReaderListener;
import org.epics.pvmanager.PVWriter;
import static org.epics.util.time.TimeDuration.*;

/**
 * This is the code from the examples in the docs, to make sure it
 * actually compiles
 *
 * @author carcassi
 */
public class MultipleChannelExamples {
    
    public void m1_readMultipleChannels() {
        // Read a map with the channels named "one", "two" and "three"
        PVReader> pvReader = PVManager
            .read(mapOf(latestValueOf(channels("one", "two", "three"))))
            .readListener(new PVReaderListener>() {
                @Override
                public void pvChanged(PVReaderEvent> event) {
                    // Print the values if any
                    Map map = event.getPvReader().getValue();
                    if (map != null) {
                        System.out.println("one: " + map.get("one") +
                                " - two: " + map.get("two") + 
                                " - three: " + map.get("three"));
                    }
                }
            })
            .maxRate(ofMillis(100));
        
        // Remember to close
        pvReader.close();
        
        // Note that when using a composite datasource, the channels can be
        //from different sources (e.g. "sim://noise" and "ca://mypv"). 
    }
    
    public void m2_readMultipleChannels() {
        // Write a map to the channels named "one", "two" and "three"
        PVWriter> pvWriter = PVManager
                .write(mapOf(channels("one", "two", "three")))
                .async();
        
        // Prepare the 3 values
        Map values = new HashMap();
        values.put("one", 1.0);
        values.put("two", 2.0);
        values.put("three", "run");
        
        // Write
        pvWriter.write(values);
        
        // Remember to close
        pvWriter.close();
        
        // Note that when using a composite datasource, the channels can be
        //from different sources (e.g. "sim://noise" and "ca://mypv"). 
    }
    
    public void m3_readWriteMultipleChannels() {
        // Read and write a map to the channels named "one", "two" and "three"
        PV, Map> pv = PVManager
            .readAndWrite(mapOf(latestValueOf(channels("one", "two", "three"))))
            .asynchWriteAndMaxReadRate(ofMillis(100));
        
        // Do something
        // ...
        
        // Remember to close
        pv.close();
    }
    
    public void m4_renameChannels() {
        // Read a map with the channels "one", "two" and "three"
        // reffered in the map as "setpoint", "readback" and "difference"
        PVReader> pvReader = PVManager
            .read(mapOf(latestValueOf(channel("one").as("setpoint")
                         .and(channel("two").as("readback"))
                         .and(channel("three").as("difference")))))
            .readListener(new PVReaderListener>() {
                @Override
                public void pvChanged(PVReaderEvent> event) {
                    // Print the values if any
                    Map map = event.getPvReader().getValue();
                    if (map != null) {
                        System.out.println("setpoint: " + map.get("setpoint") +
                                " - readback: " + map.get("readback") + 
                                " - difference: " + map.get("difference"));
                    }
                }
            })
            .maxRate(ofMillis(100));
        
        // Remember to close
        pvReader.close();
        
        // Any expression however created can be renamed.
    }
    
    public void m5_writeOrdering() {
        // Write a map to the channels named "one", "two" and "three"
        // Write "two" after "one" and write "three" after "two"
        PVWriter> pvWriter = PVManager.write(
                mapOf(channel("one")
                      .and(channel("two").after("one"))
                      .and(channel("three").after("two")))).async();
        
        // Do something
        // ...
        
        // Remember to close
        pvWriter.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy