1

I send data from arduino by method following. Configure the value : byte message[ ];

#include <SoftwareSerial.h>
#define RX 2
#define TX 3
int incomingByte = 0;

SoftwareSerial mySerial(RX, TX);

void setup()
{
  pinMode(5, OUTPUT);

  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  incomingByte = Serial.read();

  if (incomingByte == 's')
  {
    digitalWrite(5, HIGH);
    byte message[] = {0x01, 0x06, 0x00, 0x01, 0x02, 0x58, 0xD8, 0x90} ;
    mySerial.write(message1, sizeof(message1));
  }
}

It works. But I want to input from "Serial monitor" to set the temperature. It does not work when input from "Serial monitor".

1 Answer 1

1

You're not writing any data to incomingByte and you're not detecting if data is sent towards the Arduino over serial.

You should change your void loop() into this:

void loop(){

  //Detects if something is sent over serial.
  if(Serial.available() > 0){

    //reads the byte and puts it into the incomingByte variable
    incomingByte = Serial.read();

    //below is your old code
    if (incomingByte == 's') {
      digitalWrite(5, HIGH);
      byte message[] = {0x01, 0x06, 0x00, 0x01, 0x02, 0x58, 0xD8, 0x90} ;
      mySerial.write(message1, sizeof(message1));
    }
  }
}

When you're using the serial monitor to send data towards the Arduino, also make sure the baudrate you use is the same as the Arduino's. In this case you set your baudrate to 9600 Serial.begin(9600).

4
  • Thank you. And I can send hexadecimal from "serial monitor" into the variable "byte message[ ]" ? Commented Jul 12, 2016 at 7:54
  • Yes and no. As far as I know, the serial monitor connection already sends data as HEX but it is converted automatically on both ends. So, sending HEX would actually be encrypted to HEX, sent to the Arduino, decrypted to ASCII which still gives you the original HEX value. But, I honestly don't know for sure. You should experiment, it's quite easy to make a HEX converter as well. Commented Jul 12, 2016 at 8:01
  • 1
    No, the serial monitor just sends what you type (typically ASCII) as is. No encoding is automatically applied. Commented Jul 12, 2016 at 9:47
  • @TW_JM: You can send HEX from the serial monitor, but you will have to convert the data to binary in the Arduino. That could be done with the strtol() function from avr-libc. Commented Jul 12, 2016 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.