ok, so i have a C# program (can be converted to C++ if needed) and i have this:
struct RoboInfo
{
public Double Speed; //-100 to +100
public Double SteeringDirection; //-5 to +5
public Double Tilt;//0 to 20
public Double Pan;//-5 to 5
public bool Shoot;//true or false
}
i want to send that to the arduino, i know i can do this:
SerialPort.Write(byte[], 0, 5); (not acutla code, but you get the point...
but the arduino gets the ASCII values, is there any EASY was to just read the data i send into a byte array?????
I can't help with your specific question but I can provide some general guidance...
Byte ordering will very likely be different. A Double in C# is probably stored "backwards" from the way it's stored in the Arduino. You will probably have to swap the bytes for each Double.
Byte alignment is probably different. In C# the elements are very likely 8-byte aligned while in the Arduino the elements are not aligned (byte aligned). Shoot is the element most likely to be a problem. You may have to pad the structure on the Arduino side (or make the structure "packed" on the C# side).
well, i can make them all ints, or bytes, thats not an issue, if i had like
byte[] Data=new byte[5];
Data[0]=100;//speed, 0=full reverse, 100=stop, 200=fulll forward
Data[1]=5; //0 is full left, 5 is center, 10 is full right
Data[2]=0; //0 is level, 20 is full up
Data[3]=5; //0 is full left, 5 is center, 10 is full right
Data[4]=0; //0 is not shooting, 1 is shooting
is there a way to send that to the arduino, into another byte array?
See what you can do with this. It's quick and dirty, but it looks like you know what you're doing, maybe it'll give you some ideas. In particular notice what the integer value does as you write to the high and low bytes.
The BitConverter class may be useful if you need to send multi-byte values. You can find EndianBitConverter implementations that can make things a bit easier.
Arduino Sketch:
struct Info
{
byte byteVal1;
byte byteVal2;
boolean boolVal;
int intVal;
} info;
byte* infop = (byte*)&info;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
establishContact(); // send a byte to establish contact until receiver responds
showStruct();
}
void loop()
{
if (Serial.available() > 0) {
// Get the input
byte inByte = Serial.read();
// check for special value
if (inByte == 'r')
// reset the input pointer
infop = (byte*)&info;
else
// store the input and advance the pointer
*(infop++) = inByte;
// Print the current state of the structure
showStruct();
}
}
void showStruct()
{
Serial.print("byteVal1:");
Serial.println(info.byteVal1, DEC);
Serial.print("byteVal2:");
Serial.println(info.byteVal2, DEC);
Serial.print("boolVal:");
Serial.println(info.boolVal, DEC);
Serial.print("intVal:");
Serial.println(info.intVal, DEC);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A', BYTE); // send a capital A
delay(300);
}
}
C# app
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM5");
port.BaudRate = 9600;
port.Open();
Console.WriteLine("type 'x' to exit.");
while (true)
{
if (port.BytesToRead > 0)
{
Console.Write( (char)port.ReadByte());
}
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
// use 'x' to close port and exit
if (key.KeyChar == 'x')
break;
else
{
Console.WriteLine(string.Format("<-{0}({1})", (byte)key.KeyChar, key.KeyChar));
port.Write(key.KeyChar.ToString());
}
}
}
port.Close();
}
}
}