2016-03-01 86 views
0

這個類的體面代碼我正在製作一個OSX應用程序來控制串行設備,爲此我使用ORSSerialPort庫。但是我在編碼方面遇到了一些麻煩。我對編碼非常陌生,並且我不知道如何正確編寫代碼,這在嘗試創建某些工作時會導致一些問題。我會展示我的三個'班',我會描述他們的功能,然後描述我遇到的問題。如何讓這些[[Swift]

主類中,向其中庫委託被連接並從其中我做與串行設備(AE20125Controller.swift)

import Cocoa 
import ORSSerial 

class AE20125Controller:NSObject, ORSSerialPortDelegate { 

    var buffer = String() 
    var dataProcessor = DataProcessor() 

    var availablePorts = ORSSerialPortManager.sharedSerialPortManager().availablePorts 

    @IBOutlet weak var selectedPort: NSPopUpButton! 
    @IBOutlet weak var connectButton: NSButton! 
    @IBOutlet weak var connectedLogo: NSStackView! 

    var serialPort: ORSSerialPort? { 
     didSet { 
      oldValue?.close() 
      oldValue?.delegate = nil 
      serialPort?.delegate = self 
     } 
    } 

    //Actions 
    @IBAction func onOpenOrClose(sender: AnyObject) { 
     if let port = self.serialPort { 
      if (port.open) { 
       closePort(port) 

      } else { 
       openPort(port) 
      } 
     } 
    } 

    //Functions 
    func sendData(command: String) -> Bool { 
     if let data = command.dataUsingEncoding(NSUTF8StringEncoding) { 
      self.serialPort?.sendData(data) 
      return true 
     } 
     return false 
    } 

    func closePort(port: ORSSerialPort){ 
     port.close() 
     self.connectButton.title = "Verbinden" 
     self.connectedLogo.hidden = true 
    } 

    func openPort(port: ORSSerialPort){ 
     port.baudRate = 9600 
     port.open() 
     sendData("201:T:0:;") //Gets all settings form the device 
     self.connectedLogo.hidden = false 
     self.connectButton.title = "Verbreken" 
    } 

    //Delegate 
    func serialPort(serialPort: ORSSerialPort, didReceiveData data: NSData) { 
     if let string = NSString(data: data, encoding: NSUTF8StringEncoding) { 
      var message: String 

      //Assemble the full data (Data come's in in parts) 
      buffer.appendContentsOf(string as String) 
      if(buffer.containsString(dataProcessor.endToken)){ 
       let i = buffer.rangeOfString(dataProcessor.startToken)?.startIndex 
       let j = buffer.rangeOfString(dataProcessor.endToken)?.endIndex 

       if((i != nil) && (j != nil)){ 
        message = buffer[Range(start: i!, end: j!)] 
        buffer.removeRange(Range(start: i!, end: j!)) 

        if(message != "" /*message != "201:U:0:;"*/){ 
         dataProcessor.sortIncomingData(message) 
        } 
       } 
      } 
     } 
    } 

    func serialPortWasRemovedFromSystem(serialPort: ORSSerialPort) { 
     serialPort.close() 
    } 

} 

的數據處理器類,其中所有可能的消息都存儲所有的通信和形成我想組裝要發送的數據和當前全部設置(DataProcessor.swift)

import Foundation 

class DataProcessor { 

    enum dataType: String { 
     case frequency = "A" 
     case waveform = "B" 
     case mode = "C" 
     case pllReferenceEnabled = "D" 
     case pllFactor = "E" 
     case pllOffset = "F" 
     case startUpWavefrom = "G" 
     case startUpFrequency = "H" 
     case calibrationOffset = "I" 
     case sweepStartFrequency = "J" 
     case sweepStopFrequency = "K" 
     case sweepFrequency = "L" 
     case modulationType = "M" 
     case fskFrequency = "N" 
     case pskPhase = "O" 
     case modulationSource = "P" 
     case internalModulationFrequency = "Q" 
     case sweepMode = "R" 
     case getSettings = "T" 
     case keepAlive = "U" 
     case returnFromSweepMod = "V" 
     case hardwareRevison = "X" 
     case firmwareVersion = "Y" 
     case productID = "Z" 
     case present1Freq = "1" 
     case present1Waveform = "2" 
     case present2Freq = "3" 
     case present2Waveform = "4" 
     case present3Freq = "5" 
     case present3Waveform = "6" 
     case present4Freq = "7" 
     case present4Waveform = "8" 
     case present5Freq = "9" 
     case present5Waveform = "0" 

     func getWritable() -> Bool { 
      switch self { 
      case .frequency, .waveform, .mode, .pllReferenceEnabled, .pllFactor, .pllOffset, .startUpWavefrom, .startUpFrequency, .calibrationOffset, .sweepStartFrequency, .sweepStopFrequency, .sweepFrequency, .modulationType, .fskFrequency, .pskPhase, .modulationSource, .internalModulationFrequency,.sweepMode, .getSettings, .returnFromSweepMod, .present1Freq, .present1Waveform,.present2Freq,.present2Waveform, .present3Freq, .present3Waveform, .present4Freq, .present4Waveform, .present5Freq, .present5Waveform: 
       return true 

      default: 
       return false 
      } 
     } 

     func getReadable() -> Bool { 
      switch self { 
      case .frequency, .waveform, .mode, .pllReferenceEnabled, .pllFactor, .pllOffset, .startUpWavefrom, .startUpFrequency, .calibrationOffset, .sweepStartFrequency, .sweepStopFrequency, .sweepFrequency, .modulationType, .fskFrequency, .pskPhase, .modulationSource, .internalModulationFrequency, .sweepMode, .keepAlive, .hardwareRevison, .firmwareVersion, .productID: 
       return true 

      default: 
       return false 
      } 

     } 

     func getMaxValues() -> Int{ 
      switch self { 
      case .frequency: 
       return 10000000 
      case .waveform: 
       return 3 
      case .mode: 
       return 3 
      case .pllReferenceEnabled: 
       return 2 
      case .pllFactor: 
       return 9999 
      case .pllOffset: 
       return 10000000 
      case .startUpWavefrom: 
       return 3 
      case .startUpFrequency: 
       return 10000000 
      case .calibrationOffset: 
       return 10000 
      case .sweepStartFrequency: 
       fallthrough 
      case .sweepStopFrequency: 
       return 10000000 
      case .sweepFrequency: 
       return 100 
      case .modulationType: 
       return 2 
      case .fskFrequency: 
       return 10000000 
      case .pskPhase: 
       return 3599 
      case .modulationSource: 
       return 2 
      case .internalModulationFrequency: 
       return 10000 
      case .sweepMode: 
       return 2 
      case .getSettings: 
       return 0 
      case .returnFromSweepMod, .present1Freq, .present2Freq, .present3Freq, .present4Freq, .present5Freq: 
       return 10000000 
      case .present1Waveform, .present2Waveform, .present3Waveform, .present4Waveform, .present5Waveform: 
       return 3 

      default: 
       return -1 
      } 
     } 
    } 


    let endToken = ":;" 
    let startToken = "201:" 

    var settings:[(dataType):(String)] = [:] 

    //Methodes 
    func sortIncomingData(data: String){ 
     let i = data.rangeOfString(startToken)?.endIndex 
     let j = data.rangeOfString(endToken)?.startIndex 

     let sortedCode = data.substringWithRange(Range(start: i!, end: i!.advancedBy(1))) 
     let sortedData = data.substringWithRange(Range(start: i!.advancedBy(2), end: j!)) 

     settings[dataType(rawValue: sortedCode)!] = sortedData 

    } 

    func setData(data: dataType, value: Float){ 
     settings.updateValue(String(value), forKey: data) 
     AE20125Controller.sendData(...) //<-- Is not working (Ignore the  dots) 

    } 
} 

的的菜單項類,這應該處理所有的圖形用戶界面的變化和行動。 (MenuItems.swift)

import Cocoa 

class MenuItems:NSObject{ 
    @IBOutlet weak var freqView: NSView! 
    @IBOutlet weak var test: NSTextField! 

    //Actions 
    @IBAction func setFreq(sender: AnyObject) { 

    } 


} 

我離開了AppDelegate的,因爲沒有什麼在它,但爲了構建視圖的設置(例如,更改視圖的的白色背景)。

我無法在類之間進行通信,例如我需要使用DataProcessor類中的AE20125Controller類中的sendData方法發送數據,但我需要啓動它。但是DataProcessor類是在AE20125Controller類中啓動的?這也適用於MenuItems類,當按下updateFreq按鈕時,應調用DataProcessor類中的setData方法,但我需要啓動它。老實說,我不知道我在做什麼,如何正確地使用這個系統,以便一切正常,沒有把所有東西放在一個文件中,這樣我就可以保持它的組織。再次,我是編程新手,但是(如果我可以自己這樣說),我對編程有一定的瞭解。請幫助我組織我的(糟糕的)代碼,並提供一些有關如何避免這種情況的提示。

(如果你知道一個好的高級斯威夫特OSX編程指南/課程,請告訴我。(它並不必須是免費的))提前

感謝

回答

-1

的不同方式之間的通信您的應用程序中的部件與NSNotificationCenter配合使用。檢查這裏的文檔: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/

編輯:在這種情況下,我會建議讓你的溝通單身:

class Communicator:NSObject, ORSSerialPortDelegate { 
    class let sharedInstance = Communicator() 
} 

,因此您可以撥打:

Communicator.sharedInstance.sendData(...) 
+0

如何任何解釋?如示例 – sloeberGJ

+0

NSNotificationCenter可能不是正確的選擇。例如:https://davidnix.io/post/stop-using-nsnotificationcenter/ –

+1

您可能是對的,我用更好的方式編輯了我的帖子。 –