简体   繁体   English

iOS应用Modbus使用Swift读/写

[英]iOS app Modbus read/write using Swift

I have not programmed in C before and now learn Swift for iOS. 我以前没有用C编程,现在学习iOS的Swift。 I'm creating an iOS app that needs to talk to devices via Modbus. 我正在创建一个需要通过Modbus与设备对话的iOS应用。 I'm trying to use ObjectiveLibModbus https://github.com/iUtvikler/ObjectiveLibModbus but I don't know where to start. 我正在尝试使用ObjectiveLibModbus https://github.com/iUtvikler/ObjectiveLibModbus,但我不知道从哪里开始。

How to import this library in my Swift app and how using it? 如何在我的Swift应用程序中导入该库以及如何使用它?

Or does anyone have another library of code for use Modbus protocol in Swift app? 还是有人在Swift应用程序中有另一个使用Modbus协议的代码库?

For Swift new library available for modbus: new Swift fork ktsakaguchi/SwiftLibModbus 对于可用于Modbus的Swift新库:新的Swift fork ktsakaguchi / SwiftLibModbus

Now make a new instance of SwiftLibModbus and connect: 现在创建一个新的SwiftLibModbus实例并连接:

let swiftLibModbus = SwiftLibModbus(ipAddress: "192.168.2.10", port: 502, device: 1)
swiftLibModbus.connect(
    { () -> Void in
        //connected and ready to do modbus calls
    },
    failure: { (error: NSError) -> Void in
        //Handle error
        print("error")
})

Make a modbus call: 拨打Modbus电话:

swiftLibModbus.readBitsFrom(1000, count: 5,
    success: { (array: [AnyObject]) -> Void in
        //Do something with the returned data (NSArray of NSNumber)..
        print("success: \(array)")
    },
    failure:  { (error: NSError) -> Void in
        //Handle error
        print("error")
})

Disconnect when you are finished with you're modbus calls: 完成modbus调用后断开连接:

swiftLibModbus.disconnect()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM