Skip to content

Swift UART port and sockets for Raspberry Pi. Connect any UART device and debug it from your Mac.

Notifications You must be signed in to change notification settings

AleyRobotics/AleyRobotics_swift_serial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AleyRobotics swift 3.0 serial for Raspberry Pi and external UART devices.

Write cross platform swift code for Arm at Mac, compile it at ur Rpi

Use UART ports at Raspberry Pi as UART at your Mac with swift. Compile and run same code at Raspberry pi. Easy debuging UART protocols with remote socket connection at Mac throught Sockets and ip2Ser linux App.

For remote connect use little modified ip2Ser inside repository. Added more avalible UART speeds constants.

./ip2ser/ip2ser -d /dev/usb_uart_port_0 -b 230400 -p 20301 -R

Swift 3.0 init code for linux real UART or remote throught sokets at OSx.

#if os(Linux)
let uart = SomeUartDevice.init(portName: "/dev/usb_uart0")
#elseif os(OSX)
let uart = SomeUartDevice.init(host: "192.168.10.10", port: 20301)
#endif
if uart.start() == false {
    print("Error opening port \(uart.serialPort.path)")
}

Code for working with UART

import Foundation
import Dispatch

class SomeUartDevice: ARSocketDelegate {
    var serialPort: ARSerialPort!
    .......
    func start() -> Bool //Start reading thread
    func stop() // stop thread
    
    func processPacket(arrayData: inout [UInt8]) {
        //do what u want with recieved data
        print("recieved arrayData.count: \(arrayData.count)")
        print("recieved arrayData: \(arrayData)")
        
    }
    
    func createPacket() {
        packetToSend.removeAll()
        //add bytes for sending here
        packetToSend.append(0x55)
        packetToSend.append(0xAA)
    }
    
    func sendPacket() {
        createPacket()
        do {
            _ = try serialPort.writeByteArray(into: self.packetToSend)
        } catch {
            print("Error: \(error)")
        }
    }

About

Swift UART port and sockets for Raspberry Pi. Connect any UART device and debug it from your Mac.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published