Skip to content

Instantly share code, notes, and snippets.

@jarrodbell
Last active January 8, 2018 22:35
Show Gist options
  • Save jarrodbell/ddb49be2c01d814901f5d95f953aa460 to your computer and use it in GitHub Desktop.
Save jarrodbell/ddb49be2c01d814901f5d95f953aa460 to your computer and use it in GitHub Desktop.
Simple buffering and processing of CFLink packets
var cflinkRegex = /\xF2([\s\S])\xF3([QCTR])(\w{3})(\w{3})\xF4([\s\S]*?)\xF5\xF5/g; // Capture groups: ID, Command Type, Device, Command, Data
var rcvBuffer;
function incomingData(fbName, rcv) {
// Append new incoming data to buffer
rcvBuffer += rcv;
var matches;
var matchedData = "";
// Loop through any complete CFLink packets
while ((matches = cflinkRegex.exec(rcvBuffer)) !== null) {
// Convert ID to correct format 00-FF
matches[1] = ("0"+matches[1].charCodeAt(0).toString(16)).slice(-2).toUpperCase();
// Use the matches array here to do whatever you need...
// Append the processed packets here to remove from the buffer
matchedData += matches[0];
}
// Reset the regex index
cflinkRegex.lastIndex = 0;
// Remove the processed packets from the buffer
rcvBuffer = rcvBuffer.replace(matchedData, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment