Converting an Uno sketch for Due

Hi , I'm a new starter in a way.
I'm working on a project that uses a laser range finder that communicates with arduino through TTL, and I have this sketch obtained by the seller of the LRF and it is for UNO which works fine ....
And it uses SoftwareSerial...
How ever I'd like to be able to run it un DUE, as far as I know (as Due has 3 other native serial ports or for some other reason) SoftwareSerial can not be loaded to Due.
I changed some definitions. (ex: portOne => Serial1 ) and some functions to make the sketch compatible with Due.
It loads without errors. When I enter the commands through serial monitor, all I get is errors defined in sketch.
What I tried is to change Softwareserial commands with the compatible ones which will work on Serial1 but my knowledge is limited. Thus it failed ...
If someone could convert this sketch to work on Due it will be mush appreciated. Thanks in advance...
Original Sketch is like this (for UNO and using SoftwareSerial)

[color=blue]
#include <Wire.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
//ARDUNIO Serial control command

#define LASER_OPEN "O"//Open the module
#define MEASURE_ONE "D"//Single measurement
#define MEASURE_CONTINUE "C"//Continuous measurement
#define STOP_MEASURE "S"//Stop measuring
// software serial : TX = digital pin 7, RX = digital pin 6
SoftwareSerial portOne(7, 6);
//Measuring state machine status
#define STEP_LASER_OPEN 0//Open the module
#define STEP_MEASURE_ONE 1//Single measurement
#define STEP_MEASURE_CONTINUE 2//Continuous measurement
#define STEP_STOP_MEASURE 3//Stop measuring

#define MEASURE_CONTINUE_N 200//Continuous measurement

#define SUCCESS 0//measurement
#define FAIL -1//measurement

unsigned char measure_continu_cnt=0;
unsigned char measure_continue_flag=0;
//Function declaration
String ReadData(char Length);
String Serial_One_ReadData(char Length);
void Measure(char MEASURE_COMMEND);//distance measurement
String LaserOpen();
String MeasureOne();
String MeasureContinue();
void measure_continue_data();
String StopMeasure();

void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);//Serial debug baud rate
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
   // Start both software serial ports
  portOne.begin(115200);  //Laser ranging module baud rate
}

void loop() {
  String temp="";
  temp= ReadData();
  if(temp == LASER_OPEN)
   Measure(STEP_LASER_OPEN);
  else if(temp == MEASURE_ONE)
  Measure(STEP_MEASURE_ONE);
  else if(temp == MEASURE_CONTINUE)
  Measure(STEP_MEASURE_CONTINUE);
  else if(temp == STOP_MEASURE)
  Measure(STEP_STOP_MEASURE);
  else if(temp != "")
  Serial.print("UNDEFINED COMMEND\r\n");
  if(measure_continue_flag==1)
  {
    measure_continue_data();
  }
 ;
}

void Measure(char STEP_MEASURE_COMMEND)//distance measurement
{
  String temp="";
  switch(STEP_MEASURE_COMMEND)
  {
    case STEP_LASER_OPEN:
    temp = LaserOpen();
    if(temp!= "-1")
    {
      Serial.print("LASER OPEN SUCCEED\r\n");
    }
    else
    {
      Serial.print("LASER OPEN FAILD\r\n");
     }
    break;
    case STEP_MEASURE_ONE:
    temp = MeasureOne();
    if(temp!= "-1")
    {
      if(temp.indexOf("&")==9 | temp.lastIndexOf("$")==10)
      {
        temp=temp.substring(20,22)+"."+temp.substring(22,27);
      }
      else
      {
        temp=temp.substring(10,12)+"."+temp.substring(12,17);
      }
      temp="distanc="+temp+"m"+"\r\n";
      Serial.print(temp);
     }
     else
     {
      Serial.print("MEASURE_ONE FAILD\r\n");
      } 
    break;
    case  STEP_MEASURE_CONTINUE:
      Serial.print("MEASURE_CONTINUE START\r\n");
      temp=MeasureContinue();
      measure_continu_cnt=0;
      measure_continue_flag=1;
     if(temp!= "-1")
     {
      if(temp.indexOf("&")==9 | temp.lastIndexOf("$")==10)
      {
        temp=temp.substring(24,26)+"."+temp.substring(26,29);
      }
      else
      {
        temp=temp.substring(14,16)+"."+temp.substring(16,19);
      }
      temp="distanc="+temp+"m"+"\r\n";
      Serial.print(temp);
     }
     else
     {
      Serial.print("MEASURE_ONE FAILD\r\n");
      }     
    break;
    case STEP_STOP_MEASURE:
     temp = StopMeasure();
     measure_continu_cnt=0;
     measure_continue_flag=0;
    Serial.print("MEASURE STOP\r\n");   
    break;           
    default:
    break;
  }
}

