0

iPad経由でシリアルコマンドをOBD-II Wi-Fiデバイスに送信する方法を探しています。

それを行うために、CocoaAsyncSocketlib を使用して 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:

どうも

4

2 に答える 2

0

「010C/r」の代わりに「010C\r」を試してください。キャリッジ リターンを正しく使用していますが、スラッシュは逆にする必要があります。

OBD-II プロトコルは CR を終了コマンドとして使用します。送信しない場合はリッスンし続けますが、長時間待機するとタイムアウトになるはずです。それを確認する必要があります。

于 2014-05-29T16:46:52.050 に答える
0

V.250 に従って、AT コマンドは \r\n で終了します。

于 2016-08-18T19:37:49.140 に答える