我正在寻找通过iPad向OBD-II wi-fi设备发送串行命令。
为了做到这一点,我使用CocoaAsyncSocket
lib将我的iPad连接到OBD-II,它运行正常。然后,我向设备发送命令“010C / r”以获得汽车的转速,但它不起作用。我想我没有使用正确的语法来做到这一点,但我不确定。
这是我的Objective-C代码:
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
这是日志:
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
答案 0 :(得分:0)
尝试'010C \ r'而不是'010C / r'。您正在使用回车,但斜线应该是另一种方式!
OBD-II协议使用CR作为结束突击队,如果你不发送它,它会继续听,但是当你等了很长时间时它会超时,我相信。需要检查一下。
答案 1 :(得分:0)
根据V.250,AT命令以\ r \ n。
终止