极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9640|回复: 4

接收串口16进制处理问题

[复制链接]
发表于 2014-5-12 16:10:02 | 显示全部楼层 |阅读模式
本帖最后由 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'



是不是定义上就有问题
回复

使用道具 举报

发表于 2014-5-12 16:56:04 | 显示全部楼层
48 bit 既 consultant 當然太大喇.  ck 是 string, 你想比較 string,  但用了數值, string 是要用 "" 包好的.
而且, 如果傳送來的是 16位數值, 用  ck += char(Serial.read()); 也不會得到你想要的東西.
你知道送來 10 個 0x41, 你接收後 ck 會是什麼嗎?

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

使用道具 举报

 楼主| 发表于 2014-5-12 17:01:24 | 显示全部楼层
后来我用ck[3]ck[5]单独取出做了对比。估计也只能这样了。谢谢楼上的
回复 支持 反对

使用道具 举报

发表于 2014-5-12 17:05:23 | 显示全部楼层
sltalex 发表于 2014-5-12 17:01
后来我用ck[3]ck[5]单独取出做了对比。估计也只能这样了。谢谢楼上的

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

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

使用道具 举报

 楼主| 发表于 2014-5-12 17:08:16 | 显示全部楼层
byte array 没使用过,可以举个简单的例子吗。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-22 23:21 , Processed in 0.040652 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表