Created
May 6, 2021 18:22
-
-
Save algebraic-dev/f8e1c4d9a233bc6009085a2f32a3f35a to your computer and use it in GitHub Desktop.
Simple code to read from serial port with termios in reasonML
This file contains hidden or 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
open Unix; | |
let start = baud => | |
try({ | |
let fd = openfile("/dev/ttyUSB0", [O_RDWR], 0o640); | |
let tty = { | |
...tcgetattr(fd), | |
c_parenb: false, | |
c_cstopb: 1, | |
c_csize: 8, | |
c_icanon: false, | |
c_echo: false, | |
c_echoe: false, | |
c_echonl: false, | |
c_isig: false, | |
c_ixon: false, | |
c_ixoff: false, | |
c_ignbrk: false, | |
c_brkint: false, | |
c_parmrk: false, | |
c_istrip: false, | |
c_inlcr: false, | |
c_igncr: false, | |
c_icrnl: false, | |
c_opost: false, | |
c_cread: true, | |
c_clocal: true, | |
c_vtime: 10, | |
c_vmin: 0, | |
c_obaud: baud, | |
c_ibaud: baud, | |
}; | |
tcsetattr(fd, TCSANOW, tty); | |
let ic = in_channel_of_descr(fd); | |
Ok(ic); | |
}) { | |
| Unix_error(Unix.ENOENT, _, _) => Error("Error on finding arduino") | |
}; | |
let read = ic => | |
try({ | |
let bytes = input_line(ic); | |
Ok(bytes); | |
}) { | |
| _ => Error("Cannot read") | |
}; | |
let loop_read = ic => { | |
let rec loop = () => { | |
switch (read(ic)) { | |
| Ok(line) => | |
print_endline(line); | |
loop(); | |
| Error(_) => () | |
}; | |
}; | |
loop(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment