problem so serial komunikaciou
Napsal: 13 bře 2019, 09:50
Ahojte,
mam problem so seriovou komunikaciou medzi nano a mega. Po zapnuti sa komunikacia vobec nerozbehne, ta sa zacne az po stlaceni enter v monitore serioveho portu . //Je to citanie koordinatov z GRBL.//
mam problem so seriovou komunikaciou medzi nano a mega. Po zapnuti sa komunikacia vobec nerozbehne, ta sa zacne az po stlaceni enter v monitore serioveho portu . //Je to citanie koordinatov z GRBL.//
Kód: Vybrat vše
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//Globalne premenne pre komunikaciu
bool stringComplete = true;
String line;
String State;
String x_Value;
String y_Value;
String z_Value;
String wline;
String X_WCO;
String Y_WCO;
String Z_WCO;
float Fx_Value;
float Fy_Value;
float Fz_Value;
float FX_WCO;
float FY_WCO;
float FZ_WCO;
float X_Value;
float Y_Value;
float Z_Value;
void setup(){
ini_serial();
lcd.init();
lcd.backlight();
delay(2000);
lcd.clear();
}
void loop() {
requestPosition();
if (stringComplete == true) {
parse_and_store();
line = "";
stringComplete = false;
}
void ini_serial(){
Serial.begin(115200);
Serial1.begin(115200);
line.reserve(64);
}
//poziadavka na status stroja
void requestPosition(){
Serial1.println();
Serial1.println("?\r\n");
delay(1);
}
void serialEvent() {
while(Serial1.available()!=0) {
char inChar = (char)Serial1.read();
line += inChar;
delay(10);
Serial.print(inChar);
if (inChar == '\n') {
stringComplete = true;
}
}
}
void parse_and_store(){
int parseState = line.indexOf('<');
int parseState1 = line.indexOf('|');
// Serial.println(line.substring(parseState+1,parseState1));
State = line.substring(parseState + 1,parseState1);
int parseMPos = line.indexOf('MPos:');
Serial.println(parseMPos);
int xparseMPos = line.indexOf(',');
int yparseMPos = line.indexOf(',',xparseMPos + 1);
int z_parseMPos = line.indexOf('|');
int zparseMPos = line.indexOf('|',z_parseMPos + 1);
x_Value = line.substring(parseMPos+1,xparseMPos);
y_Value = line.substring(xparseMPos+1,yparseMPos);
z_Value = line.substring(yparseMPos+1,zparseMPos);
Serial.println(x_Value);
Serial.println(State);
lcd.setCursor(10,3);
lcd.print(State);
lcd.print(" ");
// lcd.print(timeString);
lcd.setCursor(0, 3);
lcd.print("X: "); lcd.print(x_Value);//lcd.print(F(" "))
/*Serial.println( line.substring(parseMPos+1,xparseMPos));
Serial.println( line.substring(xparseMPos+1,yparseMPos));
Serial.println( line.substring(yparseMPos+1,zparseMPos));*/
int parseWPos = line.indexOf('WCO'); //najdi WCO
//Serial.println(parseWPos);
if ( parseWPos != -1 ){
wline = line.substring(parseWPos); //prijmy WCO WCO: O:0.000,0.000,0.000>
// Serial.println(wline);
int wparseWPos = wline.indexOf(':');
//Serial.println(wparseWPos);
int xparseWPos = wline.indexOf(',');
int yparseWPos = wline.indexOf(',',xparseWPos + 1);
int zparseWPos = wline.indexOf('>');
X_WCO = (wline.substring(wparseWPos + 1,xparseWPos));
Y_WCO = (wline.substring(xparseWPos + 1,yparseWPos));
Z_WCO = (wline.substring(yparseWPos + 1,zparseWPos));
/*Serial.println(wline.substring(wparseWPos + 1,xparseWPos));
Serial.println(wline.substring(xparseWPos + 1,yparseWPos));
Serial.println(wline.substring(yparseWPos + 1,zparseWPos));*/
if ( wline.indexOf('Ov') == -1)
{
Fx_Value = x_Value.toFloat(); //konvertuj na float
Fy_Value = y_Value.toFloat();
Fz_Value = z_Value.toFloat();
FX_WCO = X_WCO.toFloat();
FY_WCO = Y_WCO.toFloat();
FZ_WCO = Z_WCO.toFloat();
X_Value = Fx_Value - FX_WCO; // WPos = MPos - WCO;
Y_Value = Fy_Value - FY_WCO;
Z_Value = Fz_Value - FZ_WCO;
/* Serial.println(Fx_Value - FX_WCO,3);
Serial.println(Fy_Value - FY_WCO,3);
Serial.println(Fz_Value - FZ_WCO,3);*/ //debug
}
}
}