Cant get serial working right

I have been trying to send and receive serial communications with a laser measurement system. I have been trying serial.write and print and println and looping individual bytes one at a time. . I've tried forcing HEX,DEC,OCT,BIN and nothing seems to be working. I am running out of ideas on how to make it work. The error i keep getting is the data is junk going to and from the laser. If i do the commands just in serial monitor it prints out fine there. I'm using a MEGA and using the serial1 port for communications. The laser claims it is regular ascii txt. Now when i use putty terminal all the commands i send and receive are just fine so i know my serial connections all work. I have backed out to simplify troublshooting and now i am just trying to send the command to the laser and watch the readout on putty.
The command I send to the laser is
MS,0,01
The response I am expecting back is
MS,+01.23456
Could someone help me out by giving me a sample of how you would send MS,0,01 out the serial1 port?

void setup() {
Serial1.begin(9600);
}

void loop()

 {
  Serial1.write("MS", DEC);
  Serial1.write(",", DEC);
  Serial1.write("0", DEC);
  Serial1.write(",", DEC);
  Serial1.write("01", DEC);
  Serial1.write('\r');
  delay (500);

}
void setup() {
Serial1.begin(9600, SERIAL_8N1);}

void loop()

 {
  Serial1.println("MS,0,01");
  delay (500);

}
void setup() {
Serial1.begin(9600);
}

void loop()

 {
Serial1.print("MS,0,01");
Serial1.println();
delay (500);

}

Where / how are you reading the response from the Laser? If the serial port is connected to the laser, the serial monitor will not be connected therefore cannot show any response.

1 Like

I do not see how you send anything to the serial port (PC). You're missing at least a Serial.begin() and you're never reading and printing the reply that you receive.

Please provide a link to the description of the protocol.

1 Like

this sends the query cmd , once, and prints anything received from Serial1 to Serial

void loop()
{
    if (Serial1.available ()) {
        char c = Serial1.read ();
        Serial.print (c);
    }
}

void setup() {
    Serial.begin  (9600);
    Serial1.begin (9600);

    Serial1.println ("MS,0,01");
}

Very far from correct syntax.
The modificator "DEC" is applicable with numeric data only (but you trying using it with a string) and within the print() command (but not with write())

This is the serial part of the manual with all the serial information i can find so far. Serial-info.pdf - Google Drive

Wiring diagram...??

I tried it this way and I'm still getting garbage output.



The part that throws me off is that with putty running with theses settings I am getting the right responses using just serial on windows and no arduino.

does is need a LF, CR or CR/LF termination?

    Serial1.println ("MS,0,01\r");

println will add the \n

could also add a delay before sending to insure the serial interface is configured.

maybe the first time is corrupted and should be tried again every few seconds.

That part states RS232. Are you using a serial-to-RS232 converter? Or did you directly connect the laser measurement system to the Mega?
Where do you get the communication parameters (baud rate etc) from?

Below can act as a framework to start testing the communication; it uses a simple state machine.

char command[] = "MS,0,01";
char terminator = '\r';

// timeout duration for receive from laser equipment
const uint32_t timeoutDuration = 5000;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
}

void loop()
{
  // send command and receive reply
  if (fsmComms() == true)
  {
    // wait a bit
    delay(2000);
  }
}

/*
   Finite state machine for comms with laser equipment
   Returns:
    false while in progress
    true if full reply received or in case of timeout
*/
bool fsmComms()
{
  enum STATES
  {
    SEND,
    RECEIVE,
    TIMEOUT,
  };

  bool retVal = false;

  // state of state machine
  static STATES state = SEND;
  // start time for timeout
  static uint32_t timeoutStartTime;

  switch (state)
  {
    case SEND:
      Serial1.print(command);
      Serial1.write(terminator);
      timeoutStartTime = millis();
      state = RECEIVE;
      break;
    case RECEIVE:
      // if a timeout occurs
      if (millis() - timeoutStartTime >= timeoutDuration)
      {
        state = TIMEOUT;
      }
      // check if there is data received
      else if (Serial1.available())
      {
        // reset start time for timeut
        timeoutStartTime = millis();
        // read a byte
        char ch = Serial1.read();
        // print it
        if (ch < 0x10)
        {
          Serial.print("0");
        }
        Serial.print(ch, HEX);
        Serial.print(" ");
        // stop when we receive a terminator
        if (ch == terminator)
        {
          Serial.println();
          state = SEND;
          retVal = true;
        }
      }
      break;
    case TIMEOUT:
      Serial.println(F("[Timeout]"));
      retVal = true;
      state = SEND;
      break;
  }
  return retVal;
}

Tested with a Leonardo that echoes the received data back to the sending Mega.

For the final receive functionality I suggest that you study Robin's updated Serial Input Basics.

1 Like
void loop()
{
    if (Serial1.available ()) {
        char c = Serial1.read ();
        Serial.write (c);
    }
}

Thank you so much. This got me working for my first hurdle. I did have to remove the println line to remove spaces between char and change the baud rate for my serial monitor but other than that it was perfect. Now I just have to figure out getting the rest of the way which will be a bunch more reading up on everything. I'll admit it but i did do a dumb thing and had my wires backwards on my ttl to 232 converter and that held me up for a few days of not understanding the garbage i was getting as well,

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