Created
September 7, 2021 16:52
-
-
Save netskink/b2bcce6c6f08e832cb955bd1762822f6 to your computer and use it in GitHub Desktop.
mkrnb1500.ino get serial buffer to empty?
This file contains 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
/* | |
This code clears the u-blox on a MKR NB 1500 of all certificates. | |
*/ | |
// baud rate used for both Serial ports | |
unsigned long baud = 115200; | |
// Must be large enough to capture all | |
// output. Some responses are multiple lines | |
// | |
// Notes on Serial class/API: | |
// o The serial class maintains a 64 byte buffer internally | |
// o read() reads one byte at a time | |
//char pchBuffer[64]; | |
char pchBuffer[2048]; | |
unsigned long milliseconds = 1L; // NOTE: usage of L | |
unsigned long seconds = 1000L; // NOTE: usage of L | |
unsigned long minutes = seconds * 60; | |
unsigned long hours = minutes * 60; | |
void setup() { | |
// NEVER EVER use RESET_N | |
pinMode(SARA_RESETN, OUTPUT); | |
digitalWrite(SARA_RESETN, LOW); | |
// Send Poweron pulse | |
pinMode(SARA_PWR_ON, OUTPUT); | |
digitalWrite(SARA_PWR_ON, HIGH); | |
delay(150); | |
digitalWrite(SARA_PWR_ON, LOW); | |
Serial.begin(baud); | |
SerialSARA.begin(baud); | |
// To avoid a wait for serial, just | |
// delay long enough for a serial port to | |
// be attached. | |
for(int i=0; i<40; i++) { | |
Serial.print("X"); | |
delay(150); | |
} | |
Serial.println(""); | |
} | |
void write_cmd(char *pchBuffer) { | |
int str_len; | |
str_len = strlen(pchBuffer); | |
// Write the command to the part | |
while (SerialSARA.availableForWrite() < str_len) { | |
delay(500); | |
} | |
// write to the part | |
SerialSARA.write(pchBuffer); | |
} | |
// buffer is returned in provided buffer | |
// return code is the number of bytes valid | |
// in buffer. | |
int read_resp(char *resp) { | |
char result; | |
int index=0; | |
// truncate string to start fresh | |
resp[0]=0; | |
Serial.print("how much is available? "); | |
Serial.println(SerialSARA.available()); | |
// Read the results a byte at a time | |
while (SerialSARA.available()) { | |
result = SerialSARA.read(); | |
resp[index++]=result; | |
} | |
//SerialSARA.flush(); | |
return index; | |
} | |
int read_resp2(char *resp) { | |
char result; | |
int num_read=0; | |
int num_avail; | |
// truncate string to start fresh | |
resp[0]=0; | |
num_avail = SerialSARA.available(); | |
Serial.print("how much is available? "); | |
Serial.println(num_avail); | |
num_read = SerialSARA.readBytesUntil('\n', resp, 100); | |
return num_read; | |
} | |
/* | |
* Notes on the AT+USECMNG command | |
* | |
* https://www.u-blox.com/en/docs/UBX-17003787 | |
* | |
* Section 20.3 Data Security | |
* Section 20.3.2 SSL/TLS certificates and private keys manager +USECMNG | |
* | |
* AT+USECMNG=<op code>,[type],[internal name],[param1],[param2] | |
* | |
* opcode purpose | |
* 0 import via serial IO | |
* 1 import via file on FS | |
* 2 remove cert/key 2,<type 0=cert, 1=key>,<internal name> | |
* 3 list cert/key 3,[type],<internal name> | |
* 4 retrieve MD5 of imported cert/key 4,<type><internal name> | |
* | |
*/ | |
void loop() { | |
static bool first_time = true; | |
int iRC; | |
int iRC1; | |
int iRC2; | |
int iRC3; | |
int iRC4; | |
if (first_time) { | |
// List all the certs and delete them | |
strcpy(pchBuffer,"AT+USECMNG=3\r\n"); | |
write_cmd(pchBuffer); | |
delay(20*milliseconds); | |
first_time = false; | |
} | |
iRC = read_resp2(pchBuffer); | |
Serial.print("Response len: "); | |
Serial.println(iRC); | |
Serial.print("Response: "); | |
Serial.println(pchBuffer); | |
//delay(20*milliseconds); | |
//iRC = read_resp2(pchBuffer); | |
//Serial.print("Response len: "); | |
//Serial.println(iRC); | |
//Serial.print("Response: "); | |
//Serial.println(pchBuffer); | |
//delay(20*milliseconds); | |
//iRC2 = read_resp(pchBuffer); | |
//Serial.print("Response: "); | |
//Serial.println(pchBuffer); | |
//delay(20*milliseconds); | |
//iRC3 = read_resp(pchBuffer); | |
//Serial.print("Response: "); | |
//Serial.println(pchBuffer); | |
//delay(2*seconds); | |
//iRC4 = read_resp(pchBuffer); | |
//Serial.print("Response len: "); | |
//Serial.println(iRC1); | |
//Serial.print("Response len: "); | |
//Serial.println(iRC2); | |
//Serial.print("Response len: "); | |
//Serial.println(iRC3); | |
//Serial.print("Response len: "); | |
//Serial.println(iRC4); | |
//Serial.print("Response: "); | |
//Serial.println(pchBuffer); | |
//Serial.println("END STEP 0 --------------------"); | |
//stop_here: | |
// Serial.println("Halt"); | |
// while(true); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment