在 Arduino 中将字符串转换为字符

Convert String To Char In Arduino

如何使用 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
}

这是一个非常肮脏的答案,但它应该可以帮助您入门:

// 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 设备发送它们.