Okay, so I kinda followed what you have there, expounding on what I had started previously. Now I've written a small function to grab the incoming data and pop the information to a buffer that I can check at any time.
var mdmBuffer = ""; //Main storage buffer
var mdmURCBuffer = ""; //URC Buffer
var mdmParsedBuffer = ""; //AT Response buffer
Serial6.on('data', function (data) {
//This event grabs incoming data and concatenates it into a buffer
//The buffer is scanned for identifiable modem AT responses and handles URCs.
//This assumes a regular cleanup of the main buffer in your flow.
var idx1 = -1;
var idx2 = -1;
mdmBuffer += data;
//Scan for common AT responses, need to search for these as
//sometimes you can get multiple CRLF and throw off other checks
if (mdmBuffer.indexOf("#")>=0 ||
mdmBuffer.indexOf("+")>=0 ||
mdmBuffer.indexOf("$")>=0 ||
mdmBuffer.indexOf(">")>=0 ||
mdmBuffer.indexOf("ERROR")>=0 ||
mdmBuffer.indexOf("OK")>=0) {
//Found one of these
if (mdmBuffer === "\r\nOK\r\n") {
//Straight up OK response, no other return
mdmParsedBuffer = "OK";
}
if (mdmBuffer === "\r\nERROR\r\n") {
//Straight up ERROR response, no other return
mdmParsedBuffer = "ERROR";
}
if (mdmBuffer.indexOf("#")>=0 ||
mdmBuffer.indexOf("+")>=0 ||
mdmBuffer.indexOf("$")>=0) {
//Found one of these
//Could be a URC or an AT response
//Put these to a buffer we can call upon at runtime or service through
//a set interval. If we print these immediately they may not be read/full.
if (mdmBuffer.indexOf("OK")>=0) {
//If there's an OK in here, it's not a URC
mdmParsedBuffer = mdmParseResponse(mdmBuffer);
}
else mdmURCBuffer = mdmParseResponse(mdmBuffer);
}
}
});
function mdmParseResponse(inSTR) {
//This function parses out the responses to readable format
//It recognizes non-AT style responses by the \r\n and just returns them.
if (inSTR !== -1 && inSTR.length > 0) {
rtnSTR= inSTR;
if (inSTR.indexOf('\r\n',0) !== -1) {
splitList = inSTR.split('\r\n');
rtnSTR = splitList[1];
}
}
return rtnSTR;
}
and some little helper functions
function mdmClearBuffer() {
//Clears the main buffer
mdmBuffer = "";
}
function mdmPrintBuffers() {
//Prints out the main buffers to console
console.log("mdmBuffer = " + mdmBuffer);
console.log("mdmURCBuffer = " + mdmURCBuffer);
console.log("mdmParsedBuffer = " + mdmParsedBuffer);
}
function mdmSendAT(data) {
Serial6.println(data);
}
So overall it's similar to what I had before, just done more automatic and doesn't require calling a receive/parsing function separately, which is nice.
Now the question is how do I actually take this further and if it's even possible, as in, is it possible to automate something that I want done. I'm trying to wrap my head around what normally gets done for this and it's getting a little muddy.
An example would be something simple like reading an I/O and sending an SMS upon a desired state. I feel like I'm going to be creating a bunch of little functions, and a larger function that gets an interval set and passed the desired small function to run, so that it can run the network checking and whatnot when required.
Hmm, I feel like this should be moved to the projects/JS area so it's not muddying up the General area.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Okay, so I kinda followed what you have there, expounding on what I had started previously. Now I've written a small function to grab the incoming data and pop the information to a buffer that I can check at any time.
and some little helper functions
Output
So overall it's similar to what I had before, just done more automatic and doesn't require calling a receive/parsing function separately, which is nice.
Now the question is how do I actually take this further and if it's even possible, as in, is it possible to automate something that I want done. I'm trying to wrap my head around what normally gets done for this and it's getting a little muddy.
An example would be something simple like reading an I/O and sending an SMS upon a desired state. I feel like I'm going to be creating a bunch of little functions, and a larger function that gets an interval set and passed the desired small function to run, so that it can run the network checking and whatnot when required.
Hmm, I feel like this should be moved to the projects/JS area so it's not muddying up the General area.