From 066f585c534e5f15b28d3bfb66b790195bd848d2 Mon Sep 17 00:00:00 2001 From: fedor Date: Thu, 16 Mar 2023 10:22:27 -0400 Subject: [PATCH] Reformatted Serial.swift --- Sources/tart/Serial.swift | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/Sources/tart/Serial.swift b/Sources/tart/Serial.swift index 80fc12d..56249e0 100644 --- a/Sources/tart/Serial.swift +++ b/Sources/tart/Serial.swift @@ -1,52 +1,44 @@ -// -// File.swift -// -// -// Created by Peter Nguyen on 19/12/2022. -// - import Foundation -func createPTY() -> Int32 -{ +func createPTY() -> Int32 { var tty_fd: Int32 = -1 var sfd: Int32 = -1 var termios_ = termios() let tty_path = UnsafeMutablePointer.allocate(capacity: 1024) - + var res = openpty(&tty_fd, &sfd, tty_path, nil, nil); - if(res < 0 ){ + if (res < 0) { perror("openpty error") return -1 } - + // close slave file descriptor close(sfd) - + res = fcntl(tty_fd, F_GETFL) - if(res < 0){ + if (res < 0) { perror("fcntl F_GETFL error") return res } - + // set serial nonblocking res = fcntl(tty_fd, F_SETFL, res | O_NONBLOCK) - if(res < 0){ + if (res < 0) { perror("fcntl F_SETFL O_NONBLOCK error") return res } - + // set baudrate to 115200 tcgetattr(tty_fd, &termios_) cfsetispeed(&termios_, speed_t(B115200)) cfsetospeed(&termios_, speed_t(B115200)) - if(tcsetattr(tty_fd, TCSANOW, &termios_) != 0){ + if (tcsetattr(tty_fd, TCSANOW, &termios_) != 0) { perror("tcsetattr error") return -1 } - + print("Successfully open pty \(String(cString: tty_path))") - + tty_path.deallocate() return tty_fd }