Last active
March 5, 2018 17:03
-
-
Save smurphy8/10738050 to your computer and use it in GitHub Desktop.
using haskell-modbus over network.
This file contains 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
{-# LANGUAGE NoImplicitPrelude, OverloadedStrings, RankNTypes #-} | |
module ModTest where | |
import Data.Modbus | |
import Conduit | |
import CorePrelude hiding (ByteString) | |
-- import Control.Concurrent.Async (Concurrently (..)) | |
import qualified Data.ByteString.Lazy as BL | |
import qualified Data.ByteString as B | |
import Data.Modbus | |
import Data.Digest.CRC16 | |
import Data.Serialize | |
import Numeric | |
import Data.Word | |
import Data.Conduit.Network | |
import Data.Conduit.Cereal | |
-- import Network.Simple.TCP | |
testRunTCPClient = runTCPClient (clientSettings 20090 "166.131.38.15") $ testRequest | |
where | |
testRequest server = do | |
sourceModRequestRead $$ appSink server | |
appSource server $$ sinkGet modReqGetter | |
-- sourceModRequestWrite $$ appSink server | |
-- appSource server $$ sinkGet modReqGetter | |
testSlaveId :: SlaveId | |
testSlaveId = 11 | |
readTarget = 0 | |
writeTarget = 0 | |
writeThing :: Word16 | |
writeThing = 7569 | |
sourceModRequestRead = sourceLazy $ BL.fromStrict.runPut.put $ ModRequestFrame testSlaveId (ReadHoldingRegisters readTarget 4) | |
--sourceModRequestWrite = sourceLazy $ BL.fromStrict.runPut.put $ ModRequestFrame testSlaveId (WriteSingleRegister writeTarget writeThing) | |
converter :: Get [Word8] | |
converter = do | |
i <- remaining | |
b <- getBytes i | |
return . B.unpack $ b | |
modReqGetter :: Get ModResponseFrame | |
modReqGetter = get | |
printAsNumbers bs = showHex <$> B.unpack bs <*> [""] | |
testGetModResponseFcn :: Get ModResponse | |
testGetModResponseFcn = get | |
testResponseRawSimple = B.pack [0x83,0x02] | |
testResponseRaw = B.pack [0x0b,0x83,0x02,0xe0,0xf3] | |
expectedRequestCrc = [0x85,0xde] | |
t :: B.ByteString | |
t = "\NUL\RS" -- "\SUB\144"-- "\v\131\STX\224\243" | |
-- CABAL | |
{- | | |
Name: modbus-test | |
Version: 0.1.0 | |
Author: Scott Murphy <scottmurphy09@gmail.com> | |
Maintainer: Scott Murphy <scottmurphy09@gmail.com> | |
License: BSD3 | |
License-File: LICENSE | |
-- Synopsis: | |
-- Description: | |
Cabal-Version: >= 1.10 | |
Build-Type: Simple | |
Library | |
Default-Language: Haskell2010 | |
HS-Source-Dirs: src | |
GHC-Options: -Wall | |
Exposed-Modules: ModTest | |
Other-Modules: ModTest.Internal | |
Build-Depends: base >= 4 && < 5 | |
,haskell-modbus == 0.3.1.7569 | |
,conduit == 1.1.0.1 | |
,conduit-extra == 1.1.0.1 | |
,conduit-combinators | |
,basic-prelude | |
,lens | |
,cereal | |
,cereal-conduit | |
,bytestring | |
,network | |
,network-simple | |
Test-Suite spec | |
Type: exitcode-stdio-1.0 | |
Default-Language: Haskell2010 | |
Hs-Source-Dirs: src | |
, test | |
Ghc-Options: -Wall | |
Main-Is: Spec.hs | |
Build-Depends: base | |
, hspec | |
, haskell-modbus | |
Source-Repository head | |
Type: git | |
-- Location: | |
|-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment