Serial Communication Hex commands

Hi Guys
I am working on a communication with a PC.
I need to send a Hex command called "x14CAP".
Using Serial.println() doesn't work.
This is my code:

`> //@AB Electronic 2022 con comando Start

String valor;
int Rele = 2; //Perto Salida Digital para el Rele
int Sensor = 3; // Puerto Entrada Digital para el sensor de presencia de placa

void setup(){
Serial.begin(19200);
pinMode(Rele,OUTPUT);
pinMode(Sensor,INPUT);
}

void loop(){
if (digitalRead(Sensor) == LOW){
Serial.print("Envio comando Start");
Serial.println("/x14C"); //Envío comando Stop
Serial.println("/x14CAP"); //Envío comando Start
do {
delay(500);
} while (Serial.available() == 0);
if (Serial.available() > 0){
valor = Serial.readStringUntil('\n'); //Leemos hasta encontrar RC
valor = valor.substring(valor.length()-4, valor.length()-1); //Cogemos los 3 ultimos caracteres
Serial.print("VALOR: ");
Serial.print(valor );

//Valor recibido VeriSpector OK
if (valor == "B01") { 
Serial.println("Resultado OK ");
digitalWrite(Rele, HIGH); //Rele activado
delay(5000); //1 seg Rele activado
digitalWrite(Rele, LOW); //Rele  cortado
}
//Valor recibido VeriSpector NOK
 if (valor == "B02") {
Serial.println("Resultado NOK ");
delay(1000);
}
}
valor == "";

}
}

Thanks for any help

Welcome to the forum

Serial.println("/x14C"); //Envío comando Stop
Serial.println("/x14CAP"); //Envío comando Start

Why have you got forward slashes in the string that you are printing ?

You seem to be printing both commands and text to the same Serial interface. That does not sound right

Which Arduino board are you using ?
Which pins on the board is the device connected to ?

Hi UKHeliBob

I found a mistake. This is the description of the command I got:
"

Arduino Uno
It is connected by a RS-232/TTL board

Thanks

P is not part of hexadecimal, can you clarify exactly what bytes you need to send?

I cannot read what you got

If you are expecting to see messages printed to the Serial monitor and also to send commands to the device then you will need 2 separate serial interfaces, hence my question as to which board you are using and which pins the interface to the device is connected

Please help us to help you by supplying full information

Hi Jackson

I was told to send command \x14CAP (ask for image capture) and \x14C (stop inspection).
I get command B01 and B02 in return without any problem.

Thanks

Ok so the byte 0x14 followed by ascii chars CAP ( for capture i assume)

You could do this to spell things out

deviceSerialPort.write(0x14);
deviceSerialPort.print("CAP");

deviceSerialPort Would be the serial interface to your device.

Jackson, then something like that:

while (digitalRead(Sensor) == LOW){ 
  Serial.write(0x14); //Envío comando Stop
  Serial.println("C"); 
  Serial.print("Envio comando Start");
  Serial.write(0x14); //Envío comando Start 
  Serial.println("CAP");

Not sure about the ln At the end of print, the protocol does not seem to say you need to send CR a d LF, so just print

There there is the question about Serial…. Describe your circuit… because Serial is often connected to the Serial monitor….

Yes, I do need to add CR.
There is SW for image capturing installed on a PC. PC is connected to RS-232/TTL board with a cable. We use digital pin 0 and 1 for RX/TX. That works fine. At least the reception of commands from PC to Arduino.
We also control a Rele, pin 2 to say to an external device that everything was ok.
With pin 3 monitor a PLC sensor output to trigger the communication.

I hope that you have better luck than I have in getting the details or even a clue as to which Arduino is being used

Not mentioned in your image

It is not the best document.
Im going to upload the program and test the whole system.
I will let you know.

hi Jackson
My code make nothing on the PC. There is not response

while (digitalRead(Sensor) == LOW){ 
  Serial.write(0x14); //Envío comando Stop
  Serial.println("C"); 
  Serial.print("Envio comando Start");
  Serial.write(0x14); //Envío comando Start 
  Serial.println("CAP");

Where is Serial connected to ?
Do you have a USB cable between the PC and Arduino?

How should we ask more clearly

DESCRIBE YOUR CURCUIT… please (if you want any more answer)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.