Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c+;与arduino一起打开串行端口+;在mac上使用Xcode 我编写了一个简单的C++程序,它通过串口向ARDUINO发送角度值;Arduino than使用该值控制伺服电机 这是C++代码< /P> #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"; } } #包括 #包括 #包括 使用名称空间std; int main() { 无符号整数角; 阿杜伊诺; cout_C++_Xcode_Serialization_Serial Port_Arduino - Fatal编程技术网

使用c+;与arduino一起打开串行端口+;在mac上使用Xcode 我编写了一个简单的C++程序,它通过串口向ARDUINO发送角度值;Arduino than使用该值控制伺服电机 这是C++代码< /P> #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"; } } #包括 #包括 #包括 使用名称空间std; int main() { 无符号整数角; 阿杜伊诺; cout

使用c+;与arduino一起打开串行端口+;在mac上使用Xcode 我编写了一个简单的C++程序,它通过串口向ARDUINO发送角度值;Arduino than使用该值控制伺服电机 这是C++代码< /P> #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"; } } #包括 #包括 #包括 使用名称空间std; int main() { 无符号整数角; 阿杜伊诺; cout,c++,xcode,serialization,serial-port,arduino,C++,Xcode,Serialization,Serial Port,Arduino,arduino显示为一个串行设备。您应该看看如何使用open()和close()函数。我在Linux上做过这项工作,但我非常确定这在Mac上也同样有效。这是一个示例代码段。第一个代码段打开并设置文件描述符 int fd; // File descriptor // Open port fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1){ pri

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
            }
        }
    }
}
ioctl(fd、FIONREAD和t1);
如果(t1>0){
//如果读取的字节数等于检索的字节数
如果(读(fd,pByte,t1)=t1){
对于(int i=0;i在C++中用程序发送命令之前,首先要做的是串行端口被正确配置,您只需具有串行端口闭环,您发送的所有东西都应该接收,如果您没有收到任何有串行端口配置问题的信息,请确保它在<代码>打开时停止。
如果它甚至没有打印第一张支票?这个问题与-您需要设置波特率并关闭硬件流量控制相同。
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
            }
        }
    }
}