0

I am passing in a data buffer from Arduino Uno to a C++ win32 program, but UNO is sending data too slowly. I am trying to send 128 character bytes but the win32 is receiving the bytes after like 92 places are filled 0, then it goes 1,2,3,4.....

com.read_n_data(data, 128); this line of code is receiving the data slowly from the UNO to the HOST.
// main.cpp : This code is for a console application.
//
// This code writes a character to COM2 and then reads from COM2 a character which is displayed.
// It is to work with a UNO board on the other side of COM2 that echos back whatever the UNO
// board receives from COM2.
//
// The COMport class is based on the code in http://members.ee.net/brey/Serial.pdf
//

#include "stdafx.h"
#include <atlstr.h>
#include <iostream>
#include "COMport.h"

// needed when cin , cout are used
using namespace std;

int _tmain(int argc, _TCHAR* argv[]) // This is the entry point of a Visual C++ Console Application
{
    char data[128], mychar, char_skip;
    COMport com(_T("COM2")); // works only for COM1 to COM9
    // COMport com(_T("\\\\.\\COM11"));  // This "\\\\.\\" part is necessary for COM10 and up

    for(int i=0; i<128; i++){
        cin.get(mychar); // this will read a character from keyboard, including a space character
        //cin >> mychar; // this will read a character from keyboard, but skip a space character
        cin.get(char_skip);  // ignore the "Enter" key      
        //char b[100];
        //cin.getline(b, 100); // this will read a whole line from keyboard

        com.write_and_read(mychar); // this is used to see if the other side (UNO) is echoing back the char
        cout << mychar;      
    }   

    com.write_n_data(data, 128); // write 128 bytes to COM port
    com.read_n_data(data, 128);  // read 128 bytes from COM port
    // Note that Arduino Serial.println("...") appends at the end of the string a "carrige return" character
    // followed by a "new line" character.

The uno is not sending the data fast enough, the buffer in the host have series of '0's at first 92 spots. Here is the uno code

int const BUFSIZE = 128;
char ibuffer[BUFSIZE] ;
char obuffer[BUFSIZE] ;


void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  if (Serial.available() > 0 ){
    for(int i= 0; i < 128; i++){
    ibuffer[i]=Serial.read();
    }
    
    Serial.write(ibuffer, BUFSIZE);
  }
}
9
  • 1
    thus is sn Arduino site ... win32 programming is off topic here ... find a widows site to learn about serial port programming – jsotola Apr 23 at 5:10
  • 1
    Sorry I haven't gave the whole code about Arduino. I added it now – ram patel Apr 23 at 5:32
  • 1
    data is uninitialized, so sending and receiving that will still keep it in undefined state. You do not know what you're sending, so you cannot know what you expect. Performance is definitely not your problem. – PMF Apr 23 at 7:04
  • 1
    I think I am sending the data like this.. if (Serial.available() > 0 ){ for(int i= 0; i < 128; i++){ ibuffer[i]=Serial.read(); } – ram patel Apr 23 at 7:14
  • 1
    I sort am confused with the statement actually, so should I use serial.available()>128 – ram patel Apr 23 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.