How to use serial to communicate with a host win32 program

I have been stuck with the issue about sending 128 characters from PC to Arduino UNO,
I am unable to send these characters to the serial buffer. I think it is due to the size of the buffer.

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);

  }

}

Here is my C++ main

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

looks like the PC code is reading from the keyboard and presumably there is none until after the arduino boots and is ready.

even if the PC driver returns a complete line, i doubt there are close to 128.

are both interfaces at the same bit-rate, 9600, as well as other serial parameters?

When using Serial.print the output buffer is 32. Typing some characters, say 10, on the Pc IDE they are all picked up by the UNO. It looks like there is some input buffer.
Stack up a lot of chars, send them and let the Arduino read and count them.

Robin2's serial input basics tutorial may be of interest. It shows how to read incoming serial data into a separate buffer for that purpose. The separate buffer can be made any size (within limits of memory). The bytes are read into the separate buffer as they come in and they are not stored in the hardware serial receive buffer (which is 64 bytes by default).

From the tutorial:

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

You can set the size of the receive buffer (receivedChars) to the size that you need. Be aware that the data is read into a string (null terminated character array) so there needs to be room in the buffer for the terminating null ('\0'). So to receive 128 bytes you need a 129 byte buffer.

1 Like

Looks like you expect an Enter after each char? Is that true?

Suggest you send a terminating char for the data. Like a newline.
See my Arduino Software Solutions
for alternatives for reading chars from Serial that don't require you to worry about terminating nulls ('\0'). It also covers flushing the Serial input on startup to clear out old pending data (not just the data in the Arduino RX buffer)

You could also look at my Arduino to Arduino/PC for a more extensive solution to Arduino/PC coms. May be much more than you need though and so far only has python example code for the PC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.