Skip to content

Commit

Permalink
Commands add
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanesik committed Jul 3, 2018
1 parent cc24a2b commit e55dee0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 18 deletions.
8 changes: 6 additions & 2 deletions ModSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
586037DA7437745911FAB5E6 /* Enums.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5860356FE565C3B22C5D8C18 /* Enums.swift */; };
58603AE5273B21227998B58D /* ModSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58603087D792F251503FE14F /* ModSwift.swift */; };
846CDB7B20E9F506005B86D8 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846CDB7A20E9F506005B86D8 /* Model.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -27,6 +28,7 @@
58603087D792F251503FE14F /* ModSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModSwift.swift; sourceTree = "<group>"; };
586030A215EC6361DDB57930 /* libModSwift.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libModSwift.a; sourceTree = BUILT_PRODUCTS_DIR; };
5860356FE565C3B22C5D8C18 /* Enums.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Enums.swift; sourceTree = "<group>"; };
846CDB7A20E9F506005B86D8 /* Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Model.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -53,6 +55,7 @@
children = (
58603087D792F251503FE14F /* ModSwift.swift */,
5860356FE565C3B22C5D8C18 /* Enums.swift */,
846CDB7A20E9F506005B86D8 /* Model.swift */,
);
path = ModSwift;
sourceTree = "<group>";
Expand Down Expand Up @@ -115,6 +118,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
846CDB7B20E9F506005B86D8 /* Model.swift in Sources */,
58603AE5273B21227998B58D /* ModSwift.swift in Sources */,
586037DA7437745911FAB5E6 /* Enums.swift in Sources */,
);
Expand Down Expand Up @@ -171,8 +175,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 11.4;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down Expand Up @@ -269,6 +272,7 @@
58603ABE4933B8C35D847AB1 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
58603D13BEA65649DF7F598A /* Build configuration list for PBXProject "ModSwift" */ = {
isa = XCConfigurationList;
Expand Down
Binary file not shown.
5 changes: 3 additions & 2 deletions ModSwift/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
Describe the format of sended package
*/
enum ModbusMode {
case rtu
case tcp
case rtu
case ascii
}

/**
Expand All @@ -21,7 +22,7 @@ enum ModbusMode {
- Holding: 16 bit read/write unsigned register
- Input: 16 bit only read unsigned register
*/
enum Command: UInt16 {
enum Command: UInt8 {
/** Read coil (one bit) register */
case readCoilStatus = 0x01
/** Read discrete (one bit) register */
Expand Down
119 changes: 105 additions & 14 deletions ModSwift/ModSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,181 @@

import Foundation

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////TODO: в документации не забыть описать автоинкремент//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////TODO: в документации написать что это мастер//////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////TODO: в документации описать что есть MBAP картинкой (http://cleverhouse.club/software/dispatch/chto-takoe-modbus-rtu-i-modbus-tcp.html)//////////////
/////////////////////////TODO: написать тесты//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





class ModSwift {

private var _mode: ModbusMode
private var _devAdress: UInt16
private var _slaveAdress: UInt8

/// Just for .tcp mode
private var _transactId: UInt16 = 0x00
/// Just for .tcp mode
private var _protocolId: UInt16 = 0x00

//***************************************************************************************************************
//-Setup---------------------------------------------------------------------------------------------------------
//***************************************************************************************************************

init(mode: ModbusMode = .tcp, device: UInt16 = 0x00) {
/// Set modbus device adress.
/// - parameters:
/// - mode: Modbus mode (default tcp)
/// - device: Modbus device 16-bit adress (default 0x00)
init(mode: ModbusMode = .tcp, device: UInt8 = 0x00) {
_mode = mode
_devAdress = device
_slaveAdress = device
}

/// Set modbus device adress.
///
/// - parameters:
/// - device: Modbus device 16-bit adress.
func setDevice(device: UInt16 = 0x00) {
_devAdress = device
func setDevice(device: UInt8 = 0x00) {
_slaveAdress = device
}

/// Set modbus device mode.
/// - parameters:
/// - mode: Modbus mode (.tcp/.rtu/.ascii).
func setMode(mode: ModbusMode) {
_mode = mode
}

/// Set modbus device adress.
///
/// - parameters:
/// - device: Modbus device 16-bit adress.
func createCommand(command: Command, data: [UInt16]) -> Data {
let data = Data()
return data
/// Set transactint id of package.
func setTransactionId(_ transactId: UInt16) {
_transactId = transactId
}

/// Set protocol Id (default 0x00)
func setProtocolId(_ protocolId: UInt16) {
_protocolId = protocolId
}

//***************************************************************************************************************
//-Read-Package--------------------------------------------------------------------------------------------------
//***************************************************************************************************************
func readRequest() {
func readRequest(data: Data) -> [UInt8] {
let arr = [UInt8](data)
return arr
}

//***************************************************************************************************************
//-Create-Package------------------------------------------------------------------------------------------------
//***************************************************************************************************************
func readCoidStatus(adress: UInt32) -> Data {
func createCommand(command: Command, address: UInt16, data: [UInt16]) -> Data {
var package = [UInt8]()

if _mode == ModbusMode.tcp {
addMBAP(package)
_transactId += 1
}

package.append(_slaveAdress)
package.append(command.rawValue)

switch command {
case .readCoilStatus:
break
case .readDiscreteInputs:
break
case .readHoldingRegisters:
package.append(contentsOf: [UInt8(address >> 8), UInt8(address & 0xFF)])
break
case .readInputRegisters:
break
case .forceSingleCoil:
break
case .forceMultipleCoils:
break
case .presetSingleRegister:
break
case .presetMultipleRegisters:
break
}

let data = Data(package)
return data
}


func readCoidStatus(address: UInt32) -> Data {
let data = Data()

_transactId += 1
return data
}

func readDiscreteInputs(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func readHoldingRegisters(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func readInputRegisters(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func forceSingleCoil(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func presetSingleRegister(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func forceMultipleCoils(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}

func presetMultipleRegisters(/*parameters*/) -> Data {
let data = Data()

_transactId += 1
return data
}




//***************************************************************************************************************
//-Help----------------------------------------------------------------------------------------------------------
//***************************************************************************************************************
private func addMBAP(_ arr: [UInt8]) {
var temp = arr
temp.append(UInt8(_transactId >> 8))
temp.append(UInt8(_transactId & 0xFF))
temp.append(UInt8(_protocolId >> 8))
temp.append(UInt8(_protocolId & 0xFF))
}


}
8 changes: 8 additions & 0 deletions ModSwift/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
//

import Foundation

struct Request {

}

struct Response {

}

0 comments on commit e55dee0

Please sign in to comment.