Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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
与Arduino和C+的串行端口通信+; 我在ARDUINO Nano和C++之间有一个串口通信的问题,尽管问题是在C++方面。基本上,我想把整数(或长,…)从ARDUNO发送到C++程序进行处理。p>_C++_Serial Port_Arduino - Fatal编程技术网

与Arduino和C+的串行端口通信+; 我在ARDUINO Nano和C++之间有一个串口通信的问题,尽管问题是在C++方面。基本上,我想把整数(或长,…)从ARDUNO发送到C++程序进行处理。p>

与Arduino和C+的串行端口通信+; 我在ARDUINO Nano和C++之间有一个串口通信的问题,尽管问题是在C++方面。基本上,我想把整数(或长,…)从ARDUNO发送到C++程序进行处理。p>,c++,serial-port,arduino,C++,Serial Port,Arduino,首先,我做了一个测试,使用Matlab将信息从Arduino发送到计算机。Arduino代码非常简单: int i = 0; void setup() { // start serial port at 9600 bps: Serial.begin(9600); establishContact(); } void loop() { Serial.println(i); i=i+1; delay(10); } void establishContact

首先,我做了一个测试,使用Matlab将信息从Arduino发送到计算机。Arduino代码非常简单:

int i = 0;

void setup() {

   // start serial port at 9600 bps:
   Serial.begin(9600);
   establishContact(); 
}

void loop() {
  Serial.println(i);
  i=i+1;  
  delay(10);
}

void establishContact() {
   while (Serial.available() <= 0) {
     Serial.println('A', BYTE);
     delay(10);
   }
}
inti=0;
无效设置(){
//以9600 bps的速度启动串行端口:
Serial.begin(9600);
建立联系();
}
void循环(){
序列号println(i);
i=i+1;
延迟(10);
}
无效联系人(){

while(Serial.available()如果我正确理解了您的问题,避免“后处理”的一种方法是将传递到
ReadFile
的指针移动到可用数据的末尾,这样
ReadFile
调用将附加到缓冲区,而不是覆盖

基本上,您将有两个指针。一个指向缓冲区,另一个指向缓冲区中数据的结尾。因此,当程序启动时,两个指针将是相同的。现在,您读取前2个字节。您将数据指针的结尾增加2。您进行另一次读取,但不是
szBuff
,而是将指针传递到上一个字节的结尾自动读取数据。您读取接下来的三个字节,在
szBuff
中有完整的条目

如果需要等待接收到某个分隔符来标记条目的结尾,则可以在接收到的数据中搜索它。如果该数据不存在,则继续读取直到找到它。如果存在,则可以返回

// Fill the buffer with 0
char szBuff[256] = {0};
// We have no data in the buffer, so the end of data points to the beginning 
// of the buffer.
char* szEndOfData = szBuff; 
while (i < maxSamples)
{
    WaitCommEvent (hSerial, &dwCommStatus, 0);

    if (dwCommStatus & EV_RXCHAR) 
    {
        // Append up to 4 bytes from the serial port to the buffer
        ReadFile(hSerial, LPVOID(szEndOfData), 4, &dwBytesRead, NULL);
        // Increment the end of data pointer, so it points to the end of the
        // data available in the buffer.
        szEndOfData += dwBytesRead;

        cout<<szBuff;
        printf(" - %d - \n", atoi(szBuff));
    }
    i++;     
}

// Output, assuming what you mentioned happens:
// - 88 -
// - 8889 -
//用0填充缓冲区
char szBuff[256]={0};
//缓冲区中没有数据,因此数据的结尾指向开始
//缓冲区的一部分。
char*szEndOfData=szBuff;
而(i
int i = 0;  
DWORD dwBytesRead = 0;
DWORD dwCommStatus = 0;
char szBuff[2] = "";                
int maxRead = 20;   
int sizeNum = 1;    
int *num    = (int*)malloc(maxRead*sizeof(int)); 
char *currNum;
char *pastNum;

// Write something into the Serial Port to start receive 
// information from the Arduino
WriteFile(hSerial, (LPCVOID)"A\0", 1, &dwBytesRead, NULL);    
SetCommMask(hSerial, EV_RXCHAR);

// Start reading from the Serial Port
while ( i < maxRead )
{
    WaitCommEvent (hSerial, &dwCommStatus, 0);

    if (dwCommStatus & EV_RXCHAR) // if a char is received in the serial port
    {
        ReadFile(hSerial, LPVOID(szBuff), 1, &dwBytesRead, NULL);

        if ( szBuff[0] > 47 && szBuff[0] < 58 )
        {
            sizeNum++;
            if (sizeNum ==2)
            {
                currNum = (char*)malloc(sizeNum*sizeof(char));
                strcpy(currNum, szBuff);
            } else
            {
                if (pastNum != NULL)
                    free(pastNum);
                pastNum = currNum;
                currNum = (char*)malloc(sizeNum*sizeof(char));
                strcpy(currNum, pastNum);
                strcpy(currNum+(sizeNum-2)*sizeof(char), szBuff);
            }

            cout << szBuff<<endl;   
        } else if (szBuff[0] == '\n' && sizeNum > 1) // end of number
        {
            num[i] = atoi(currNum);
            i++;                    

            sizeNum = 1;
            if (currNum!=NULL)
                free(currNum);
        }
    }
}
inti=0;
DWORD-dwBytesRead=0;
DWORD dwCommStatus=0;
char szBuff[2]=“”;
int maxRead=20;
int-sizeNum=1;
int*num=(int*)malloc(maxRead*sizeof(int));
char*currNum;
char*pastNum;
//将某些内容写入串行端口以启动接收
//来自Arduino的信息
WriteFile(hSerial,(LPCVOID)“A\0”、1和dwBytesRead,NULL);
SetCommMask(串行、EV_RXCHAR);
//从串行端口开始读取
而(i47&&szBuff[0]<58)
{
sizeNum++;
如果(sizeNum==2)
{
currNum=(char*)malloc(sizeNum*sizeof(char));
strcpy(currNum,szBuff);
}否则
{
if(pastNum!=NULL)
免费(pastNum);
pastNum=currNum;
currNum=(char*)malloc(sizeNum*sizeof(char));
strcpy(currNum,pastNum);
strcpy(currNum+(sizeNum-2)*sizeof(char),szBuff);
}

如果你在格式化这篇文章,可能会有帮助吗?我对格式化更多的文本没有问题,但我不确定你指的是什么。代码,“解释”或者两者都有?PS:请注意解释中的引号。我不确定我是否很好地解释了这个问题。只要使用原始API,就无法避免它。带有实现ReadLine()的串行端口包装器的类库肯定可用。感谢您的回答。这两个系统都有问题(我现在正在使用的方法和您的解决方案)是,在这两种情况下,我都必须一直阅读
char
,直到我读到一行的末尾。然后将所有的char放在一起,重新创建初始的
int
。尽管看起来我错了,但我确信有一种类似于Matlab
fscanf(h,“%d”)的解决方案
已经解决了这个问题,尽管我没有发现任何类似的问题。在Matlab代码中,这是使用终止符字符完成的,我基本上想知道我是否遗漏了所用函数的某些属性。哦,我明白了。这基本上是类似fscanf的实现方式,但Windows没有提供任何函数据我所知,至少我能做到。
// Fill the buffer with 0
char szBuff[256] = {0};
// We have no data in the buffer, so the end of data points to the beginning 
// of the buffer.
char* szEndOfData = szBuff; 
while (i < maxSamples)
{
    WaitCommEvent (hSerial, &dwCommStatus, 0);

    if (dwCommStatus & EV_RXCHAR) 
    {
        // Append up to 4 bytes from the serial port to the buffer
        ReadFile(hSerial, LPVOID(szEndOfData), 4, &dwBytesRead, NULL);
        // Increment the end of data pointer, so it points to the end of the
        // data available in the buffer.
        szEndOfData += dwBytesRead;

        cout<<szBuff;
        printf(" - %d - \n", atoi(szBuff));
    }
    i++;     
}

// Output, assuming what you mentioned happens:
// - 88 -
// - 8889 -
int i = 0;  
DWORD dwBytesRead = 0;
DWORD dwCommStatus = 0;
char szBuff[2] = "";                
int maxRead = 20;   
int sizeNum = 1;    
int *num    = (int*)malloc(maxRead*sizeof(int)); 
char *currNum;
char *pastNum;

// Write something into the Serial Port to start receive 
// information from the Arduino
WriteFile(hSerial, (LPCVOID)"A\0", 1, &dwBytesRead, NULL);    
SetCommMask(hSerial, EV_RXCHAR);

// Start reading from the Serial Port
while ( i < maxRead )
{
    WaitCommEvent (hSerial, &dwCommStatus, 0);

    if (dwCommStatus & EV_RXCHAR) // if a char is received in the serial port
    {
        ReadFile(hSerial, LPVOID(szBuff), 1, &dwBytesRead, NULL);

        if ( szBuff[0] > 47 && szBuff[0] < 58 )
        {
            sizeNum++;
            if (sizeNum ==2)
            {
                currNum = (char*)malloc(sizeNum*sizeof(char));
                strcpy(currNum, szBuff);
            } else
            {
                if (pastNum != NULL)
                    free(pastNum);
                pastNum = currNum;
                currNum = (char*)malloc(sizeNum*sizeof(char));
                strcpy(currNum, pastNum);
                strcpy(currNum+(sizeNum-2)*sizeof(char), szBuff);
            }

            cout << szBuff<<endl;   
        } else if (szBuff[0] == '\n' && sizeNum > 1) // end of number
        {
            num[i] = atoi(currNum);
            i++;                    

            sizeNum = 1;
            if (currNum!=NULL)
                free(currNum);
        }
    }
}