So I have made a new code.
The laser always sends a set of initial chars that should be ignored, regardless of that I still want to read that initial communication to make sure everything is working properly.
in main.cpp setup I have this:
void setup()
{
Serial_1.begin(baudRate_1);
Serial_2.begin(baudRate_2);
//setup here
sys.initPyrometer(); //initialize Pyrometer
sys.initLaser(); //initialize Laser
~
//PID Setup
Setpoint = 750; //Target value (Temperature)
myPID.SetMode(AUTOMATIC); //Turn PID ON
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetSampleTime(10); //Sample time = 10 [ms]
myPID.SetOutputLimits(10, 100); //Output limits = Diode Current (10-100% of Max Laser Power)
//Serial Communication Setup
sys.laser_COM(); //Establish init communications to laser. check system.cpp
}
sys.laser_COM() function:
SoftwareSerial Serial_1(0, 1); //RX and TX ports
const byte numChars = 32; //number of readable characters
char receivedChars[numChars]; //chars to print
boolean newData = false;
void System::laser_COM()
{
//laser initial communications (to run once)
//Check Laser YLR-200-AC User Manual for laser instructions descriprion
Serial_1.begin(baudRate_1); //Set the data rate for the software serial port
while (!Serial_1)
{
; //wait for the serial port to connect. needed for native usb port only
}
recieving_data();
show_data();
Serial_1.println('\n');
//Send Laser instructions now:
Serial_1.println("DMOD"); // Disable Modulation
Serial_1.write('\r');
recieving_data();
show_data();
Serial_1.println('\n');
Serial_1.println("DEC"); // Disable External Control
Serial_1.write('\r');
recieving_data();
show_data();
Serial_1.println('\n');
Serial_1.println("ELE"); // Enable Hardware Emission Control
Serial_1.write('\r');
recieving_data();
show_data();
Serial_1.println('\n');
}
Serial communication functions taken from example 3 in: Serial Input Basics - updated - #2 by Robin2
void System::recieving_data() //Recieve data from laser control board
{
static boolean recvInProgress = false;
static byte ndx = 0;
char rc;
while (Serial_1.available() > 0 && newData == false)
{
rc = Serial_1.read();
if (recvInProgress == true)
{
if (rc != '\r')
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == '\n')
{
recvInProgress = true;
}
}
}
void System::show_data() //print recieved data from laser control board
{
if (newData == true)
{
Serial_1.print("LASER COMMUNICATING: ");
Serial_1.println(receivedChars);
newData = false;
}
}
Then everytime I send an instruction to the laser, if the laser control board understands the instruction it replies back that instruction, otherwise it gives back ERROR.
This is the code to send and recieve those instructions.
in main.cpp loop I have this:
void loop()
{
sys.tempReading(); //temperature reading
Input = sys.tempReading();
//PID calculation
myPID.Compute();
sys.laser_output(Output); //laser output intructions (Serial communication, RS232)
}
laser_output() function:
void System::laser_output(float input)
{
// //Check Laser YLR-200-AC User Manual for laser instructions descriprion
Serial_1.print("SDC "); // Set Diode Current.
Serial_1.println(input); //PID_value = Diode Current (10-100% of Max Laser Power)
Serial_1.write('\r');
recieving_data();
show_data();
Serial_1.println('\n');
Serial_1.println("EMON"); // Emission ON
Serial_1.write('\r');
recieving_data();
show_data();
Serial_1.println('\n');
}
I have baudRate_1 set for 57600
and baudRate_2 set for 9600
I will probably change the baudRate_1 to 38400 as advised.
I am now using an arduino mega.
I am also using SoftwareSerial to have the temperature readings in monitor 2 and laser control instructions in monitor 1.
Do you think this can work?
Sorry for the amount of code I am posting, I can only test this code tomorrow morning but would like some feedback.