internal static MacSerialDevice TryCreate(NativeMethods.io_string_t path) { var d = new MacSerialDevice() { _path = path }; var handle = NativeMethods.IORegistryEntryFromPath(0, ref path).ToIOObject(); if (!handle.IsSet) { return(null); } using (handle) { d._fileSystemName = NativeMethods.IORegistryEntryGetCFProperty_String(handle, NativeMethods.kIOCalloutDeviceKey); if (d._fileSystemName == null) { return(null); } } return(d); }
internal MacSerialStream(MacSerialDevice device) : base(device) { string fileSystemName = device.GetFileSystemName(); int ret; int handle = NativeMethods.retry(() => NativeMethods.open(fileSystemName, NativeMethods.oflag.RDWR | NativeMethods.oflag.NOCTTY | NativeMethods.oflag.NONBLOCK)); if (handle < 0) { var error = (NativeMethods.error)Marshal.GetLastWin32Error(); if (error == NativeMethods.error.EACCES) { throw DeviceException.CreateUnauthorizedAccessException(device, "Not permitted to open serial device at " + fileSystemName + "."); } else { throw DeviceException.CreateIOException(device, "Unable to open serial device (" + error.ToString() + ")."); } } ret = NativeMethods.retry(() => NativeMethods.ioctl(handle, NativeMethods.TIOCEXCL)); if (ret < 0) { NativeMethods.retry(() => NativeMethods.close(handle)); throw new IOException("Unable to open serial device exclusively."); } /* * ret = NativeMethods.retry(() => NativeMethods.fcntl(handle, NativeMethods.F_SETFL, 0)); * if (ret < 0) * { * NativeMethods.retry(() => NativeMethods.ioctl(handle, NativeMethods.TIOCNXCL)); * NativeMethods.retry(() => NativeMethods.close(handle)); * throw new IOException("Unable to remove blocking from port."); * } */ ret = NativeMethods.retry(() => NativeMethods.tcgetattr(handle, out _oldSettings)); if (ret < 0) { NativeMethods.retry(() => NativeMethods.ioctl(handle, NativeMethods.TIOCNXCL)); NativeMethods.retry(() => NativeMethods.close(handle)); throw new IOException("Unable to get serial port settings."); } _newSettings = _oldSettings; NativeMethods.cfmakeraw(ref _newSettings); _handle = handle; InitSettings(); UpdateSettings(); }
protected override bool TryCreateSerialDevice(object key, out Device device) { device = MacSerialDevice.TryCreate((NativeMethods.io_string_t)key); return(device != null); }