通过CDC COM端口向Arduino进行Windows COM通信

时间:2016-04-28 11:46:57

标签: c++ c arduino serial-communication cdc

我刚刚购买了SparkFun Pro Micro(https://www.sparkfun.com/products/12640)并试图在Windows 10上使用ReadFile和WriteFile与它进行通信。

我已经测试并运行了我的代码与Stellaris,Tiva,Arduino Mega,甚至是Arduino Leonardo几乎没有问题(它有效)。但是,我无法使用micro USB线和我自己的自定义程序从Pro Micro发送任何数据或在我的计算机上接收数据。我可以使用Arduino串行监视器来发送和接收数据。我也可以使用PuTTY终端。 Arduino IDE和PuTTY中的波特率似乎对使用Pro Micro发送/接收数据的能力没有影响。

我希望能够使用我自己的程序发送和接收数据,因为我将其用作数据记录,后处理和实时图形/显示的服务器。如果这个项目不需要更小的硬件包我会使用Arduino Mega,但遗憾的是这不是一个选择。

我正在使用Visual Studio 2015在Windows 10上进行编译。我还使用官方的Arduino IDE和SparkFuns插件/驱动程序,v1.6.7(更新为1.6.8,但问题相同)。 / p>

这是我连接到COM端口的代码,我尝试了各种波特率以及BAUD_XXXX宏:

*port = CreateFile(COM, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //CreateFile(TEXT("COM8:"), ...
if (*port == INVALID_HANDLE_VALUE){
    printf("Invalid handle\n");
    return(1);
}

/// COM Port Configuration
portDCB.DCBlength = sizeof(DCB);                         ///< Initialize the DCBlength member
GetCommState(*port, &portDCB);                           ///< Get the default port setting information.
/// Change the DCB structure settings
portDCB.BaudRate = 115200;                              ///< Current baud 
portDCB.fBinary = TRUE;                                   ///< Binary mode; no EOF check 
portDCB.fParity = FALSE;                                  ///< Disable parity checking 
portDCB.fOutxCtsFlow = FALSE;                             ///< No CTS output flow control 
portDCB.fOutxDsrFlow = FALSE;                             ///< No DSR output flow control 
portDCB.fDtrControl = DTR_CONTROL_DISABLE;                ///< Disable DTR flow control type 
portDCB.fDsrSensitivity = FALSE;                          ///< DSR sensitivity 
portDCB.fTXContinueOnXoff = TRUE;                         ///< XOFF continues Tx 
portDCB.fOutX = FALSE;                                    ///< No XON/XOFF out flow control 
portDCB.fInX = FALSE;                                     ///< No XON/XOFF in flow control 
portDCB.fErrorChar = FALSE;                               ///< Disable error replacement 
portDCB.fNull = FALSE;                                    ///< Disable null stripping 
portDCB.fRtsControl = RTS_CONTROL_DISABLE;                ///< Disable RTS flow control 
portDCB.fAbortOnError = FALSE;                            ///< Do not abort reads/writes on error
portDCB.ByteSize = 8;                                     ///< Number of bits/byte, 4-8 
portDCB.Parity = NOPARITY;                                ///< 0-4 = no, odd, even, mark, space 
portDCB.StopBits = ONESTOPBIT;                            ///< 0, 1, 2 = 1, 1.5, 2 

if (!SetCommState(*port, &portDCB)){
    printf("Error Configuring COM Port\n");
    return(1);
}

GetCommTimeouts(*port, &comTimeOut);

comTimeOut.ReadIntervalTimeout = 20;
comTimeOut.ReadTotalTimeoutMultiplier = 10;
comTimeOut.ReadTotalTimeoutConstant = 100;
comTimeOut.WriteTotalTimeoutMultiplier = 10;
comTimeOut.WriteTotalTimeoutConstant = 100;

SetCommTimeouts(*port, &comTimeOut);

我的读写功能:

char inChar(HANDLE port){
    char output = 0;
    DWORD noOfBytesRead = 0;
    int retval = ReadFile(port, &output, 1, &noOfBytesRead, NULL);
    if (retval == 0) {
        return (0);
    }
    return(output);
}

void outChar(HANDLE port, char output){
    DWORD bytesTransmitted = 0;
    char buffer[] = { output, 0 };
    WriteFile(port, buffer, 1, &bytesTransmitted, NULL);
}

我有这个来测试PC上的通信:

while (1) {
    outChar(portHandle, 'b');
    inchar = inChar(portHandle);
    printf("%c", inchar);
}

关于Arduino:

void setup(){Serial.begin(115200);}
void loop(){
    Serial.read();
    Serial.println('a');
    delay(10);
}

Rx LED在Arduino上闪烁着疯狂,但是Tx LED什么也没做,表示只有一种方式存在数据。我已经完成了其他测试,我发现Arduino正在读取正确的信息(如果输入字符是特定字符,则通过闪烁LED来测试)但是它不会向我的程序发送任何内容(PC端不使用Arduino时) IDE或PuTTY)。

在PuTTY中,无论Arduinos Serial.begin()如何,我都可以使用任何波特率启动COM通信。 8个数据位,1个停止位,无奇偶校验,无流量控制,与我在Visual Studio中的设置相同。

编辑: 我想如果我自己没有配置它,我只是捎带从PuTTy留下的COM配置,所以我修改了我的代码并删除了所有多余的:

/// COM Port Configuration
portDCB.DCBlength = sizeof(DCB);                         ///< Initialize the DCBlength member
GetCommState(*port, &portDCB);                           ///< Get the default port setting information.
/// Change the DCB structure settings
portDCB.BaudRate = 115200;                              ///< Current baud 
portDCB.ByteSize = 8;                                     ///< Number of bits/byte, 4-8 
portDCB.Parity = NOPARITY;                                ///< 0-4 = no, odd, even, mark, space 
portDCB.StopBits = ONESTOPBIT;                            ///< 0, 1, 2 = 1, 1.5, 2 
/*
portDCB.fBinary = TRUE;                                   ///< Binary mode; no EOF check 
portDCB.fParity = FALSE;                                  ///< Disable parity checking 
portDCB.fOutxCtsFlow = FALSE;                             ///< No CTS output flow control 
portDCB.fOutxDsrFlow = FALSE;                             ///< No DSR output flow control 
portDCB.fDtrControl = DTR_CONTROL_DISABLE;                ///< Disable DTR flow control type 
portDCB.fDsrSensitivity = FALSE;                          ///< DSR sensitivity 
portDCB.fTXContinueOnXoff = TRUE;                         ///< XOFF continues Tx 
portDCB.fOutX = FALSE;                                    ///< No XON/XOFF out flow control 
portDCB.fInX = FALSE;                                     ///< No XON/XOFF in flow control 
portDCB.fErrorChar = FALSE;                               ///< Disable error replacement 
portDCB.fNull = FALSE;                                    ///< Disable null stripping 
portDCB.fRtsControl = RTS_CONTROL_DISABLE;                ///< Disable RTS flow control 
portDCB.fAbortOnError = FALSE;                            ///< Do not abort reads/writes on error
*/

使用注释代码可以正常工作,但为什么呢?是什么让这款Pro Micro与我使用的其他微控制器如此不同?我将逐一检查它们,直到我发现哪个是负责任的,因为只有在首次打开和关闭PuTTY中的端口后才能连接(不方便)。

2 个答案:

答案 0 :(得分:2)

当您在Windows DCB结构中禁用RTS控件时,SparkFun Pro Micro不喜欢它。

问题通过以下方式解决:

portDCB.fRtsControl = RTS_CONTROL_ENABLE; //was RTS_CONTROL_DISABLE
portDCB.fOutxCtsFlow = TRUE;              //was FALSE

像往常一样,忽略数据表中的重要信息是一个错误,我花了几个小时阅读注册信息,试图确认我出错的地方或原因,答案很简单,因为看到的是USART设备的功能列表在数据表中:

USART:
...
• Flow control CTS/RTS signals hardware management
...

答案 1 :(得分:0)

char inChar(HANDLE port){
        char output = 0;
        DWORD noOfBytesRead = 0;
        int retval = ReadFile(port, &output, 1, &noOfBytesRead, NULL);
        if (retval == NULL) {
            return (NULL);
        }
        return(output);
    }

这是不正确的,因为您将retval(int为int)与NULL进行比较,并且您的函数返回NULL作为char函数的返回值。虽然我不相信这会导致报告的问题,但应该改变它。

查看已接受的答案here。我建议你从PC端的一个工作实例开始,然后根据你的需要减少它。