Send data to arduino via Visual C++  and print it

Hello everyone,

I'm trying to send some serial data from a Visual C++ console program to an arduino, and i would like to print it out in the serial monitor.

I can't manage to print the data in real time becouse the serial port is busy with the C program, and probably the things are too fast and when I open the serial monitor data has gone.

What can I do about this? Can i save dato to an eprom, or what..?

Thanks a lot for help!

Here the sketch

byte inByte;
byte messaggio[] = {
  0, 0, 0, 0, 0, 0, 0, 0};
void setup()
{
  Serial.begin(9600);


}

void loop()
{

  if (Serial.available() > 0) {
    inByte = Serial.read();
    for (int ii=0; ii<=7; ii++)
    {
      messaggio[ii] = inByte;
      ii = ii + 1;
    }
    delay(1000);
    Serial.println("il valore di  e'" );
    Serial.print (messaggio[0]);

    Serial.print (messaggio[1]);

    Serial.print (messaggio[2]);

    Serial.print (messaggio[3]);

    Serial.print (messaggio[4]);

    Serial.print (messaggio[5]);

    Serial.print (messaggio[6]);

    Serial.println (messaggio[7]);


  }
}

and the Visual C++ program (I'm sending data with sendData_starString method)

// sendPacket.cpp : file di progetto principale.

namespace sendPacket {
#include "stdafx.h"
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>



using namespace System;
using namespace std;
using namespace System::IO;
using namespace System::IO::Ports;
using namespace System::Threading;

//initialize flags structure (:1 means 1 bit register dimension, using bitfields)
  struct FLAGS{

  unsigned short pitch:1;
  unsigned short roll:1;
  unsigned short thrust:1;
  unsigned short yaw:1;
  unsigned short acc:1;
  unsigned short height:1;
  unsigned short acc_height:1;
  unsigned short trigger:1;
  unsigned short freq:8;
};

  //initialize data to send
struct SCIENTIFIC_COMMANDDATA
{
    //always 0x17
     unsigned char packetdescriptor;
    //pitch, roll, thrust, yaw commands. 0..4095 2048=middle
    unsigned short pitch;
    unsigned short roll;
    unsigned short thrust;
    unsigned short yaw;
    //flags
    //Bit 0(0x01): Pitch control through serial interfacae enabled
    //Bit 1(0x02): Roll control through serial interface enabled
    //Bit 2(0x04): Thrust control through serial interface enabled
    //Bit 3(0x08): Yaw control through serial interface enabled
    //Bit 4(0x10): ACC-Mode on/off
    //Bit 5(0x20): Height control - on/off (only with ACC)
    //Bit 6(0x40): overwrite ACC/Height mode control
    //(0=mode selected by RC 1=mode selected by Bit 4 and 5)
    //Bit 7(0x80): Trigger Scientific status packet (triggers a response
    //with the actual scientific state)
    //Bit 8..15: sendrate of the scientific packet in 5Hz
    //(0=off;1=5Hz, 20=100Hz, 200=1kHz)
    //Scientific packet is send for three seconds max.
    //after the last command_data packet
    unsigned short flags;

};


//send-data methods
void sendData_startString(array<unsigned char> ^ startString)
{


    
    
    StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
    //Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));

    // Create a new SerialPort object with default settings.
    SerialPort^  _serialPort = gcnew SerialPort("COM4");

    // Allow the user to set the appropriate properties.
    _serialPort->BaudRate = 9600 ;
    _serialPort->Parity = Parity::None;
    _serialPort->DataBits = 8;
    _serialPort->StopBits = StopBits::One;
    _serialPort->Handshake =Handshake::None;

    // Set the read/write timeouts
   // _serialPort->ReadTimeout = 500;
  //  _serialPort->WriteTimeout = 500;
    _serialPort->Open();
   int ii;
   for(ii=0;ii<10;ii++)
   {
    _serialPort->Write(startString,0,3);
    Thread::Sleep(1000);
   }


    _serialPort->Close();


}

 //declare crc methods
unsigned short crc_update(unsigned short crc, unsigned char data)
{

    data ^= (crc & 0xff);
    data ^= data << 4;
    return ((((unsigned short )data << 8) | ((crc>>8)&0xff)) ^ (unsigned char )(data >> 4)
            ^ ((unsigned short )data << 3));


}
unsigned short crc16(void* data, unsigned short cnt)
{
    //unsigned short crc=0xff;
    unsigned char * ptr=(unsigned char *) data;
    int i;
    unsigned short crc;
    for (i=0;i<cnt;i++)
    {
        crc=crc_update(crc,*ptr);
        Console::WriteLine("crc:"+crc);
        ptr++;
    }
    return  crc;

}



int data;
unsigned short crc=0xff;
unsigned short crc_fin;


int main(array<System::String ^> ^args)
{
    //initialize struct
   FLAGS myFlags;
   SCIENTIFIC_COMMANDDATA scientific_commanddata;
  
    //set startstring
    array<unsigned char>^ startString={'>','*','>'};
   //unsigned char startString[]={'>','*','>'};
    // flags manually set
   myFlags.pitch=0;
   myFlags.roll=0;
   myFlags.thrust=0;
   myFlags.yaw=0;
   myFlags.acc=1;
   myFlags.height=0;
   myFlags.acc_height=0;
   myFlags.trigger=1;
   myFlags.freq= 20; //20 hz??
    
    //other SCIENTIFIC_COMMANDATA variables manually set
   scientific_commanddata.packetdescriptor = 0x17;
   scientific_commanddata.pitch= 0;
   scientific_commanddata.roll=0;
   scientific_commanddata.thrust=100;
   scientific_commanddata.yaw=0;
   _int8 length=sizeof(scientific_commanddata);
   void* ptr;
   ptr=&(scientific_commanddata.packetdescriptor);
       void *data_p=&data;

    crc16(ptr,length);
    sendData_startString(startString);


    printf("sizeof startString: %d bytes\n",sizeof(startString));
    printf("sizeof scientific_commandata: %d bytes\n",sizeof(scientific_commanddata));
    printf("sizeof flags: %d bytes\n",sizeof(myFlags));
    printf("sizeof lenght: %d bytes\n",sizeof(length));
    printf("starbytes: %s\n",startString);
    printf("crc16: %d\n",crc);
    char Hex[64];
    _itoa_s(crc, Hex, 16);
    printf("crc16 (hex): %s\n",Hex);
    getchar();
    return 0;
}

Mhh some topic ago is written that Close() reset the arduino...

Mhh some topic ago is written that Close() reset the arduino...

It does. So does the Open() call.

You need to open the connection once, and then wait for the Arduino to reset before you send it data.

I'm trying to send some serial data from a Visual C++ console program to an arduino, and i would like to print it out in the serial monitor.

That you will not be able to do, unless you use NewSoftSerial to establish a second serial connection that you use to write to the Serial Monitor.

But, there is no reason to do this. The C++ application is perfectly capable of reading serial data, as well as writing it. Make the C++ application read the serial data.

Thanks Paul :smiley:
I'm trying with this code:

 _serialPort->Write(startString,0,3);
     int Buffer= _serialPort->Read(buffer,0,3);
    }
    _serialPort->Close();

But I'm receiving odd data (Like buffer = 1, instead of '>' I'm sending). Have I to set some Sleep?or cycles or set String^ instead of int

:o

But I'm receiving odd data (Like buffer = 1, instead of '>' I'm sending). Have I to set some Sleep?or cycles or set String^ instead of int

The short answer is yes.

The Arduino needs time to receive the serial data, process it, and return a response. It is slower than your PC. Yet, you are expecting a response to be ready immediately after you sent the message to the Arduino.

You are reading a maximum of three characters from the serial port, and storing them in the character array buffer. At least, I assume it's a character array.

You also store the return code from SerialPort::Read() into an integer called Buffer.

I hope you are not trying to use Buffer for anything, since SerialPort::Read() doesn't return a value.

You should make sure that the Arduino includes a carriage return/new line in it's reply, by using Serial.println(), and you should have your application call SerialPort::ReadLine() instead.

The ReadLine method blocks until the CR/LF appears, so it is not necessary to sleep for an indeterminate period of time. The ReadLine() method returns a String^.

Really Really thanks Paul, you are one of the biggest resource of the entire Arduino environment. You have to put yourself in a library (PaulS.h :D).

When I'm back home I'll try to modify the code