//Turn on the laser pointer
String LaserOpen()
{
  String return_data="";
  unsigned char temp=0;
  portOne.listen();

  if (portOne.isListening()) {
   portOne.flush();
   portOne.print("$0003260130&"); 
   return_data = Serial_One_ReadData(22);
   return  return_data;//Turn on the laser module
  }
}

//Single range measurement
String MeasureOne()
{
  String return_data="";
  unsigned char temp=0;
  portOne.listen();
  if (portOne.isListening()) {
   portOne.flush();
   portOne.print("$00022123&"); 
   return_data = Serial_One_ReadData(28);
   return  return_data;
   }
}

//Continuous ranging
String MeasureContinue()
{
    String return_data="";
  unsigned char temp=0;
  portOne.listen();

  if (portOne.isListening()) {
   portOne.flush();
   portOne.print("$00022426&"); 
   return_data = Serial_One_ReadData(48);
   return  return_data;
}
}

void measure_continue_data()
{
        String temp="";
        temp=Serial_One_ReadData(38);
        measure_continu_cnt++;
        if(temp.substring(17,19)=="15"|temp.substring(17,19)=="16"|temp.substring(17,19)=="")
        {
          Serial.print( measure_continu_cnt);
          Serial.print("ERROR\r\n"); 
          temp=MeasureContinue();
          temp=temp.substring(24,26)+"."+temp.substring(26,29);;//¼ÓÈëĞ¡Êıµã
          Serial.print( measure_continu_cnt);
          temp="  distanc="+temp+"m"+"\r\n";
        }
        else
        {
          temp=temp.substring(14,16)+"."+temp.substring(16,19);;//¼ÓÈëĞ¡Êıµã
          Serial.print( measure_continu_cnt);
          temp="  distanc="+temp+"m"+"\r\n";
        }
        Serial.print(temp);  
}
//Stop measurement
String StopMeasure()
{
  String return_data="";
  unsigned char temp=0;
  
  portOne.listen();

  if (portOne.isListening()) {
   portOne.flush();
   portOne.print("$0003260029&"); 
   return_data = Serial_One_ReadData(22);
   return  return_data;
}
}

//Read serial port to return data
String ReadData()
{
  int wait_time=0;
  String return_data="";
  do
  {
    delay(1);
    wait_time++;
    while(Serial.available()>0)//serial data read
    {
      char CharRead=Serial.read();
      if(CharRead!=10&&CharRead!=13)
      {
         return_data +=CharRead;
      }
      wait_time=0;
    } 
  }
  while(wait_time<500);  //Wait for 1 second, no data is returned, the read is finished.
  return return_data;
}

//Read serial port to return data
String Serial_One_ReadData(char Length)
{
  int wait_time=0;
  String return_data="";
  do
  {
    delay(1);
    wait_time++;
    while( portOne.available()>0)//serial data read
    {
      char CharRead= portOne.read();
      if(CharRead!=10&&CharRead!=13)
      {
          return_data +=CharRead;
      }
      wait_time=0;
      if(return_data.length()>=Length)
      {
         wait_time=6000;
      }
    } 
  }
  while(wait_time<2000);  //Wait for 1 second, no data is returned, the read is finished.
  unsigned char last_byte=(unsigned char)return_data.charAt(return_data.length()-1);
  if( wait_time == 6000)
  {     
  }
  else
  {
    return_data="-1";
  }
  return return_data;
}

[/color]

I tried my chance and wrote this ( it loads OK. But returns predefined errors (in sketch) like "LASER OPEN FAILD"

Read these arduino tutorials to convert Softwareserial commands into Serial commands:

And look at example sketches in: Menu>File>Examples>Communication>SerialEvent and SerialCallResponse

BTW: edit your post and post your code between post tags </>


Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Thank you. And sorry TomGeorge . That was my first post. I'll take care...

ard_newbie:
Read these arduino tutorials to convert Softwareserial commands into Serial commands:

https://www.arduino.cc/en/Reference/SoftwareSerial

Serial - Arduino Reference

And look at example sketches in: Menu>File>Examples>Communication>SerialEvent and SerialCallResponse

BTW: edit your post and post your code between post tags </>


Thank you for the reply. And sorry for the cross-post. Excuse me for this is my first post. But now I read all the rules.
I'd made some research on the "reference" as you mentioned. I could manage to do some conversions .
The code now loads without any errors but I could not be able to get an errorless response from the serial device (which in my case is a Laser Range Finder ).
BTW I suceeded sending commands and getting correct measurement with a basic skech But anyway before I spend your time I'll concentrate on SerialEvent and Serailcallresponse .
On the refenrence I read SerialEvent1 is only for Mega. What is the correct way to use this for Due and for Serial1

Thank you...