5. Code sample
5. CODE SAMPLE
5.1 Java code sample
Overview
This code sample in Java c ontains a small program that will connect to a projector and send the comman
d to switch the lamps on
(lamp status, write).
Java code
/*
* SimpleConnect.java
*
* Copyright 2008 Barco NV. All rights reserved.
* BARCO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.barco.connect;
import java.io.*;
import java.net.Socket;
/**
* Sample code shows how to connect to a projector and switch the lamps on.
*/
public class SimpleConnect
{
private static final int PORT = 0xAAA0;
private Socket connection;
private OutputStream out;
private InputStream in;
public static void main(String[] args)
{
SimpleConnect sc = new SimpleConnect();
try
{
// make connection with projector
sc.connect("192.168.100.1");
// send command to switch lamps on
byte[] arr = new byte[9];
arr[1] = 0x00; // device address
arr[2] = 0x00; // answer prefix (1)
arr[3] = 0x03; // answer prefix (2)
arr[4] = 0x02; // answer prefix data (send result)
arr[5] = 0x76; // command byte (1)
arr[6] = 0x1A; // command byte (2)
arr[7] = 0x01; // data byte (0x01 = switch lamps on)
arr[8] = sc.generateChecksum(arr);
arr[0] = (byte) 0xFE;
arr[9] = (byte) 0xFF;
// write request
sc.writeBytes(arr);
System.out.println("Sent command to switch lamps on");
// wait for acknowledgement
if (!sc.readAcknowledge())
{
System.out.println("Command not ACK’ed");
return;
}
System.out.println("Command acknowledged.");
// wait for answer result
int result = sc.readAnswerResult();
System.out.println("Command result="+result);
if (result == 0x01)
{
System.out.println("Lamps successfully switched on.");
}
} catch (IOException e)
{
System.err.println("Communication error: " + e.getMessage());
} finally
{
MED20080612 COMMUNICATING WITH A BARCO PROJECTOR OVER NETWORK 12/06/2008
13