Skip to content

Instantly share code, notes, and snippets.

@tdrkDev
Created August 18, 2021 20:45
Show Gist options
  • Save tdrkDev/d27ea4debb8bfe1a527a4c1d403a1c9e to your computer and use it in GitHub Desktop.
Save tdrkDev/d27ea4debb8bfe1a527a4c1d403a1c9e to your computer and use it in GitHub Desktop.
I know this is shitty and that it is not working.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
class Serial2IO {
// Default: ttyUSB0
static final String _serialPort = "/dev/ttyUSB2";
// Request serial data via Linux commands... yeah...
static Future<Map<String, dynamic>?> request(
Map<String, dynamic> data) async {
// Configure serial port
var pr = await Process.run(
"stty",
["-F", _serialPort, "57600", "raw"],
);
if (pr.exitCode != 0) {
return null;
}
pr = await Process.run(
"bash",
["-c", "echo '${jsonEncode(data)}' > $_serialPort"],
);
if (pr.exitCode != 0) {
return null;
}
bool finished = false;
Map<String, dynamic>? json;
Process p = await Process.start("cat", ["/dev/ttyUSB0"]);
p.stdout.listen((event) {
String rawjs = utf8.decode(event);
print(rawjs);
if (rawjs.length < 10) {
return;
}
Map<String, dynamic> tmpJson = jsonDecode(rawjs);
if (tmpJson["status"] == null) {
return;
}
if (tmpJson["status"] == "ok") {
json = tmpJson;
}
finished = true;
});
while (!finished) {
await Future.delayed(Duration(milliseconds: 100));
}
p.kill();
return json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment