c++ - 在 mac 上使用带有 Xcode 的 c++ 打开带有 arduino 的串口

标签 c++ xcode serialization serial-port arduino

我用 C++ 编写了一个简单的程序,它通过串行端口向 Arduino 发送一个角度值; Arduino 而不是使用该值来控制伺服电机。

这是c++代码

#include <iostream>
#include <unistd.h>
#include <fstream>

using namespace std;

int main()
{
    unsigned int angle;
    fstream arduino;
    
    cout<<"check-1";

    arduino.open("/dev/tty.usbmodem3a21");

    cout<<"check-2";
    
    if(arduino)
    {
        do
        {
            cout<<"\n\ninsert a number between 0 and 179";
        
            cin>>angle;
            arduino<<angle;
            
        }while(angle <= 179);
        
        arduino.close();
    }
    else
    {
        cout<<"\n\nERROR!!\n\n";
    }
    
    
}

这是arduino的:

#include <Servo.h>

Servo servo;
const int pinServo = 2;
unsigned int angle;

void setup()
{
    Serial.begin(9600);
    servo.attach(pinServo);
    
    servo.write(0);
    
}

void loop()
{
    if(Serial.available()>0)
    {  
       angle = Serial.read();
       
       if(angle <= 179)
       {
         servo.write(angle);
       }
    }
}

问题是它在 arduino.open(...) 处停止。尽管我在 tools > serial 端口中检查了 Arduino 应用程序中选择的端口是否正确,但它甚至没有打印出“check-1”。

为了添加更多有用的信息,我测试了使用 iOS::binary 参数打开端口,因此打开命令变为 arduino.open("/dev/tty.usbmodem3a21",iOS::二进制);结果,它打印出“check-1”、“check-2”和“ERROR!!”。如果我输入错误的串行端口名称,我会得到相同的结果。

为什么会发生这种情况,我该如何解决?

最佳答案

arduino 显示为串行设备。您应该查看使用 open()close() 函数。我在 Linux 上完成了这个,但我很确定这在 Mac 上也同样有效。这是一个示例代码片段。第一个片段打开并设置文件描述符。

int fd;                             // File descriptor
// Open port
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1){
    printf("Device cannot be opened.\n");
    exit(-1);                       // If the device is not open, return -1
}
struct termios options;

fcntl(fd, F_SETFL, FNDELAY);                    // Open the device in nonblocking mode

// Set parameters
tcgetattr(fd, &options);                        // Get the current options of the port
bzero(&options, sizeof(options));               // Clear all the options
speed_t         Speed;
switch (baudRate)                               // Set the speed (baudRate)
{
    case 110  :     Speed=B110; break;
    case 300  :     Speed=B300; break;
    case 600  :     Speed=B600; break;
    case 1200 :     Speed=B1200; break;
    case 2400 :     Speed=B2400; break;
    case 4800 :     Speed=B4800; break;
    case 9600 :     Speed=B9600; break;
    case 19200 :    Speed=B19200; break;
    case 38400 :    Speed=B38400; break;
    case 57600 :    Speed=B57600; break;
    case 115200 :   Speed=B115200; break;
    default : exit(-4);
}
cfsetispeed(&options, Speed);                   // Set the baud rate at 115200 bauds
cfsetospeed(&options, Speed);
options.c_cflag |= ( CLOCAL | CREAD |  CS8);    // Configure the device : 8 bits, no parity, no control
options.c_iflag |= ( IGNPAR | IGNBRK );
options.c_cc[VTIME]=0;                          // Timer unused
options.c_cc[VMIN]=0;                           // At least on character before satisfy reading
tcsetattr(fd, TCSANOW, &options);               // Activate the settings

这只是关闭它:

close(fd); 

从实际的文件描述符中读取:

ioctl(fd, FIONREAD, &t1);                           
if(t1 > 0) {
    // If the number of bytes read is equal to the number of bytes retrieved
    if(read(fd,pByte, t1) == t1) {  
        for(int i =0; i < t1; i++) {
            if(pByte[i] != '\r'){ // Just makes sure you're not scanning new lines
                // TODO: Do what you want with this character
            }
        }
    }
}

关于c++ - 在 mac 上使用带有 Xcode 的 c++ 打开带有 arduino 的串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27609972/

相关文章:

c++ - 在 for 循环中重新声明对象 - C++

ios - 安装 Xcode 11 后无法在模拟器上运行应用程序 - CFBundleVersion 错误

ios - Xcode 中的时间分析器缺少记录设置、显示设置

python - 扭曲的 self.transport.write() - Python - 附加输出

machine-learning - 如何将神经网络模型转换为独立函数源代码

Django REST Framework - 对嵌套序列化程序的查询限制?

c++ - 表达式必须具有整数或枚举类型 Char*

c++ - 创建高阶映射函数时,我应该使用指针、引用还是值?

java - 序列化哈希表,Java

c++ - 是否有任何程序/方式来模拟 arduino 串行端口并监视正在发送的内容