Created
June 27, 2018 11:57
-
-
Save JackTheEngineer/65d0caa1e1ace13a00394116b07a8b6e to your computer and use it in GitHub Desktop.
Haskell Serial Port with "serialport" library
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
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