Haskell:串口接收数据打印不正确
Haskell: Serial received data not printed correctly
我正在尝试从我的 Arduino Serial.println("No Format");
接收数据。当我打开 arduino-serial-monitor 时,我可以看到我想要的输出。
No Format
No Format
...
我用 serialport package.但是在ghci上打印的输出是:
*Main> main
"\r\nNo Forma"
"t\r\nNo Form"
"at\r\nNo For"
"mat\r\nNo Fo"
"rmat\r\nNo F"
"ormat\r\nNo "
"Format\r\nNo"
" Format\r\nN"
"o Format\r\n"
...
...
"\nNo"
" For"
"mat\r"
"\nNo "
"Form"
"at\r\n"
....
Haskell:
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
import System.IO
import Control.Monad
main :: IO ()
main = forever $ do
let port = "/dev/ttyACM0"
s <- openSerial port SerialPortSettings { commSpeed = CS9600,
bitsPerWord = 8,
stopb = One,
parity = NoParity,
flowControl = NoFlowControl,
timeout = 10 }
recv s 10 >>= print
closeSerial s
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("No Format");
}
任何帮助都会很棒。谢谢。
您可以只接收字符,直到接收到“\n”字符。也许是这样的:
import Control.Monad (forever)
import Control.Applicative ((<$>))
import Data.ByteString.Char8 (unpack)
import System.Hardware.Serialport (SerialPort, openSerial, recv)
-- Read a character from the serial port.
receive :: SerialPort -> IO String
receive sp = fmap unpack (recv sp 1)
-- Read a line, ending with '\n'.
readLine :: SerialPort -> IO String
readLine sp = readLineRec sp []
-- Recursive function to read a line from the serial port.
readLineRec :: SerialPort -> String -> IO String
readLineRec sp [] = receive sp >>= readLineRec sp
readLineRec sp chars
| last chars == '\n' = return chars
| otherwise = (chars ++) <$> receive sp >>= readLineRec sp
main = do
let port = "/dev/ttyACM0"
s <- openSerial port SerialPortSettings { commSpeed = CS9600,
bitsPerWord = 8,
stopb = One,
parity = NoParity,
flowControl = NoFlowControl,
timeout = 10 }
forever $ do
line <- readLine s
print line
还有改进的余地,但很管用!
使用 B.putStr
而不是 print
解决了问题。
感谢@pat.
我正在尝试从我的 Arduino Serial.println("No Format");
接收数据。当我打开 arduino-serial-monitor 时,我可以看到我想要的输出。
No Format
No Format
...
我用 serialport package.但是在ghci上打印的输出是:
*Main> main
"\r\nNo Forma"
"t\r\nNo Form"
"at\r\nNo For"
"mat\r\nNo Fo"
"rmat\r\nNo F"
"ormat\r\nNo "
"Format\r\nNo"
" Format\r\nN"
"o Format\r\n"
...
...
"\nNo"
" For"
"mat\r"
"\nNo "
"Form"
"at\r\n"
....
Haskell:
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
import System.IO
import Control.Monad
main :: IO ()
main = forever $ do
let port = "/dev/ttyACM0"
s <- openSerial port SerialPortSettings { commSpeed = CS9600,
bitsPerWord = 8,
stopb = One,
parity = NoParity,
flowControl = NoFlowControl,
timeout = 10 }
recv s 10 >>= print
closeSerial s
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("No Format");
}
任何帮助都会很棒。谢谢。
您可以只接收字符,直到接收到“\n”字符。也许是这样的:
import Control.Monad (forever)
import Control.Applicative ((<$>))
import Data.ByteString.Char8 (unpack)
import System.Hardware.Serialport (SerialPort, openSerial, recv)
-- Read a character from the serial port.
receive :: SerialPort -> IO String
receive sp = fmap unpack (recv sp 1)
-- Read a line, ending with '\n'.
readLine :: SerialPort -> IO String
readLine sp = readLineRec sp []
-- Recursive function to read a line from the serial port.
readLineRec :: SerialPort -> String -> IO String
readLineRec sp [] = receive sp >>= readLineRec sp
readLineRec sp chars
| last chars == '\n' = return chars
| otherwise = (chars ++) <$> receive sp >>= readLineRec sp
main = do
let port = "/dev/ttyACM0"
s <- openSerial port SerialPortSettings { commSpeed = CS9600,
bitsPerWord = 8,
stopb = One,
parity = NoParity,
flowControl = NoFlowControl,
timeout = 10 }
forever $ do
line <- readLine s
print line
还有改进的余地,但很管用!
使用 B.putStr
而不是 print
解决了问题。
感谢@pat.