Here is an example that should work. I think your code above works but note that \x0D and \r are identical and you are sending 2 carriage returns with each command that does nothing except to cause extra processing on the receiver's end.
Put all this code into the Activity Script.
System.setDebugMask(9);
var socket; var buffer = ""; var CR = "\r";
function onSocketConnect() { // once connection is established, you can write anything // write is not done here as we want to send commands based on button presses. // sendCommand("!1MVLUP"); }
function onSocketData() { var temp,first; buffer+=socket.read(); // here, you have to use substring, split, or other methods to get actual messages. // assuming that each response is delineated by a carriage return \r or \x0d, you can do the following: first = buffer.indexOf(CR); while (first >= 0) { // do something with the data - extract first message up to but on including CR. temp = buffer.substring(0, first); buffer = buffer.substring(first + 1); notifyMessageReceived(temp); // find next CR. first = buffer.indexOf(CR); } }
function onSocketIOError(e) { System.print("sock IO Error error:" + e); }
function notifyMessageReceived(message) { System.print("Received:"); // message will not have the terminating \r as it was stripped of as the message delimiter. System.print(message); }
function closeSocket() { if (socket) { if (socket.connected) socket.close; socket = null; } }
function resetSocket() { // if currently connected, clean things up. closeSocket(); // create asynchronous socket. socket = new TCPSocket(false); // reset buffer clearing any data from previous socket. buffer = "";
// assign callbacks for connect, data received, and ioError. socket.onConnect = onSocketConnect; socket.onData = onSocketData; socket.onIOError = onSocketIOError; }
function sendCommand(command) { if (socket) socket.write(command + CR); }
// create a new socket and connect resetSocket(); // request connect to IP, port with a timeout socket.connect("192.168.1.6",4999,10000);
Put this code into a button you create on a page in the same activity. sendCommand("!1MVLUP");
On the same page, create a panel with ProntoScript Name of "_PS_DEBUG_" so you can see the output. See section 14.1 of the ProntoScript Dev Guide 1.4.3 from here [Link: files.remotecentral.com]