# AMP Protocol - receiving the response

**URL:** <https://rossvideo.community/t/amp-protocol-receiving-the-response/8409>\
**Category:** Automation & Control\
**Created:** [March 18, 2020, 9:43pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409 "2020-03-18T21:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonlatus](https://rossvideo.community/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@simonlatus](https://rossvideo.community/u/simonlatus)\
**Post date:** [March 18, 2020, 9:43pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409/1 "2020-03-18T21:43:00Z")

</div>

Hi

I am writing an AMP protocol control UI in Dashboard. I can see how to send the binary bytes using writeAsBytes(myByteArray).

What I can’t see is how to receive the variable length response. If I set the listener to have a fixed length of 1 byte, I receive an event for each byte in the response. Problem is I don’t know when there are no more bytes to read. Is there a way to get at the underlying socket object to probe the receive buffer?

I’m currently using event.getBytes(). I was hoping calling that until I received no more data would work, but it just return the same 1 byte (my listener is set to return 1 byte per message event).

Thanks

Simon

---

<div class="post-metadata">

**Author:** ![jamespeltzer](https://rossvideo.community/user_avatar/rossvideo.community/jamespeltzer/32/50_2.png) [@jamespeltzer](https://rossvideo.community/u/jamespeltzer)\
**Post date:** [March 18, 2020, 9:50pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409/2 "2020-03-18T21:50:00Z")

</div>

Hi Simon

The key to this is to select “Stop all receive/task execution on pause” for the listener and use ogscript.createMessageParser(this.getInputStream()) inside of your listener’s code. This will allow you to read directly from the wire instead of waiting for messages to be complete:

 function sendMessageToListener(type, message) { var mb = ogscript.createMessageBuilder(); mb.writeShort(type); mb.writeShort(message.length); mb.writeString(message); rosstalk.sendBytes('localhost', 2244, mb.toByteArray(), null); } if (event.isMessageEvent()) { var cmdTypeBytes = event.getBytes(); var commandType = cmdTypeBytes[0] \<\< 8 | cmdTypeBytes[1]; var messageParser = ogscript.createMessageParser(this.getInputStream()); var length = messageParser.readShort(); var string = messageParser.readString(length);

ogscript.rename(‘output’, 'TYPE: ’ + commandType + ’ MESSAGE: ’ + string);  
}

 sendMessageToListener(1, params.getValue('Message\_Type\_1', 0)); sendMessageToListener(2, params.getValue('Message\_Type\_2', 0)); [#DashBoard](https://livinglive.community/search?s=tags%3A%22DashBoard%22&executesearch=true)

---

<div class="post-metadata">

**Author:** ![jamespeltzer](https://rossvideo.community/user_avatar/rossvideo.community/jamespeltzer/32/50_2.png) [@jamespeltzer](https://rossvideo.community/u/jamespeltzer)\
**Post date:** [March 18, 2020, 9:51pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409/3 "2020-03-18T21:51:00Z")

</div>

Also, for mixing Binary and ASCII, the list of available methods on the message builder/parser is available in the script palette:

var messageBuilder = ogscript.createMessageBuilder();  
/\*

- messageBuilder.writeByteArray(bytesArray);
- messageBuilder.writeByteArray(bytesArray, offset, length);
- messageBuilder.writeBoolean(boolVal);
- messageBuilder.writeByte(byteVal);
- messageBuilder.writeShort(shortVal);
- messageBuilder.writeChar(charVal);
- messageBuilder.writeInt(intVal);
- messageBuilder.writeLong(longVal);
- messageBuilder.writeFloat(floatVal);
- messageBuilder.writeDouble(doubleVal);
- messageBuilder.writeLine(strVal);
- messageBuilder.writeString(strVal);
- messageBuilder.writeUTF(strVal); //includes 2-byte length count  
\*/  
var bytes = messageBuilder.toByteArray();

var messageReader = ogscript.createMessageParser(messageBytes);  
/\*

- var byteArray = messageReader.readBytes(length);
- messageReader.skipBytes(length);
- var boolVal = messageReader.readBoolean();
- var byteVal = messageReader.readByte();
- var byteVal = messageReader.readUnsignedByte();
- var shortVal = messageReader.readShort();
- var shortVal = messageReader.readUnsignedShort();
- var charVal = messageReader.readChar();
- var intVal = messageReader.readInt();
- var longVal = messageReader.readLong();
- var floatVal = messageReader.readFloat();
- var doubleVal = messageReader.readDouble();
- var str = messageReader.readUTF(); //includes 2-byte length count
- var str = messageReader.readLine(); //read bytes until newline character is reached
- var str = messageReader.readString(length); //without 2-byte length count  
\*/  
[#DashBoard](https://livinglive.community/search?s=tags%3A%22DashBoard%22&executesearch=true)

---

<div class="post-metadata">

**Author:** ![simonlatus](https://rossvideo.community/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@simonlatus](https://rossvideo.community/u/simonlatus)\
**Post date:** [March 18, 2020, 10:42pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409/4 "2020-03-18T22:42:00Z")

</div>

Thanks James. It’s almost what I needed, but gave me enough to figure the rest out! I have kept the delimiter set to 1 byte.

Here is my code now. It will allow for any length of response that I can then parse, and pass onto a messageParser if need be.

if (event.isMessageEvent())  
{  
ogscript.debug(“Dyno data”);  
var data = ;

//we need to get the first byte the normal way  
data.push(event.getBytes()[0]);  
var stream = this.getInputStream();  
ogscript.debug("Data available: " + stream.available());

//get the rest of the bytes in the message and add to our array  
while( stream.available())  
{  
data.push(stream.read());  
}

//what we got?  
for(var i = 0; i\< data.length; i++)  
{  
ogscript.debug(data[i]);  
}  
return;

}

I don’t have access to real hardware yet, just a simple TCP server, but it’s proved what I need

Thanks

[#DashBoard](https://livinglive.community/search?s=tags%3A%22DashBoard%22&executesearch=true)

---

<div class="post-metadata">

**Author:** ![alexsamih-zade](https://rossvideo.community/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@alexsamih-zade](https://rossvideo.community/u/alexsamih-zade)\
**Post date:** [October 28, 2020, 10:37pm UTC](https://rossvideo.community/t/amp-protocol-receiving-the-response/8409/5 "2020-10-28T22:37:00Z")

</div>

@Simon, this is awesome.

This code makes getting messages much easier, no need to mess with delimiter type and value. Thanks to available() method. I wonder why this is not preffered way to get any response, instead of using delimiter. But, in fact, i didn’t found info about that method, not in ogscript pdf’s, nor in that forum. Actually, your topic is uniq in that case.

Congratulations, and thanks for posting that code, i already integrated it in some panels and i think this is much easier way to get messages via listener.

Alex.

[#DashBoard](https://livinglive.community/search?s=tags%3A%22DashBoard%22&executesearch=true)
