[SOLVED]Byte processing , serial communication
hello,
i'm working on gui project arduino , want send 7 bytes arduino. array should 16115061 16 sop , 61 eop. 1 parameter type(1 speed,2 proportional value, etc.) , 150 pwm value.
for example if wanted speed of motor 150(pwm) packet lool 16115061, if wanted to send value of 10,54 proportional value packet should 16210,5461.
i made numericupdown control should ,by pressing enter, send packet arduino
printbytearray() helper function shows array in console. if enter 40 prints new byte[] { 16, 1, 0, 0, 72, 66, 61, }, 255 prints new byte[] { 16, 1, 0, 0, 127, 67, 61, }.
the receiving part on arduino looks,so far, this:
the problem don't know how convert 4 bytes float , pass them analogwrite. motor doesn't start. how should proceed ? doing wrong ?
i'm working on gui project arduino , want send 7 bytes arduino. array should 16115061 16 sop , 61 eop. 1 parameter type(1 speed,2 proportional value, etc.) , 150 pwm value.
for example if wanted speed of motor 150(pwm) packet lool 16115061, if wanted to send value of 10,54 proportional value packet should 16210,5461.
i made numericupdown control should ,by pressing enter, send packet arduino
code: [select]
private void numericsetpoint_keypress(object sender, keypresseventargs e)
{
if (e.keychar == 13)
{
if (serialport1.isopen)
{
byte[] send = new byte[7];
float setpoint = (float)numericsetpoint.value;
byte[] x = bitconverter.getbytes(setpoint);
send[0] = convert.tobyte(16);
send[1] = convert.tobyte(1);
send[2] = x[0];
send[3] = x[1];
send[4] = x[2];
send[5] = x[3];
send[6] = convert.tobyte(61);
//array.reverse(x);
serialport1.write(send, 0, 1);
printbytearray(send);
}
printbytearray() helper function shows array in console. if enter 40 prints new byte[] { 16, 1, 0, 0, 72, 66, 61, }, 255 prints new byte[] { 16, 1, 0, 0, 127, 67, 61, }.
the receiving part on arduino looks,so far, this:
code: [select]
int motorpin = 9;
byte bytearray[7];
float speed;
union
{
byte b[4];
float kfloat;
}p;
void setup()
{
serial.begin(9600);
}
void loop()
{
if(serial.available() == 7)
{
for(int = 0; i<7; i++)
{
bytearray[i] = serial.read();
}
}
//convert float
p.b[0] = bytearray[2];
p.b[1] = bytearray[3];
p.b[2] = bytearray[4];
p.b[3] = bytearray[5];
speed = p.kfloat;
analogwrite(motorpin,speed);
}
the problem don't know how convert 4 bytes float , pass them analogwrite. motor doesn't start. how should proceed ? doing wrong ?
the second parameter in analogwrite byte not float.
Arduino Forum > Using Arduino > Programming Questions > [SOLVED]Byte processing , serial communication
arduino
Comments
Post a Comment