I'm looking for sending serial commands to an OBD-II wi-fi device via my iPad.
In order to do it, I've use CocoaAsyncSocket
lib to connect my iPad to the OBD-II and it works fine. Then, I've send a command "010C/r" to the device to get the rpm of the car, but it doesn't work. I think I don't use the right syntax to do it, but I'm not sure.
Here is my Objective-C code:
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#define HOST @"192.168.0.10"
#define USE_SECURE_CONNECTION 0
#define VALIDATE_SSL_CERTIFICATE 1
#define READ_HEADER_LINE_BY_LINE 0
@implementation SimpleHTTPClientAppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDDispatchQueueLogFormatter *formatter = [[DDDispatchQueueLogFormatter alloc] init];
[formatter setReplacementString:@"socket" forQueueLabel:GCDAsyncSocketQueueName];
[formatter setReplacementString:@"socket-cf" forQueueLabel:GCDAsyncSocketThreadName];
[[DDTTYLogger sharedInstance] setLogFormatter:formatter];
asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
NSString *host = HOST;
#if USE_SECURE_CONNECTION
uint16_t port = 443; // HTTPS
#else
uint16_t port = 35000; // HTTP
#endif
if (![asyncSocket connectToHost:host onPort:port error:&error])
{
DDLogError(@"Unable to connect to due to invalid configuration: %@", error);
}
else
{
DDLogVerbose(@"Connecting to \"%@\" on port %hu...", host, port);
}
#if USE_SECURE_CONNECTION
#if VALIDATE_SSL_CERTIFICATE
{
DDLogVerbose(@"Requesting StartTLS with options: (nil)");
[asyncSocket startTLS:nil];
}
#else
{
NSDictionary *options =
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
DDLogVerbose(@"Requesting StartTLS with options:\n%@", options);
[asyncSocket startTLS:options];
}
#endif
#endif
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
DDLogVerbose(@"socket:didConnectToHost:%@ port:%hu", host, port);
NSString *requestStrFrmt = @"010C\r";
NSString *requestStr = [NSString stringWithFormat:requestStrFrmt, HOST];
NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
DDLogVerbose(@"Sending HTTP Request:\n%@", requestStr);
#if READ_HEADER_LINE_BY_LINE
[asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1.0 tag:0];
#else
NSData *responseTerminatorData = [@"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding];
[asyncSocket readDataToData:responseTerminatorData withTimeout:-1.0 tag:0];
#endif
}
- (void)socketDidSecure:(GCDAsyncSocket *)sock
{
DDLogVerbose(@"socketDidSecure:");
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
DDLogVerbose(@"socket:didWriteDataWithTag:");
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
DDLogVerbose(@"socket:didReadData:withTag:");
NSString *httpResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
#if READ_HEADER_LINE_BY_LINE
DDLogInfo(@"Line httpResponse: %@", httpResponse);
if ([data length] == 2)
{
DDLogInfo(@"<done>");
}
else
{
[asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1.0 tag:0];
}
#else
DDLogInfo(@"Full HTTP Response:\n%@", httpResponse);
#endif
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
DDLogVerbose(@"socketDidDisconnect:withError: \"%@\"", err);
}
@end
Here is the logs:
2014-05-28 17:02:05:559 [main] Connecting to "192.168.0.10" on port 35000...
2014-05-28 17:02:05:619 [main] socket:didConnectToHost:192.168.0.10 port:35000
2014-05-28 17:02:05:621 [main] Sending HTTP Request:
010C
2014-05-28 17:02:05:622 [main] socket:didWriteDataWithTag:
Thx