sltalex 发表于 2014-5-12 16:10:02

接收串口16进制处理问题

本帖最后由 sltalex 于 2014-5-12 17:01 编辑

String ck = "";

while(Serial.available() > 0)
{
    ck += char(Serial.read());
    delay(2);
    mark =1;
}
if(mark == 1)
{
if(ck==0x06A33A2EFF12){
    fid = 0;
      if(digitalRead(41)==HIGH){
      delay(200);
      fid = 1;
      }else{
      fid = 0;
      }
      switch(fid){
      case 0: digitalWrite(41, LOW);break;
      case 1: digitalWrite(41, HIGH);break;
      }
      delay(2);
      }
}
ck = String("");
mark = 0;

串口接收16进制06A33A2EFF12后进入判断。调试的时候,出现一下错误。



zonghe:63: error: integer constant is too large for 'long' type
zonghe:79: error: integer constant is too large for 'long' type
zonghe:94: error: integer constant is too large for 'long' type
zonghe.ino: In function 'void loop()':
zonghe:63: error: invalid conversion from 'long long int' to 'const char*'
zonghe:63: error: initializing argument 1 of 'unsigned char String:perator==(const char*) const'
zonghe:79: error: invalid conversion from 'long long int' to 'const char*'
zonghe:79: error: initializing argument 1 of 'unsigned char String:perator==(const char*) const'
zonghe:94: error: invalid conversion from 'long long int' to 'const char*'
zonghe:94: error: initializing argument 1 of 'unsigned char String:perator==(const char*) const'



是不是定义上就有问题

Super169 发表于 2014-5-12 16:56:04

48 bit 既 consultant 當然太大喇.ck 是 string, 你想比較 string,但用了數值, string 是要用 "" 包好的.
而且, 如果傳送來的是 16位數值, 用ck += char(Serial.read()); 也不會得到你想要的東西.
你知道送來 10 個 0x41, 你接收後 ck 會是什麼嗎?

之前我也出過一個 post 談及 serial communication 中數值及字符的分別, 看來大家都不感興趣呢.

sltalex 发表于 2014-5-12 17:01:24

后来我用ckck单独取出做了对比。估计也只能这样了。谢谢楼上的

Super169 发表于 2014-5-12 17:05:23

sltalex 发表于 2014-5-12 17:01 static/image/common/back.gif
后来我用ckck单独取出做了对比。估计也只能这样了。谢谢楼上的

如果要對比一連串的16位, 直接放到一個 byte array 中, 對比當中的值就可以不受 單一數值的最大值限制了, 要對比 12個 hex 也不是問題.

你現用把它轉成 char, 要小心有部份的數值可能會有改變.而且, string 中遇上 0x00 也會有問題的.

sltalex 发表于 2014-5-12 17:08:16

byte array 没使用过,可以举个简单的例子吗。
页: [1]
查看完整版本: 接收串口16进制处理问题