packages feed

serialport 0.4.0.1 → 0.4.1

raw patch · 6 files changed

+93/−46 lines, 6 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- System.Hardware.Serialport.Posix: cIoctl' :: Fd -> Int -> Ptr d -> IO ()
- System.Hardware.Serialport.Posix: c_ioctl :: CInt -> CInt -> Ptr () -> IO CInt
- System.Hardware.Serialport.Posix: closeSerial :: SerialPort -> IO ()
- System.Hardware.Serialport.Posix: commSpeedToBaudRate :: CommSpeed -> BaudRate
- System.Hardware.Serialport.Posix: configureSettings :: TerminalAttributes -> SerialPortSettings -> TerminalAttributes
- System.Hardware.Serialport.Posix: getSerialSettings :: SerialPort -> SerialPortSettings
- System.Hardware.Serialport.Posix: getTIOCM :: Fd -> IO Int
- System.Hardware.Serialport.Posix: openSerial :: FilePath -> SerialPortSettings -> IO SerialPort
- System.Hardware.Serialport.Posix: recvChar :: SerialPort -> IO (Maybe Char)
- System.Hardware.Serialport.Posix: sendChar :: SerialPort -> Char -> IO ()
- System.Hardware.Serialport.Posix: setDTR :: SerialPort -> Bool -> IO ()
- System.Hardware.Serialport.Posix: setRTS :: SerialPort -> Bool -> IO ()
- System.Hardware.Serialport.Posix: setSerialSettings :: SerialPort -> SerialPortSettings -> IO SerialPort
- System.Hardware.Serialport.Posix: setTIOCM :: Fd -> Int -> IO ()
- System.Hardware.Serialport.Posix: withFlowControl :: TerminalAttributes -> FlowControl -> TerminalAttributes
- System.Hardware.Serialport.Posix: withParity :: TerminalAttributes -> Parity -> TerminalAttributes
- System.Hardware.Serialport.Posix: withStopBits :: TerminalAttributes -> StopBits -> TerminalAttributes
- System.Hardware.Serialport.Types: CS110 :: CommSpeed
- System.Hardware.Serialport.Types: CS115200 :: CommSpeed
- System.Hardware.Serialport.Types: CS1200 :: CommSpeed
- System.Hardware.Serialport.Types: CS19200 :: CommSpeed
- System.Hardware.Serialport.Types: CS2400 :: CommSpeed
- System.Hardware.Serialport.Types: CS300 :: CommSpeed
- System.Hardware.Serialport.Types: CS38400 :: CommSpeed
- System.Hardware.Serialport.Types: CS4800 :: CommSpeed
- System.Hardware.Serialport.Types: CS57600 :: CommSpeed
- System.Hardware.Serialport.Types: CS600 :: CommSpeed
- System.Hardware.Serialport.Types: CS9600 :: CommSpeed
- System.Hardware.Serialport.Types: Even :: Parity
- System.Hardware.Serialport.Types: NoFlowControl :: FlowControl
- System.Hardware.Serialport.Types: NoParity :: Parity
- System.Hardware.Serialport.Types: Odd :: Parity
- System.Hardware.Serialport.Types: One :: StopBits
- System.Hardware.Serialport.Types: SerialPort :: Fd -> SerialPortSettings -> SerialPort
- System.Hardware.Serialport.Types: SerialPortSettings :: CommSpeed -> Word8 -> StopBits -> Parity -> FlowControl -> Int -> SerialPortSettings
- System.Hardware.Serialport.Types: Software :: FlowControl
- System.Hardware.Serialport.Types: Two :: StopBits
- System.Hardware.Serialport.Types: bitsPerWord :: SerialPortSettings -> Word8
- System.Hardware.Serialport.Types: commSpeed :: SerialPortSettings -> CommSpeed
- System.Hardware.Serialport.Types: data CommSpeed
- System.Hardware.Serialport.Types: data FlowControl
- System.Hardware.Serialport.Types: data Parity
- System.Hardware.Serialport.Types: data SerialPort
- System.Hardware.Serialport.Types: data SerialPortSettings
- System.Hardware.Serialport.Types: data StopBits
- System.Hardware.Serialport.Types: defaultSerialSettings :: SerialPortSettings
- System.Hardware.Serialport.Types: fd :: SerialPort -> Fd
- System.Hardware.Serialport.Types: flowControl :: SerialPortSettings -> FlowControl
- System.Hardware.Serialport.Types: instance Show CommSpeed
- System.Hardware.Serialport.Types: newSettings :: SerialPort -> SerialPortSettings
- System.Hardware.Serialport.Types: parity :: SerialPortSettings -> Parity
- System.Hardware.Serialport.Types: stopb :: SerialPortSettings -> StopBits
- System.Hardware.Serialport.Types: timeout :: SerialPortSettings -> Int
+ System.Hardware.Serialport: recvString :: SerialPort -> IO String
+ System.Hardware.Serialport: sendString :: SerialPort -> String -> IO ()

