2 communication using java applet – SENA HD1100 User Manual

Page 37

Advertising
background image

Starter Kit and User Manual for the HelloDevice 1100

34

commandBuf[0] = IOSet ;

commandBuf[1] = (BYTE) ((IOStatus[2] >> 8) & 0x000000ff) ;

commandBuf[2] = (BYTE) (IOStatus[3] & 0x000000ff) ;

commandLen = 3 ;


// Send command to HelloDevice

err = sendto

(

sock,

&commandBuf,

commandLen,

0,

(struct sockaddr*)&clientAddr,

sizeof(clientAddr)

) ;

if (err == -1 )

{

perror("\nsend error\n");

exit (1);

}

}

6.3.2 Communication Using Java Applet

The program source code for a Java applet (IOdemo.java, IOComm.java, Led.java and OutButton.java) can be

found at the “…\Source\java” subdirectory of the HelloDevice installation folder. The users can build his own

Web-based remote I/O control by inserting this Java applet in to their Web page.

The following Java program source shows an example implementing the Internet socket interface using a Java

applet that sends and receives I/O control signals through the TCP port number 6001 of the HelloDevice.

/* Output read */
public int readValueTCP() {
Socket socketTCP = null;
int tmp = 0;
int inputData = 0;
byte rxData[] = new byte[16];
byte data[] = {COMMAND_GET};
try {

socketTCP = new Socket(InetAddress.getByName(m_host), m_port);

socketTCP.setTcpNoDelay(true);

socketTCP.getOutputStream().write(data, 0, data.length);

instream = new DataInputStream(socketTCP.getInputStream());

tmp = instream.read(rxData, 0, rxData.length);

if (tmp != -1) {

inputData = (int) (rxData[2] << 8 | (rxData[3] & 0x00ff));

inputData &= 0xffff;

}

socketTCP.close();

instream.close();

} catch (Exception e) {

System.out.println("Err : " + e);

}

return inputData;

}

/* Output write */
public void wirteValueTCP(int outdata) {
Socket socketTCP = null;
byte[] data = new byte[4];

Advertising