Newbie: how to talk to USB serial device from my app?

Hi,

I am starting to look at communicating with a USB interface which I understand behaves like a serial device. I am now looking at documentation and sample code to help me do that. I found some documentation, but it's all in the "archive" section of the developer documentation, which to me looks like it's more or less out of date.

Did I miss something? What would be some suggestions to start learning about doing that?

FWIW, I would like to communicate with some marine NMEA USB interfaces. NMEA 0183 but NMEA 2000 too. The ones I have are made by Digital Yacht and Yacht Devices.

Thanks

JD

Replies

Could you show the code you tried so far ?

If that can help, here is an example to retrieve information of an USB disk.

Code Block
import Foundation
func findRemovableVolumes() -> [URL] {
var allMountedURL = [URL]()
let keys: [URLResourceKey] = [.volumeNameKey, .volumeIsRemovableKey, .volumeIsEjectableKey]
let paths = FileManager().mountedVolumeURLs(includingResourceValuesForKeys: keys, options: [])
if let urls = paths {
for url in urls {
let components = url.pathComponents
if components.count > 1 && components[1] == "Volumes" {
allMountedURL.append(url)
}
}
}
return allMountedURL
}
func buildFileURL(driveUrl: URL) -> URL {
let fileName = "My file"
let fileURL = driveUrl.appendingPathComponent(fileName).appendingPathExtension("txt")
return fileURL
}
func getSerialAndSize(_ url: URL) -> (String, Int, Bool) {
var officialVendor = false
if let session = DASessionCreate(nil) {
if let disk : DADisk = DADiskCreateFromVolumePath(nil, session, url as CFURL) {
// We found a mount point...
let ioService : io_service_t = DADiskCopyIOMedia(disk)
let key = "USB Serial Number"
let options : IOOptionBits = IOOptionBits(kIORegistryIterateParents) |
IOOptionBits(kIORegistryIterateRecursively)
if let sSerial : CFTypeRef = IORegistryEntrySearchCFProperty(ioService, kIOServicePlane, key as CFString, nil, options) {
let dico: CFDictionary = DADiskCopyDescription(disk)!
let vendor = (dico as NSDictionary)["DADeviceVendor"] as? String ?? "--"
officialVendor = vendor == "VendorCo"
let driveCapacity = (dico as NSDictionary)["DAMediaSize"] as? Int ?? 0
return (String(describing: sSerial), driveCapacity, officialVendor)
} else {
return ("", 0, false)
}
}
}
return ("", 0, false)
}