Files

System/Hardware/Serialport.hs view
@@ -3,9 +3,11 @@ -- |This module provides the serial port interface. -- -- > import System.Hardware.Serialport--- > s <- openSerial "/dev/ttyUSB0" defaultSerialSettings--- > sendChar s 'A'--- > Just resp <- recvChar s+-- > let port = "COM3"          -- Windows+-- > let port = "/dev/ttyUSB0"  -- Linux+-- > s <- openSerial port defaultSerialSettings { commSpeed = CS2400 }+-- > sendString s "AT\r"+-- > recvString s >>= print -- > closeSerial s -- @@ -20,15 +22,20 @@   -- | You don't need the get or set functions, they are used by openSerial   ,SerialPortSettings(..)   ,defaultSerialSettings-  -- * Serial methods +  ,setSerialSettings+  ,getSerialSettings+  -- * Serial methods+  -- ** Device   ,openSerial+  ,closeSerial+  -- ** Sending & receiving   ,sendChar+  ,sendString   ,recvChar-  ,closeSerial+  ,recvString+  -- ** Line control   ,setDTR   ,setRTS-  ,setSerialSettings-  ,getSerialSettings   ) where  #if defined(mingw32_HOST_OS)
System/Hardware/Serialport/Posix.hsc view
@@ -1,4 +1,5 @@ {-# LANGUAGE ForeignFunctionInterface #-}+{-# OPTIONS_HADDOCK hide #-} module System.Hardware.Serialport.Posix where  import System.IO.Error@@ -18,27 +19,42 @@   fd' <- openFd dev ReadWrite Nothing defaultFileFlags { noctty = True }   let serial_port = (SerialPort fd' defaultSerialSettings)   return =<< setSerialSettings serial_port settings-         + -- |Possibly receive a character unless the timeout given in openSerial is exceeded. recvChar :: SerialPort -> IO (Maybe Char) recvChar (SerialPort fd' _) = do   result <- try $ fdRead fd' 1-  case result of-    Right (str, _) -> return $ Just $ head str-    Left _         -> return Nothing+  return $ case result of+             Right (str, _) -> Just $ head str+             Left _         -> Nothing  +-- |Receive a string+recvString :: SerialPort -> IO String+recvString (SerialPort fd' _) = do+  result <- try $ fdRead fd' 128+  return $ case result of+             Right (str, _) -> str+             Left _         -> ""++ -- |Send a character sendChar :: SerialPort -> Char -> IO () sendChar (SerialPort fd' _ ) c =   fdWrite fd' [c] >> return ()  +-- |Send a string+sendString :: SerialPort -> String -> IO ()+sendString (SerialPort fd' _) s =+  fdWrite fd' s >> return ()++ -- |Close the serial port closeSerial :: SerialPort -> IO () closeSerial (SerialPort fd' _ ) =-  closeFd fd' +  closeFd fd'   #include <sys/ioctl.h>@@ -59,7 +75,7 @@ setTIOCM :: Fd -> Int -> IO () setTIOCM fd' val =   with val $ cIoctl' fd' #{const TIOCMSET}-  +  -- |Set the Data Terminal Ready level setDTR :: SerialPort -> Bool -> IO ()
System/Hardware/Serialport/Types.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-}+{-# OPTIONS_HADDOCK hide #-} module System.Hardware.Serialport.Types where  import Data.Word@@ -30,16 +31,16 @@ data FlowControl = Software | NoFlowControl  data SerialPortSettings = SerialPortSettings {-                      commSpeed :: CommSpeed,     -- ^ baudrate-                      bitsPerWord :: Word8,       -- ^ Number of bits in a word-                      stopb :: StopBits,          -- ^ Number of stop bits-                      parity :: Parity,           -- ^ Type of parity-                      flowControl :: FlowControl, -- ^ Type of flowcontrol-                      timeout :: Int              -- ^ Timeout when receiving a char in tenth of seconds+                      commSpeed    :: CommSpeed,   -- ^ baudrate+                      bitsPerWord  :: Word8,       -- ^ Number of bits in a word+                      stopb        :: StopBits,    -- ^ Number of stop bits+                      parity       :: Parity,      -- ^ Type of parity+                      flowControl  :: FlowControl, -- ^ Type of flowcontrol+                      timeout      :: Int          -- ^ Timeout when receiving a char in tenth of seconds                   }-                       -data SerialPort = SerialPort { ++data SerialPort = SerialPort { #if defined(mingw32_HOST_OS)                       handle :: HANDLE, #else@@ -50,7 +51,7 @@   -- | Most commonly used configuration---  +-- --  - 9600 baud -- --  - 8 data bits
System/Hardware/Serialport/Windows.hs view
@@ -1,4 +1,5 @@-module System.Hardware.Serialport.Windows where +{-# OPTIONS_HADDOCK hide #-}+module System.Hardware.Serialport.Windows where  import Data.Bits import qualified System.Win32.Comm as Comm@@ -16,11 +17,11 @@ openSerial :: String      -- ^ The filename of the serial port, such as @COM5@ or @\/\/.\/CNCA0@            -> SerialPortSettings            -> IO SerialPort-openSerial dev settings = do +openSerial dev settings = do   h <- createFile dev access_mode share_mode security_attr create_mode file_attr template_file   let serial_port = SerialPort h defaultSerialSettings   return =<< setSerialSettings serial_port settings-  where +  where     access_mode = gENERIC_READ .|. gENERIC_WRITE     share_mode = fILE_SHARE_NONE     security_attr = Nothing@@ -32,27 +33,48 @@ -- |Possibly receive a character unless the timeout given in openSerial is exceeded. recvChar :: SerialPort -> IO (Maybe Char) recvChar (SerialPort h _) =-  allocaBytes 1 $ \ p_n -> do -  received <- win32_ReadFile h p_n count overlapped-  if received == 0-    then return Nothing-    else do c <- peek p_n :: IO CChar-            return $ Just $ castCCharToChar c+  allocaBytes 1 $ \ p_n -> do+    received <- win32_ReadFile h p_n count overlapped+    if received == 0+      then return Nothing+      else do c <- peek p_n :: IO CChar+              return $ Just $ castCCharToChar c   where     count = 1     overlapped = Nothing  +-- |Receive a string+recvString :: SerialPort -> IO String+recvString (SerialPort h _) =+  allocaBytes 128 $ \ p_n -> do+    recv_cnt <- win32_ReadFile h p_n count overlapped+    peekCStringLen (p_n, fromIntegral recv_cnt)+  where+    count = 128+    overlapped = Nothing++ -- |Send a character sendChar :: SerialPort -> Char -> IO () sendChar (SerialPort h _) s =-  with s (\ p_s -> do _ <- win32_WriteFile h p_s count overlapped +  with s (\ p_s -> do _ <- win32_WriteFile h p_s count overlapped                       return () )   where     count = 1     overlapped = Nothing  +-- |Send a string+sendString :: SerialPort -> String -> IO ()+sendString (SerialPort h _) s =+  withCString s (\ p_s -> do _ <- win32_WriteFile h p_s (fromIntegral count) overlapped+                             return () )+  where+    count = length s+    overlapped = Nothing++ -- |Close the serial port closeSerial :: SerialPort -> IO () closeSerial (SerialPort h _) =@@ -77,8 +99,8 @@                   -> IO SerialPort        -- ^ New serial port setSerialSettings (SerialPort h _) new_settings = do   let ct = Comm.COMMTIMEOUTS {-                    Comm.readIntervalTimeout = maxBound :: DWORD, -                    Comm.readTotalTimeoutMultiplier = maxBound :: DWORD, +                    Comm.readIntervalTimeout = maxBound :: DWORD,+                    Comm.readTotalTimeoutMultiplier = maxBound :: DWORD,                     Comm.readTotalTimeoutConstant = fromIntegral (timeout new_settings) * 100,                     Comm.writeTotalTimeoutMultiplier = 0,                     Comm.writeTotalTimeoutConstant = 0 }
System/Win32/Comm.hsc view
@@ -1,4 +1,5 @@ {-# LANGUAGE ForeignFunctionInterface #-}+{-# OPTIONS_HADDOCK hide #-} module System.Win32.Comm where  import System.Win32.Types@@ -92,7 +93,7 @@   alloca (\dcbp -> do _ <- c_GetCommState h dcbp                       peek dcbp ) - + --BOOL WINAPI GetCommState( --  __in     HANDLE hFile, --  __inout  LPDCB lpDCB@@ -126,8 +127,8 @@ --   * If no bytes arrive within the time specified by ReadTotalTimeoutConstant, ReadFile times out. -- type LPCOMMTIMEOUTS = Ptr COMMTIMEOUTS-data COMMTIMEOUTS = COMMTIMEOUTS -    { readIntervalTimeout :: DWORD, -- in milliseconds +data COMMTIMEOUTS = COMMTIMEOUTS+    { readIntervalTimeout :: DWORD, -- in milliseconds       readTotalTimeoutMultiplier :: DWORD,  -- in milliseconds       readTotalTimeoutConstant :: DWORD,  -- in milliseconds       writeTotalTimeoutMultiplier :: DWORD,  -- in milliseconds@@ -155,8 +156,8 @@                           readTotalTimeoutConstant = _readTotalTimeoutConstant,                           writeTotalTimeoutMultiplier = _writeTotalTimeoutMultiplier,                           writeTotalTimeoutConstant = _writeTotalTimeoutConstant }-     + getCommTimeouts :: HANDLE -> IO COMMTIMEOUTS getCommTimeouts h =   alloca (\c -> do _ <- c_GetCommTimeouts h c@@ -170,8 +171,8 @@   --- | --- +-- |+-- -- On success it returns nonzero. On failure, the return value is zero and the GetLastError should be called. -- setCommTimeouts :: HANDLE -> COMMTIMEOUTS -> IO ()@@ -197,7 +198,7 @@ setRTS :: DWORD setRTS = #const SETRTS --- +-- -- foreign import stdcall unsafe "winbase.h EscapeCommFunction"   c_EscapeCommFunction :: HANDLE -> DWORD -> IO BOOL
serialport.cabal view
@@ -1,5 +1,5 @@ Name:           serialport-Version:        0.4.0.1+Version:        0.4.1 Cabal-Version:  >= 1.6 Build-Type:     Custom license:        BSD3@@ -19,14 +19,14 @@  Library   Exposed-Modules:    System.Hardware.Serialport-                      System.Hardware.Serialport.Types-  Build-Depends:      base >= 4, base < 5 +  Other-Modules:      System.Hardware.Serialport.Types+  Build-Depends:      base >= 4, base < 5   ghc-options:        -Wall    if !os(windows)     Build-Depends:    unix-    Exposed-Modules:  System.Hardware.Serialport.Posix+    Other-Modules:    System.Hardware.Serialport.Posix   else     Build-Depends:    Win32-    Exposed-Modules:  System.Hardware.Serialport.Windows+    Other-Modules:    System.Hardware.Serialport.Windows                       System.Win32.Comm