forked from armadsen/ORSSerialPort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
202 lines (170 loc) · 6.46 KB
/
main.m
File metadata and controls
202 lines (170 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// main.m
// ORSSerialPortCommandLineDemo
//
// Created by Andrew Madsen on 12/11/12.
// Copyright (c) 2011-2012 Andrew R. Madsen (andrew@openreelsoftware.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@import Foundation;
#import "ORSSerialPort.h"
#import "ORSSerialPortManager.h"
@interface ORSSerialPortHelper : NSObject <ORSSerialPortDelegate>
@property (nonatomic, strong) ORSSerialPort *serialPort;
@end
typedef NS_ENUM(NSUInteger, ORSApplicationState) {
ORSInitializationState = 0,
ORSWaitingForPortSelectionState,
ORSWaitingForBaudRateInput,
ORSWaitingForUserInputState,
};
static ORSApplicationState gCurrentApplicationState = ORSInitializationState;
static ORSSerialPortHelper *serialPortHelper = nil;
void printIntroduction(void)
{
printf("This program demonstrates the use of ORSSerialPort\n");
printf("in a Foundation-based command-line tool.\n");
printf("Please see http://github.com/armadsen/ORSSerialPort/\nor email andrew@openreelsoftware.com for more information.\n\n");
}
void printPrompt(void)
{
printf("\n> ");
}
void listAvailablePorts(void)
{
printf("\nPlease select a serial port: \n");
ORSSerialPortManager *manager = [ORSSerialPortManager sharedSerialPortManager];
NSArray *availablePorts = manager.availablePorts;
[availablePorts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
ORSSerialPort *port = (ORSSerialPort *)obj;
printf("%lu. %s\n", (unsigned long)idx, [port.name UTF8String]);
}];
printPrompt();
}
void promptForBaudRate(void)
{
printf("\nPlease enter a baud rate:");
}
BOOL setupAndOpenPortWithSelectionString(NSString *selectionString)
{
selectionString = [selectionString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSCharacterSet *invalidChars = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([selectionString rangeOfCharacterFromSet:invalidChars].location != NSNotFound) return NO;
ORSSerialPortManager *manager = [ORSSerialPortManager sharedSerialPortManager];
NSArray *availablePorts = manager.availablePorts;
NSInteger index = [selectionString integerValue];
index = MIN(MAX(index, 0), [availablePorts count]-1);
ORSSerialPort *port = [availablePorts objectAtIndex:index];
serialPortHelper = [[ORSSerialPortHelper alloc] init];
serialPortHelper.serialPort = port;
port.delegate = serialPortHelper;
[port open];
return YES;
}
BOOL setBaudRateOnPortWithString(NSString *string)
{
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSCharacterSet *invalidChars = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([string rangeOfCharacterFromSet:invalidChars].location != NSNotFound) return NO;
NSInteger baudRate = [string integerValue];
serialPortHelper.serialPort.baudRate = @(baudRate);
printf("Baud rate set to %li", (long)baudRate);
return YES;
}
void handleUserInputData(NSData *dataFromUser)
{
NSString *string = [[NSString alloc] initWithData:dataFromUser encoding:NSUTF8StringEncoding];
if ([string rangeOfString:@"exit" options:NSCaseInsensitiveSearch].location == 0 ||
[string rangeOfString:@"quit" options:NSCaseInsensitiveSearch].location == 0)
{
printf("Quitting...\n");
exit(0);
return;
}
switch (gCurrentApplicationState) {
case ORSWaitingForPortSelectionState:
if (!setupAndOpenPortWithSelectionString(string))
{
printf("\nError: Invalid port selection.");
listAvailablePorts();
return;
}
promptForBaudRate();
gCurrentApplicationState = ORSWaitingForBaudRateInput;
break;
case ORSWaitingForBaudRateInput:
if (!setBaudRateOnPortWithString(string))
{
printf("\nError: Invalid baud rate. Baud rate should consist only of numeric digits.");
promptForBaudRate();
return;
}
gCurrentApplicationState = ORSWaitingForUserInputState;
printPrompt();
break;
case ORSWaitingForUserInputState:
[serialPortHelper.serialPort sendData:dataFromUser];
printPrompt();
break;
default:
break;
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
printIntroduction();
if ([[[ORSSerialPortManager sharedSerialPortManager] availablePorts] count] == 0)
{
printf("No connected serial ports found. Please connect your USB to serial adapter(s) and run the program again.\n\n");
return 0;
}
listAvailablePorts();
gCurrentApplicationState = ORSWaitingForPortSelectionState;
NSFileHandle *standardInputHandle = [NSFileHandle fileHandleWithStandardInput];
standardInputHandle.readabilityHandler = ^(NSFileHandle *fileHandle) { handleUserInputData([fileHandle availableData]); };
[[NSRunLoop currentRunLoop] run]; // Required to receive data from ORSSerialPort and to process user input
// Cleanup
standardInputHandle.readabilityHandler = nil;
serialPortHelper = nil;
}
return 0;
}
@implementation ORSSerialPortHelper
- (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data
{
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
printf("\nReceived: \"%s\" %s", [string UTF8String], [[data description] UTF8String]);
printPrompt();
}
- (void)serialPortWasRemovedFromSystem:(ORSSerialPort *)serialPort
{
self.serialPort = nil;
}
- (void)serialPort:(ORSSerialPort *)serialPort didEncounterError:(NSError *)error
{
NSLog(@"%s %@ %@", __PRETTY_FUNCTION__, serialPort, error);
}
- (void)serialPortWasOpened:(ORSSerialPort *)serialPort
{
printf("Serial port %s was opened", [serialPort.name UTF8String]);
printPrompt();
}
@end