Problem receiving bytes from VB.NET
hi everyone!
i'm designing particular project in serial communication in important.
i try explain system in few words: vb.net program sends arduino letter recognized, arduino sends data sensors, vb.net program elaborates these data , sends arduino 2 bytes. these 2 bytes represent logic states have written 16 digital pins (one bit each pin).
now, arduino recognizes letter sent computer, program receives data arduino correctly , creates final 2 bytes perfectly, here comes problem!
arduino receives 2 bytes (i think), uses bit masks , tries write obtained values digital pins, these digital pins behave in wrong way , don't understand how work!
here vb.net code specific part:
and code arduino:
this evening i'll try understand arduino receives, in meantime i'd advice.
thanks lot!
i'm designing particular project in serial communication in important.
i try explain system in few words: vb.net program sends arduino letter recognized, arduino sends data sensors, vb.net program elaborates these data , sends arduino 2 bytes. these 2 bytes represent logic states have written 16 digital pins (one bit each pin).
now, arduino recognizes letter sent computer, program receives data arduino correctly , creates final 2 bytes perfectly, here comes problem!
arduino receives 2 bytes (i think), uses bit masks , tries write obtained values digital pins, these digital pins behave in wrong way , don't understand how work!
here vb.net code specific part:
code: [select]
dev16to9 = dev16to9array(7) * 128 + dev16to9array(6) * 64 + dev16to9array(5) * 32 + dev16to9array(4) * 16 + dev16to9array(3) * 8 + dev16to9array(2) * 4 + dev16to9array(1) * 2 + dev16to9array(0)
dev8to1 = dev8to1array(7) * 128 + dev8to1array(6) * 64 + dev8to1array(5) * 32 + dev8to1array(4) * 16 + dev8to1array(3) * 8 + dev8to1array(2) * 4 + dev8to1array(1) * 2 + dev8to1array(0)
serialport1.open()
serialport1.write(dev16to9)
serialport1.write(dev8to1)
label4.text = dev8to1
serialport1.close()
and code arduino:
code: [select]
#include <wire.h>
// pins
// current sensors
const unsigned int panelcs = 0;
const unsigned int toenelcs = 1;
const unsigned int requiredcs = 2;
const unsigned int fromeneltodevcs = 3;
const unsigned int fromeneltobatterycs = 4;
// voltage dividers
const unsigned int panelvd = 5;
const unsigned int batteryvd = 6;
// smart devices
const unsigned int dev1 = 22;
const unsigned int dev2 = 23;
const unsigned int dev3 = 24;
const unsigned int dev4 = 25;
const unsigned int dev5 = 26;
const unsigned int dev6 = 27;
const unsigned int dev7 = 28;
const unsigned int dev8 = 29;
const unsigned int dev9 = 30;
const unsigned int dev10 = 31;
const unsigned int dev11 = 32;
const unsigned int dev12 = 33;
const unsigned int dev13 = 34;
const unsigned int dev14 = 35;
const unsigned int dev15 = 36;
const unsigned int dev16 = 37;
// variables
// currents
int panelcurrent;
int toenelcurrent;
int requiredcurrent;
int fromeneltodevcurrent;
int fromeneltobatterycurrent;
// voltages
int panelvoltage;
int batteryvoltage;
// powers
byte producedpower;
byte consumedpower;
int boughtpower;
int soldpower;
// panel angle
byte panelangle;
// communication start byte
byte codea;
// bytes pc
byte bytesfrompc[2];
void setup(){
pinmode(dev1,output);
pinmode(dev2,output);
pinmode(dev3,output);
pinmode(dev4,output);
pinmode(dev5,output);
pinmode(dev6,output);
pinmode(dev7,output);
pinmode(dev8,output);
pinmode(dev9,output);
pinmode(dev10,output);
pinmode(dev11,output);
pinmode(dev12,output);
pinmode(dev13,output);
pinmode(dev14,output);
pinmode(dev15,output);
pinmode(dev16,output);
serial.begin(9600);
wire.begin();
}
void loop(){
producedpower = 1;
consumedpower = 2;
batteryvoltage = 3;
//-------------------------------------------------------------
wire.requestfrom(2, 1);
while(wire.available()){
panelangle = wire.read();
}
if(serial.available()) codea = serial.read();
if(codea==0x41){
delay(50);
serial.write(producedpower);
serial.write(consumedpower);
serial.write(batteryvoltage);
serial.write(panelangle);
if(serial.available()){
for(int i=0; i<2; i++){
bytesfrompc[i] = serial.read();
}
}
digitalwrite(dev1, bytesfrompc[1] & 1);
digitalwrite(dev2, (bytesfrompc[1] >> 1)& 1);
digitalwrite(dev3, (bytesfrompc[1] >> 2)& 1);
digitalwrite(dev4, (bytesfrompc[1] >> 3)& 1);
digitalwrite(dev5, (bytesfrompc[1] >> 4)& 1);
digitalwrite(dev6, (bytesfrompc[1] >> 5)& 1);
digitalwrite(dev7, (bytesfrompc[1] >> 6)& 1);
digitalwrite(dev8, (bytesfrompc[1] >> 7)& 1);
digitalwrite(dev9, bytesfrompc[0] & 1);
digitalwrite(dev10, (bytesfrompc[0] >> 1)& 1);
digitalwrite(dev11, (bytesfrompc[0] >> 2)& 1);
digitalwrite(dev12, (bytesfrompc[0] >> 3)& 1);
digitalwrite(dev13, (bytesfrompc[0] >> 4)& 1);
digitalwrite(dev14, (bytesfrompc[0] >> 5)& 1);
digitalwrite(dev15, (bytesfrompc[0] >> 6)& 1);
digitalwrite(dev16, (bytesfrompc[0] >> 7)& 1);
}
}
this evening i'll try understand arduino receives, in meantime i'd advice.
thanks lot!
first problem, suspect, vb code opens , closes serial port. when opens port arduino reset , vb code needs wait complete before sending anything. should keep port open until finished arduino.
your receiving code not @ robust. if works luck.
the examples in serial input basics show few simple ways reliably recieve data.
it may wise require vb code wait request arduino before sends data.
this python demo may give ideas. concepts can apply in language.
your receiving code not @ robust. if works luck.
the examples in serial input basics show few simple ways reliably recieve data.
it may wise require vb code wait request arduino before sends data.
this python demo may give ideas. concepts can apply in language.
Arduino Forum > Using Arduino > Interfacing w/ Software on the Computer > Problem receiving bytes from VB.NET
arduino
Comments
Post a Comment