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