I have a problem using an hostlink command to read a certain data from a plc but only when I include a command to write data from a load cell. So this doesn’t work because I added the first hostlink command
Serial.print(composizione(“00”,“WD”,“0152”,intToHex(cella1.get_units(5))));
#include <HX711.h>
#include <EEPROM.h>
#define rip 5
HX711 cella1(A1,A0);
int scambio=0;
void setup() {
float prova;
long t;
pinMode(13, OUTPUT);
Serial.begin(19200,SERIAL_7E2);//aggiungere ,SERIAL_7E2 per la trasmissione hostlink
EEPROM.get(0,t);
cella1.set_offset(t);
EEPROM.get(4*sizeof(long),prova);
cella1.set_scale(prova);
delay(2000);
}
void loop() {
int i; //contatore per tempo massimo di attesa
int z=0; //conferma avvenuta lettura
char a=13;
String datoLetto=String(); //dato ricevuto in input
String strIn=String(); //stringa ricevuta in input
String str=String();
//writes the weigth
Serial.print(composizione("00","WD","0152",intToHex(cella1.get_units(5))));
if(Serial.available()>0){
str=Serial.readStringUntil(a);
}
//send the command for reading from the plc
Serial.print(composizione("00","RD","0152","0001"));
if(Serial.available()>0){
strIn=Serial.readStringUntil(a);
}
//verify that the string is correct
if(strIn.length()>0){
for(i=0;i<strIn.length();i++){
if(strIn.charAt(i)=='R' && strIn.charAt(i+1)=='D'){
if(strIn.charAt(i+2)=='0' && strIn.charAt(i+3)=='0'){
datoLetto=strIn.substring(i+4,i+8);
z=1;
}
}
}
}
//write the data just read back to the plc
Serial.println(composizione("00","WD","0150",datoLetto));
if(Serial.available()>0){
str=Serial.readStringUntil(a);
}
}
//create the correct hostlink command
String composizione(String nodo,String comando,String canale,String valore){
String temp;
char a=13;
temp="@"+nodo+comando+canale+valore;
//temp+=intToHex(valore);
String fcs=String(CalcFCS(temp,temp.length()),HEX);
fcs.toUpperCase();
temp+=fcs+'*'+a;
return temp;
}
void taratura(){
long tara;
int i;
//if(s.charAt(3)=='1'){
tara=cella1.read_average(rip);
EEPROM.put(1*sizeof(long)*0,tara);
cella1.set_offset(tara);
//}
delay(4);
}
//la calibrazione è da fare con un peso da 1 Kg
void calibrazione(){
float scala;
if(1){
cella1.set_scale();
scala=cella1.get_units(rip);
scala=scala/1000;
EEPROM.put(4*sizeof(long)+1*sizeof(long)*0,scala);
cella1.set_scale(scala);
}
delay(4);
}
//convert a number into a hex and store it into a string
String intToHex(unsigned long n){
int i;
String str="00000000";
for(i=7;n>0;i--){
if(i>3)
str.setCharAt(i-4,hexToChar(n%16));
else
str.setCharAt(i+4,hexToChar(n%16));
n=n/16;
}
return str;
}
//convenrt an hex into a char
char hexToChar(int h){
if(h<10){
h+=48;
}else{
h+=55;
}
return (char)h;
}
//calculate the fcs
unsigned char CalcFCS(String str, int lun)
{
unsigned char sum = 0;
for(int i=0;i<=lun;i++) sum ^= str.charAt(i);
return sum;
}
//empty the uart buffer
void serialFlush(){
while (Serial.read() > 0)
delay(1); /* do nothing else */
}
While this work and I can receive a valid response from the plc and the only change between the two is one command more in the upper code
#include <HX711.h>
HX711 pesa(A1,A0);
void setup() {
Serial.begin(19200,SERIAL_7E2); //aggiungere ,SERIAL_7E2 per la trasmissione hostlink
delay(2000);
}
void loop() {
int i; //contatore per tempo massimo di attesa
int j;
int z=0;
char a=13,input;
String stringa=String();
String strIn=String();
String msg="@00RD01520001";
String fcs=String(CalcFCS(msg,msg.length()),HEX);
fcs.toUpperCase();
msg+=fcs+'*'+a;
Serial.println(msg); //comando di lettura
if(Serial.available()>0){
strIn=Serial.readStringUntil(a);
}
//lettura
if(strIn.length()>7){
for(i=0;i<strIn.length();i++){
if(strIn.charAt(i)=='R' && strIn.charAt(i+1)=='D'){
if(strIn.charAt(i+2)=='0' && strIn.charAt(i+3)=='0'){
stringa=strIn.substring(i+4,i+8);
z=1;
}
}
}
}
if(z==1){
msg="@00WD0153";
msg+=stringa;//strIn.substring(7,11);
fcs=String(CalcFCS(msg,msg.length()),HEX);
fcs.toUpperCase();
msg+=fcs+'*'+a;
Serial.println(msg); // trasmette cosa è stato letto
strIn=Serial.readStringUntil(a);
}
strIn="";
}
//conversione di un intero in hex contenuto in due word
String intToHex(unsigned long n){
int i;
String str="00000000";
for(i=7;n>0;i--){
if(i>3)
str.setCharAt(i-4,hexToChar(n%16));
else
str.setCharAt(i+4,hexToChar(n%16));
n=n/16;
}
return str;
}
char hexToChar(int h){
if(h<10){
h+=48;
}else{
h+=55;
}
return (char)h;
}
unsigned char CalcFCS(String str, int lun)
{
unsigned char sum = 0;
for(int i=0;i<=lun;i++) sum ^= str.charAt(i);
return sum;
}
The library I’m using is at GitHub - bogde/HX711: An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for Weight Scales..