Last active
January 8, 2018 22:35
-
-
Save jarrodbell/ddb49be2c01d814901f5d95f953aa460 to your computer and use it in GitHub Desktop.
Simple buffering and processing of CFLink packets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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