Hello arduino community,
I know this is a topic many times spoken, but i still couldn't make it work and find the right answer in the topics i searched for... so here it is:
I own an arduino mega1280 and use 0022 to make and upload my code.
I've already made quite a few projects including custom midi shields and love the way things work. Problem is, i'm not good at C, i'm a more fluent programmer of vb.
So i use vb.net to program my interface. Anyway, i'm making a printer like a plotter that uses stepper motors, i made my own custom class for them and they work like a charm, so i use serial communication to send instructions to the arduino from vb.net
it's simple, an instruction is like
XMOTP0024 meaning move motor X to the right for 24 steps. all instructions will be fixed length of 9 characters, and i want to split it into header=XMOT sign=P steps=24.
i've tried many ways of doing it, using memcpy, strcpy, one-by one characters copy... but one of these lines always seems to mess up my buffer or resulting variables can anyone help?
here's the piece of code that deals with it:
int ledPin = 13;
char buffer[9];
char header[4];
char number[4];
int received;
void setup() {
//setup motor pins
pinMode(ledPin, OUTPUT);
//serial communication
Serial.begin(9600); // start serial communication
received=0;
}
void loop() {
//take instruction from serial port, fill buffer
if (Serial.available()){
buffer[received++] = Serial.read();
if (received >= (sizeof(buffer))){
received=0;
//extract variables from buffer
memcpy(number, &buffer[5],4);
memcpy(header, &buffer[0],4);
int steps = atoi(number);
char sign = buffer[4];
//DEBUG print over serial
Serial.println(header);
Serial.println(sign);
Serial.println(steps);
}
}
}
using the serial monitor i'm giving XMOTP0025 but i get back
xmot0025
p
25
i just can't get the "header" to work in any way...! does any experienced programmer have something to suggest?
thank you in advance!