I need to write a program to communicate with the STM32(a kind of microcontroller) via Serial Port. Is there any library of OCaml to use Serial Port?
This is the third time where you ask if something exists in OCaml and response of this kind of question can be found in the OPAM website which has a list of all libraries/packages available in the OCaml ecosystem.
Then, opam search
in your shell can give you a response too.
At the end, most of these projects are hosted on GitHub
or GitLab
otherwise.
Assuming the serial interface is connected to your computer and it shows up as a /dev
file you can simply open this file and use the Unix
library bundled with OCaml.
More precisely the terminal interface will allow you to setup to baud rate, parity, etc.
Here’s a program I wrote a long time ago that may help you to get started.
But I was on Windows.
Which serial, USB or COM? If the latter, then COM devices provide files in basically any directory, so instead of /dev/ttyX you could read COM[0…9].
At the time of writing there is https://github.com/m-laniakea/oserial for unix.
Hello,
I’m using Ocaml in windows too, and it seems that three years later, there is no more solutions for this issue.
I’m able to open a connexion to the serial port in python in windows using pyserial:
import serial
s = serial.Serial("\\\\.\\COM12")
s.write(bytes("Hello World", "utf-8"))
And the code in OCaml using oserial works fine when I’m using linux:
module Serial_config = struct
let port = "/dev/ttyACM1"
let baud_rate = 115200
end
module Serial0 = Serial.Make (Serial_config)
let () = Lwt_main.run (Serial0.write_line "Hello, World!")
But I’m facing a situation where I do not know how to port this code into Windows, because the functions required for opening a terminal, like tcgetattr are not implemented. I’ve spent some time in order to see if I could find a solution but didn’t found anything usefull, and even this thread does not give any concluding answer.
Should I keep going for looking a solution, or should I use another langage for my windows code? (It’s fine if I have to, I just want to be sure before spending too much time on it).
Thanks!
On Windows you need to use different APIs to interact with serial ports. It should not be difficult to implement if you have some spare time, see for example https://ds.opdenbrouw.nl/micprg/pdf/serial-win.pdf.
Cheers,
Nicolas
Thanks! This is a very usefull pointer. I keep the reference and will try to see how to implement it!