加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用带有Xcode的c使用arduino打开一个串口

发布时间:2020-12-14 19:04:27 所属栏目:百科 来源:网络整理
导读:我已经提出了类似的问题.这是一个简单的程序,它通过一个串口向arduino发送一个角度,而arduino将这个角度应用于伺服电机. 这是c代码 #include iostream#include unistd.h#include fstreamusing namespace std;int main(){ unsigned int angle; fstream arduin
我已经提出了类似的问题.这是一个简单的程序,它通过一个串口向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<<"nninsert a number between 0 and 179";

            cin>>angle;
            arduino<<angle;

        }while(angle <= 179);

        arduino.close();
    }
    else
    {
        cout<<"nnERROR!!nn";
    }


}

这是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(…),我不知道为什么,它甚至不写“check-1”,我也确定这是在arduino中选择的端口app中的工具>串口.

知道如果我写arduino.open(“/ dev / tty.usbmodem3a21”,iOS :: binary)或写错了串口名,它写“check-1”,“check-2”也是有用的.和“错误!!”,所以错误有效.

解决方法

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
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读