【问题标题】:Convert String To Char In Arduino在 Arduino 中将字符串转换为字符
【发布时间】:2015-08-03 05:49:07
【问题描述】:

如何通过 CAN 总线将数据从一个 Arduino 板发送到另一个?

数据必须像这个例子

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10);                                      // Set CS to pin 10

void setup()
{
  Serial.begin(115200);
  // init can bus, baudrate: 500k
  if(CAN0.begin(CAN_500KBPS) == CAN_OK) Serial.print("can init ok!!\r\n");
  else Serial.print("Can init fail!!\r\n");
}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
  // send data:  id = 0x00, standrad flame, data len = 8, stmp: data buf
  CAN0.sendMsgBuf(0x00, 0, 8, stmp);  
  delay(100);                       // send data per 100ms
}

【问题讨论】:

    标签: string char arduino converter can-bus


    【解决方案1】:

    这是一个非常肮脏的答案,但它应该让你开始:

    // demo: CAN-BUS Shield, send data
    #include <mcp_can.h>
    #include <SPI.h>
    
    MCP_CAN CAN(10);                              
    
    void setup() {
        Serial.begin(115200);
    
        // Override the default 1 second timeout on the read.
        // I don't deal much with Arduino so I don't know how to set the read to blocking mode.
        Serial.setTimeout(999999); 
    
    START_INIT:
    
        if(CAN_OK == CAN.begin(CAN_500KBPS))
        {
            Serial.println("CAN BUS Shield init ok!");
        }
        else
        {
            Serial.println("CAN BUS Shield init fail");
            Serial.println("Init CAN BUS Shield again");
            delay(100);
            goto START_INIT;
        } }
    
    
    void loop()
    {
       // Create an array to hold our bytes that we'll send.
       char stmp[8] = {  0, 0, 0, 0, 0, 0, 0, 0  }; 
    
       Serial.println("Waiting for input...");
    
       // Wait till 8 bytes are read before sending.
       int bytes_read = Serial.readBytes(stmp, 8); 
    
       // Send the 8 byte "string".
       CAN.sendMsgBuf(0xFA, 0, bytes_read, (byte*)stmp); 
    }
    

    基本上,它会等待 999999 毫秒(因为我不知道如何将 arduino 设置为阻塞模式)让您输入 8 个字符,然后再将这些字符读入缓冲区并通过 CAN 设备发送。

    【讨论】:

    • Tnx。但我想从串口读取字符串并用 can bus 发送
    • 是的,没问题,谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2011-12-16
    • 2014-09-20
    相关资源
    最近更新 更多