Created
August 18, 2021 20:45
-
-
Save tdrkDev/d27ea4debb8bfe1a527a4c1d403a1c9e to your computer and use it in GitHub Desktop.
I know this is shitty and that it is not working.
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
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