Join/Login

Business Software

Open Source Software

For Vendors

Blog

About

More

Articles

Create

SourceForge Podcast

Site Documentation

Subscribe to our Newsletter

Support Request

For Vendors Help Create Join Login

Business Software

Open Source Software

SourceForge Podcast

Resources

Articles

Case Studies

Blog

Menu

Help

Create

Join

Login

Home

Browse

AMSerialPort

Code

AMSerialPort Code

Brought to you by: harmless-de

Summary

Files

Reviews

Support

Wiki

Code

Menu ▾ ▴

Browse Commits

Browse Files

[r35]: / trunk / AMSerialTest / AppController.m  Maximize  Restore  History

Download this file

187 lines (150 with data), 4.8 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

//

// AppController.m

// AMSerialTest

//

// 2009-09-09 Andreas Mayer

// - fixed memory leak in -serialPortReadData:

#import "AppController.h"

#import "AMSerialPortList.h"

#import "AMSerialPortAdditions.h"

@implementation AppController

- (void)awakeFromNib

{

[deviceTextField setStringValue:@"/dev/cu.modem"]; // internal modem

[inputTextField setStringValue: @"ati"]; // will ask for modem type

// register for port add/remove notification

NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self selector:@selector(didAddPorts:) name:AMSerialPortListDidAddPortsNotification object:nil];

[defaultCenter addObserver:self selector:@selector(didRemovePorts:) name:AMSerialPortListDidRemovePortsNotification object:nil];

[AMSerialPortList sharedPortList]; // initialize port list to arm notifications

}

- (AMSerialPort *)port

{

return port;

}

- (void)setPort:(AMSerialPort *)newPort

{

#if !__has_feature(objc_arc)

id old = nil;

if (newPort != port) {

old = port;

port = [newPort retain];

[old release];

}

#else

port = newPort;

#endif

}

- (void)initPort

{

NSString *deviceName = [deviceTextField stringValue];

if (![deviceName isEqualToString:[port bsdPath]]) {

[port close];

AMSerialPort* newPort = [[AMSerialPort alloc] init:deviceName withName:deviceName type:(NSString*)CFSTR(kIOSerialBSDModemType)];

#if !__has_feature(objc_arc)

[newPort autorelease];

#endif

[self setPort:newPort];

// register as self as delegate for port

[port setDelegate:self];

[outputTextView insertText:@"attempting to open port\r"];

[outputTextView setNeedsDisplay:YES];

[outputTextView displayIfNeeded];

// open port - may take a few seconds ...

if ([port open]) {

[outputTextView insertText:@"port opened\r"];

[outputTextView setNeedsDisplay:YES];

[outputTextView displayIfNeeded];

// listen for data in a separate thread

[port readDataInBackground];

} else { // an error occured while creating port

[outputTextView insertText:@"couldn't open port for device "];

[outputTextView insertText:deviceName];

[outputTextView insertText:@"\r"];

[outputTextView setNeedsDisplay:YES];

[outputTextView displayIfNeeded];

[self setPort:nil];

}

}

}

- (void)serialPortReadData:(NSDictionary *)dataDictionary

{

// this method is called if data arrives

// @"data" is the actual data, @"serialPort" is the sending port

AMSerialPort *sendPort = [dataDictionary objectForKey:@"serialPort"];

NSData *data = [dataDictionary objectForKey:@"data"];

if ([data length] > 0) {

NSString *text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

[outputTextView insertText:text];

#if !__has_feature(objc_arc)

[text release];

#endif

// continue listening

[sendPort readDataInBackground];

} else { // port closed

[outputTextView insertText:@"port closed\r"];

}

[outputTextView setNeedsDisplay:YES];

[outputTextView displayIfNeeded];

}

- (void)didAddPorts:(NSNotification *)theNotification

{

[outputTextView insertText:@"didAddPorts:"];

[outputTextView insertText:@"\r"];

[outputTextView insertText:[[theNotification userInfo] description]];

[outputTextView insertText:@"\r"];

[outputTextView setNeedsDisplay:YES];

}

- (void)didRemovePorts:(NSNotification *)theNotification

{

[outputTextView insertText:@"didRemovePorts:"];

[outputTextView insertText:@"\r"];

[outputTextView insertText:[[theNotification userInfo] description]];

[outputTextView insertText:@"\r"];

[outputTextView setNeedsDisplay:YES];

}

- (IBAction)listDevices:(id)sender

{

(void)sender;

// get an port enumerator

NSEnumerator *enumerator = [AMSerialPortList portEnumerator];

AMSerialPort *aPort;

while ((aPort = [enumerator nextObject])) {

// print port name

[outputTextView insertText:[aPort name]];

[outputTextView insertText:@":"];

[outputTextView insertText:[aPort bsdPath]];

[outputTextView insertText:@"\r"];

}

[outputTextView setNeedsDisplay:YES];

}

- (IBAction)chooseDevice:(id)sender

{

(void)sender;

// new device selected

[self initPort];

}

- (IBAction)send:(id)sender

{

(void)sender;

NSString *sendString = [[inputTextField stringValue] stringByAppendingString:@"\r"];

if(!port) {

// open a new port if we don't already have one

[self initPort];

}

if([port isOpen]) { // in case an error occured while opening the port

[port writeString:sendString usingEncoding:NSUTF8StringEncoding error:NULL];

}

}

- (IBAction)sendSerialBreak:(id)sender

{

(void)sender;

if(!port) {

// open a new port if we don't already have one

[self initPort];

}

if([port isOpen]) { // in case an error occured while opening the port

[port sendBreak];

}

}

@end

SourceForge

Create a Project

Open Source Software

Business Software

Top Downloaded Projects

Company

About

Team

SourceForge Headquarters
225 Broadway Suite 1600
San Diego, CA 92101
+1 (858) 422-6466

Resources

Support

Site Documentation

Site Status

SourceForge Reviews

© 2025 Slashdot Media. All Rights Reserved.

Terms Privacy Opt Out Advertise

×
Want the latest updates on software, tech news, and AI?

Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.

Submit