使用char作为运算符?

时间:2017-01-24 23:49:46

标签: android arduino

我正在制作一个Android应用程序+ arduino,它将从arduino接收ir代码并通过蓝牙将results.value(ir解码)发送到android。在android方面,我已经收到代码作为一个字符串,导致(例如)92c0然后做了一个测试按钮,将其发送回arduino并触发它通过irsend.sendNEC(0x92c0, 32)问题将ir代码发送到设备当我从Android应用程序接收代码时,我必须通过char data接收代码,如何使用data这是一个字符,并将其用作irsend.sendN中的0x92c0的替代在下面草绘:

 #include <IRremote.h>
 #include <SoftwareSerial.h>



 SoftwareSerial bluetoothPort(4,5);
 const int RECV_PIN = 12;
 char data = "0";
 const int SEND_PIN = 13;

 IRsend irsend;


 IRrecv irrecv(RECV_PIN);

 decode_results results;
 int BTval;
 int IRval;



 void setup() 
 {
   bluetoothPort.begin(9600);
   Serial.begin(9600);
   irrecv.enableIRIn();

 }




 void loop() 
 {
   if(bluetoothPort.available() > 0)
   {
     data = bluetoothPort.read();
     Serial.print(data);
     irsend.sendNEC(operator[data],32);
     irrecv.resume();
   }
   if(irrecv.decode(&results))
   {
     Serial.println(results.value);
     int set = results.value;
     bluetoothPort.println(results.value, HEX);
     irrecv.resume();


   }
 }

1 个答案:

答案 0 :(得分:0)

这里的问题是你不能将0x92c0作为char(或字节)传输。仅仅因为它不是一个字节,而是两个。

现在,我对Android并不多,所以我需要看到android代码来制作一个真正的解决方案你可以通过三种方式处理这个问题:

  1. 二进制传输数据(2个字节)
  2. 将数据作为字符串传输(5个字节)
  3. 索引数组中可能的回复并传输索引(仅适用于少量代码)。
  4. 我告诉你最后2个,因为第1个是最有效但我认为你没有正确的知识从android发送和接收二进制格式的数据(当然我没有它)

    因此,如果数据以字符串格式传输(与上传数据的方式相同),您将收到更多字节:

    • 1到4个十六进制数字
    • 1回车

    代码只是将您收到的字节存储在一个变量中,然后在收到CR或LF时发送它:

    // Outside the loop function
    uint16_t receivedData;
    
    // Inside the loop function
    if(bluetoothPort.available() > 0)
    {
        data = bluetoothPort.read();
    
        if ((data >= '0') && (data <= '9'))
        { // If it is a digit between 0 and 9 (in ascii)
            receivedData = (receivedData << 4) | (data - '0');
        }
        else if ((data >= 'A') && (data <= 'F'))
        { // If it is a digit between A and F (in ascii)
            receivedData = (receivedData << 4) | (data - 'A' + 10);
        }
        else if ((data >= 'a') && (data <= 'f'))
        { // Lowercase case
            receivedData = (receivedData << 4) | (data - 'a' + 10);
        }
        else if (((data == '\r') || (data == '\n')) && (receivedData > 0))
        { // I tend to consider both CR and LF, because windows always screws this
            Serial.print(receivedData, HEX);
            irsend.sendNEC(receivedData,32); // Not sure about the 32 here...
            irrecv.resume();
            receivedData = 0;
        }
        else
            receivedData = 0; // Something went wrong, just reset the variable
    }
    

    如果您只需要发送一些代码,则可以存储它们,然后只传输正确的索引。例如:

    // Outside the loop function
    uint16_t possibleCodes[] = { 0x92c0, 0x8238, 0x5555 };
    
    // Inside the loop function
    if(bluetoothPort.available() > 0)
    {
        data = bluetoothPort.read();
        // If you are using string transmission, use the following
        // line to get the correct value
        // data = bluetoothPort.read() - '0';
    
        if (data < sizeof(possibleCodes) / sizeof(possibleCodes[0]))
        {
            Serial.print(possibleCodes[data], HEX);
            irsend.sendNEC(possibleCodes[data],32); // Not sure about the 32 here...
            irrecv.resume();
        }
    }
    

    例如在这种情况下发送0x92c0,你必须从android发送值0。

相关问题