Skip to content

Instantly share code, notes, and snippets.

@JackTheEngineer
Created June 27, 2018 11:57
Show Gist options
  • Save JackTheEngineer/65d0caa1e1ace13a00394116b07a8b6e to your computer and use it in GitHub Desktop.
Save JackTheEngineer/65d0caa1e1ace13a00394116b07a8b6e to your computer and use it in GitHub Desktop.
Haskell Serial Port with "serialport" library
import qualified Data.ByteString.Char8 as B
import System.IO
import System.Hardware.Serialport
import Control.Monad.Loops
import Control.Monad
import Data.Char
stringToIntList :: String -> [Int]
stringToIntList line
| ((>2) . length) line = map ord line
| otherwise = []
main = do
let port = "/dev/ttyUSB0" -- Linux
s <- hOpenSerial port defaultSerialSettings {commSpeed = CS115200,
timeout = 1}
--- The Timeout is given in tenth of seconds.
--- This means, that the program waits 100 ms for a
--- character, until it throws an "End of File reached" error
replicateM_ 100 (hGetLine s >>= (print . stringToIntList))
hClose s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment