Skip to content

Instantly share code, notes, and snippets.

@oryjkov

oryjkov/check.py Secret

Created October 6, 2020 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oryjkov/3d7b0a6402b85cad6db9d14b2078b560 to your computer and use it in GitHub Desktop.
Save oryjkov/3d7b0a6402b85cad6db9d14b2078b560 to your computer and use it in GitHub Desktop.
import serial
import struct
s = serial.Serial('/dev/ttyACM1', 460800, timeout=1.0)
print('Opened port %s', s)
s.write(b'1')
ok = 0
while True:
buf = s.read(1)
(msgLen, ) = struct.unpack('B', buf)
buf = s.read(msgLen)
sm = 0
for c in buf[:-1]:
sm = (sm + c) % 256
if sm != buf[-1]:
print("wrong sum", sm, buf[-1])
else:
ok += 1
if ok % 10 == 1:
print("ok", ok)
s.write(buf)
#include "mbed.h"
BufferedSerial com(USBTX, USBRX, 460800);
void check(BufferedSerial *com) {
uint8_t u;
com->read(&u, 1);
uint8_t buf[256];
while(1) {
uint8_t len = rand() % 254 + 1;
uint8_t sum = 0;
for (int i = 0; i < len; i++) {
buf[i] = rand() % 256;
sum = (sum + buf[i]) % 256;
}
buf[len] = sum;
len += 1;
com->write(&len, 1);
com->write(buf, len);
int rd = 0;
while (rd < len) {
int x = com->read(&buf, len);
rd += x;
}
sum = 0;
for (int i = 0; i < len-1; i++) {
sum = (sum + buf[i]) % 256;
}
if (sum != buf[len-1]) {
while(1) {}
}
}
}
int main() {
check(&com);
}
{
"requires": ["bare-metal"],
"target_overrides": {
"*": {
"target.c_lib": "small"
},
"K64F": {
"platform.stdio-baud-rate": 9600
}
}
}
{
"target_overrides": {
"*": {
"target.c_lib": "small"
},
"K64F": {
"platform.stdio-baud-rate": 9600
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment