Serial.write() blocks receive when written inside draw() function

import processing.serial.*; //<>// //<>//
//import static javax.swing.JOptionPane.*;
import controlP5.*;
import java.util.*;
import javax.swing.JOptionPane; 


Serial myPort;        // The serial port which will be opened
ControlP5 cp5_internal_test, cp5_external_test;  // two guis. one for internal test, one for external

PImage led_red, led_green, led_white;

PFont font, fontHead;
final int UNKNOWN =-1;            //these are test results for internal testing
final int OK = 1;
final int NOK = 0;
final char START_PACKET = 0xAA;
final char END_PACKET = 0xAB;
//command codes for serial protocol

//when serial port is not connected and connect button is not presseed, gui is in either JUST_STARTED or DISCONNECTED state.

final byte ACK_INTERNAL_CC = 0x00;   //when connect button is pressed. state changes to CONNECT_COMMAND_GIVEN and GUI sends packet with this CC and 1 byte of 0 data. the control card sends same packet for acknowledgment
// 0xAA  NOB(0x05) ACK_INTERNAL_CC 0x00 CS 0xAB

final byte INTERNAL_CC = 0x01;       //after receiving ack from control card for internal mode, GUI switches state to INTERNAL_TEST. Now the control card sends commands 
// with this CC. This commmand has state of all comm channels, eeprom, adcs and dac.
// 0xAA  NOB INTERNAL_CC <eeprom ch6 ch5 ch4 ch3 ch2 ch1 0 > <adc8 adc7 adc6 adc5 adc4 adc3 adc2 adc1> <dac8 dac7 dac6 dac5 dac4 dac3 dac2 dac1> CS 0xAB
// 1B  + 1B + 1B                  +1B                              +1B                                            +1B                           +1B  +1B
// NOB = all bytes excepts 0xAB



final byte ACK_EXTERNAL_CC = 0x02; //When External Test button is pressed, gui_state changes to EXTERNAL_TEST_COMMAND_GIVEN.,gui sends packet with this CC and 1 byte of 0 data, 
//the control card sends same packet for acknowledgment

final byte EXTERNAL_CC = 0x03;     //after receiving ack, gui_state changes to EXTERNAL_TEST. Control Card will send ADC data with this CC. 
//GUI will send DAC command to control card with same CC

final byte ACK_BACK_CC = 0x04;      //when back button is clicked during EXTERNAL_TEST gui_state. this command is sent to control card to tell it to switch to INTERNAL_TEST state
// control card sends same command back ack and then switches to internal mode commmands
// GUI switches to INTERNAL_TEST state when ack is received

final byte ACK_DISCONNECT_CC = 0x05;  //when disconnect button is pressed gui_state changes to DISCONNET_COMMAND_GIVEN.
//this is to tell control card to go in default state when disconnected. 
// GUI will send command with this CC and control card will give ack with same CC and then control card goes in default mode.
// GUI will switch to gui_state DISCONNECTED


//states of the GUI state machine
final int JUST_STARTED =0;        
final int CONNECT_COMMAND_GIVEN =1;
final int INTERNAL_TEST =2;
final int EXTERNAL_TEST_COMMAND_GIVEN =3;
final int EXTERNAL_TEST =4;
final int BACK_COMMAND_GIVEN = 5;
final int DISCONNET_COMMAND_GIVEN = 6;
final int DISCONNECTED = 7;


boolean connected =false;
int gui_state=JUST_STARTED;
byte DAC_knob_value=0;
int adc1_state = UNKNOWN;
int adc2_state = UNKNOWN;
int adc3_state = UNKNOWN;
int adc4_state = UNKNOWN;
int adc5_state = UNKNOWN;
int adc6_state = UNKNOWN;
int adc7_state = UNKNOWN;
int adc8_state = UNKNOWN;

int dac1_state = UNKNOWN;
int dac2_state = UNKNOWN;
int dac3_state = UNKNOWN;
int dac4_state = UNKNOWN;
int dac5_state = UNKNOWN;
int dac6_state = UNKNOWN;
int dac7_state = UNKNOWN;
int dac8_state = UNKNOWN;

int ch1_state = UNKNOWN;
int ch2_state = UNKNOWN;
int ch3_state = UNKNOWN;
int ch4_state = UNKNOWN;
int ch5_state = UNKNOWN;
int ch6_state = UNKNOWN;
int eeprom_state = UNKNOWN;

int portSelected = UNKNOWN;
boolean recvInProgress = false;
byte ndx = 0;
boolean  SerialDataReady = false;
boolean SerialTransferComplete = false;
Slider adc1;
Slider adc2;
Slider adc3;
Slider adc4;
Slider adc5;
Slider adc6;
Slider adc7;
Slider adc8;

Knob dac_knob;
Button connectButton;
Button backButton;
Button externalButton;
ScrollableList serialPortDD;
final int MAX_PACKET_SIZE = 20;
char[] receivedData = new char[MAX_PACKET_SIZE];

//String receivedDataString="";


class ProcessedData { 
  int NOB=0;
  int CC=0;
  int [] data;
  int dataLen ;
  int CS=0;
  byte calculated_CS=0;

  char[] _raw_data;
  ProcessedData(char[] inp) {
    //_raw_data = inp;
    NOB = inp[0];
    CC = inp[1];
    data = new int[NOB-4];


    for (int i = 0; i<NOB-4; i++) {
      data[i]=inp[i+2];
    }

    CS = inp[NOB-2] ;

    dataLen = NOB-4;
  }

  int calculateCS() {
    int calculated_CS = 0;
    for (int i =0; i<NOB-1; i++) {
      calculated_CS = calculated_CS ^ _raw_data[i];
    }
    calculated_CS =calculated_CS ^ START_PACKET;

    return calculated_CS;
  }
} 

ProcessedData  rx_packet;






void setup() {
  font = createFont("AdobeHeitiStd-Regular", 12);  
  size(600, 600);    
  led_red = loadImage("red.png");
  led_green = loadImage("green.png");
  led_white = loadImage("white.png");
  List l = Arrays.asList(Serial.list());
  CColor bg = new CColor();
  bg.setBackground(#323F4B);
  CColor bg_button = new CColor();
  bg_button.setBackground(#2980B9);
  cp5_internal_test = new ControlP5(this);
  connectButton = cp5_internal_test.addButton("CONNECT")  //The button
    .setPosition(5, 33)  //x and y coordinates of upper left corner of button
    .setSize(80, 40)      //(width, height)
    .setColorBackground(  #2a4a6a  )
    .setColorActive(#305e8e) 
    .setColorForeground(#5d6d7e)
    .setFont(font)

    ;

  serialPortDD = cp5_internal_test.addScrollableList("dropdown")
    .setPosition(5, 5)
    .setSize(150, 100)
    .setBarHeight(20)
    .setItemHeight(20)
    .addItems(l)
    .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
    .setOpen(false)
    .setColorBackground(  #2a4a6a  ) 
    .setColorActive(#305e8e) 
    .setColorForeground(#5d6d7e)
    .setCaptionLabel("Select Serial Port") 
    .setFont(font)
    ;


  externalButton= cp5_internal_test.addButton("External_Test")  //The button
    .setPosition(450, 33)  //x and y coordinates of upper left corner of button
    .setSize(120, 40)      //(width, height)
    .setColorBackground(  #2a4a6a  )
    .setColorActive(#305e8e) 
    .setColorForeground(#5d6d7e)
    .setFont(font)
    .setCaptionLabel("External Test") 


    ;

  cp5_external_test = new ControlP5(this);

  backButton = cp5_external_test.addButton("Back")  //The button
    .setPosition(450, 33)  //x and y coordinates of upper left corner of button
    .setSize(120, 40)      //(width, height)
    .setColorBackground(  #2a4a6a  )
    .setColorActive(#305e8e) 
    .setColorForeground(#5d6d7e)
    .setFont(font)

    ;

  dac_knob=  cp5_external_test.addKnob("DAC_knob")
    .setRange(0, 10)
    .setValue(220)
    .setPosition(180, 100)
    .setRadius(40)
    .setNumberOfTickMarks(5)
    .setTickMarkLength(4)
    .snapToTickMarks(true)
    .setColorForeground(color(255))
    .setColorBackground(color(0, 160, 100))
    .setColorActive(color(255, 255, 0))
    .setDragDirection(Knob.HORIZONTAL)
    .setCaptionLabel(" ")
    //.setCaptionLabel("DAC Test")
    ;

  adc1 = cp5_external_test.addSlider("ADC1")
    .setPosition(180, 235)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    //.setMouseOver(false)
    .setLock(true)
    ;
  adc2 = cp5_external_test.addSlider("ADC2")
    .setPosition(180, 265)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)
    ;
  adc3 = cp5_external_test.addSlider("ADC3")
    .setPosition(180, 295)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)

    ;
  adc4 =   cp5_external_test.addSlider("ADC4")
    .setPosition(180, 325)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)
    ;
  adc5 =   cp5_external_test.addSlider("ADC5")
    .setPosition(180, 355)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)
    ;
  adc6 =   cp5_external_test.addSlider("ADC6")
    .setPosition(180, 385)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)
    ;
  adc7 =  cp5_external_test.addSlider("ADC7")
    .setPosition(180, 415)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ") 
    .setLock(true)
    ;
  adc8 =  cp5_external_test.addSlider("ADC8")
    .setPosition(180, 445)
    .setSize(100, 20)
    .setRange(0, 10)
    .setCaptionLabel(" ")
    .setLock(true)
    ;

  //gui_state = EXTERNAL_TEST;  //for testing
}


void draw() {
  if (gui_state == JUST_STARTED || gui_state == DISCONNECTED)
  {
    println("state = Just Started or DISCONNECTED");
    cp5_internal_test.show();
    cp5_external_test.hide();
    background(#323F4B);
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 

    textAlign(LEFT);
    text("CH1", 75, 110);  
    text("CH2", 75, 160); 
    text("CH3", 75, 210); 
    text("CH4", 75, 260); 
    text("CH5", 75, 310);
    text("CH6", 75, 360);  
    text("EEPROM", 75, 410);

    image(led_white, 10, 80);
    image(led_white, 10, 130);
    image(led_white, 10, 180);
    image(led_white, 10, 230);
    image(led_white, 10, 280);
    image(led_white, 10, 330);
    image(led_white, 10, 380);

    text("ADC1", 275, 110);  
    text("ADC2", 275, 160); 
    text("ADC3", 275, 210); 
    text("ADC4", 275, 260); 
    text("ADC5", 275, 310);
    text("ADC6", 275, 360);  
    text("ADC7", 275, 410);
    text("ADC8", 275, 460);

    image(led_white, 210, 80);
    image(led_white, 210, 130);
    image(led_white, 210, 180);
    image(led_white, 210, 230);
    image(led_white, 210, 280);
    image(led_white, 210, 330);
    image(led_white, 210, 380);
    image(led_white, 210, 430);

    text("DAC1", 475, 110);  
    text("DAC2", 475, 160); 
    text("DAC3", 475, 210); 
    text("DAC4", 475, 260); 
    text("DAC5", 475, 310);
    text("DAC6", 475, 360);  
    text("DAC7", 475, 410);
    text("DAC8", 475, 460);

    image(led_white, 410, 80);
    image(led_white, 410, 130);
    image(led_white, 410, 180);
    image(led_white, 410, 230);
    image(led_white, 410, 280);
    image(led_white, 410, 330);
    image(led_white, 410, 380);
    image(led_white, 410, 430);
  }

  if ( gui_state ==CONNECT_COMMAND_GIVEN)
  {

    println("state = CONNECT_COMMAND_GIVEN");

    //byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_INTERNAL_CC ^ 0x00);
    //myPort.write(START_PACKET);
    //myPort.write(0x05);
    //myPort.write(ACK_INTERNAL_CC);
    //myPort.write(0x00);
    //myPort.write(checksum);
    //myPort.write(END_PACKET);


    cp5_internal_test.show();
    cp5_external_test.hide();
    background(#323F4B);
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 

    textAlign(LEFT);
    text("CH1", 75, 110);  
    text("CH2", 75, 160); 
    text("CH3", 75, 210); 
    text("CH4", 75, 260); 
    text("CH5", 75, 310);
    text("CH6", 75, 360);  
    text("EEPROM", 75, 410);

    image(led_white, 10, 80);
    image(led_white, 10, 130);
    image(led_white, 10, 180);
    image(led_white, 10, 230);
    image(led_white, 10, 280);
    image(led_white, 10, 330);
    image(led_white, 10, 380);

    text("ADC1", 275, 110);  
    text("ADC2", 275, 160); 
    text("ADC3", 275, 210); 
    text("ADC4", 275, 260); 
    text("ADC5", 275, 310);
    text("ADC6", 275, 360);  
    text("ADC7", 275, 410);
    text("ADC8", 275, 460);

    image(led_white, 210, 80);
    image(led_white, 210, 130);
    image(led_white, 210, 180);
    image(led_white, 210, 230);
    image(led_white, 210, 280);
    image(led_white, 210, 330);
    image(led_white, 210, 380);
    image(led_white, 210, 430);

    text("DAC1", 475, 110);  
    text("DAC2", 475, 160); 
    text("DAC3", 475, 210); 
    text("DAC4", 475, 260); 
    text("DAC5", 475, 310);
    text("DAC6", 475, 360);  
    text("DAC7", 475, 410);
    text("DAC8", 475, 460);

    image(led_white, 410, 80);
    image(led_white, 410, 130);
    image(led_white, 410, 180);
    image(led_white, 410, 230);
    image(led_white, 410, 280);
    image(led_white, 410, 330);
    image(led_white, 410, 380);
    image(led_white, 410, 430);
  }

  if (gui_state == INTERNAL_TEST)
  {


    println("state = INTERNAL_TEST");

    cp5_internal_test.show();
    cp5_external_test.hide();
    background(#323F4B);
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 

    textAlign(LEFT);
    text("CH1", 75, 110);  
    text("CH2", 75, 160); 
    text("CH3", 75, 210); 
    text("CH4", 75, 260); 
    text("CH5", 75, 310);
    text("CH6", 75, 360);  
    text("EEPROM", 75, 410);

    image(led_white, 10, 80);
    image(led_white, 10, 130);
    image(led_white, 10, 180);
    image(led_white, 10, 230);
    image(led_white, 10, 280);
    image(led_white, 10, 330);
    image(led_white, 10, 380);

    text("ADC1", 275, 110);  
    text("ADC2", 275, 160); 
    text("ADC3", 275, 210); 
    text("ADC4", 275, 260); 
    text("ADC5", 275, 310);
    text("ADC6", 275, 360);  
    text("ADC7", 275, 410);
    text("ADC8", 275, 460);

    image(led_white, 210, 80);
    image(led_white, 210, 130);
    image(led_white, 210, 180);
    image(led_white, 210, 230);
    image(led_white, 210, 280);
    image(led_white, 210, 330);
    image(led_white, 210, 380);
    image(led_white, 210, 430);

    text("DAC1", 475, 110);  
    text("DAC2", 475, 160); 
    text("DAC3", 475, 210); 
    text("DAC4", 475, 260); 
    text("DAC5", 475, 310);
    text("DAC6", 475, 360);  
    text("DAC7", 475, 410);
    text("DAC8", 475, 460);

    image(led_white, 410, 80);
    image(led_white, 410, 130);
    image(led_white, 410, 180);
    image(led_white, 410, 230);
    image(led_white, 410, 280);
    image(led_white, 410, 330);
    image(led_white, 410, 380);
    image(led_white, 410, 430);


    if (ch1_state == OK) {
      image(led_green, 10, 80);
    } else if (ch1_state == NOK) {
      image(led_red, 10, 80);
    } 


    if (ch2_state == OK) {
      image(led_green, 10, 130);
    } else   if (ch2_state == NOK) {
      image(led_red, 10, 130);
    }


    if (ch3_state == OK) {

      image(led_green, 10, 180);
    } else   if (ch3_state == NOK) {

      image(led_red, 10, 180);
    }

    if (ch4_state == OK) {
      image(led_green, 10, 230);
    } else   if (ch4_state == NOK) {
      image(led_red, 10, 230);
    }


    if (ch5_state == OK) {
      image(led_green, 10, 280);
    } else   if (ch5_state == NOK) {
      image(led_red, 10, 280);
    }


    if (ch6_state == OK) {
      image(led_green, 10, 330);
    } else   if (ch6_state == NOK) {
      image(led_red, 10, 330);
    }


    if (eeprom_state == OK) {
      image(led_green, 10, 380);
    } else   if (eeprom_state == NOK) {
      image(led_red, 10, 380);
    }



    if (adc1_state == OK) {
      image(led_green, 210, 80);
    } else if (adc1_state == NOK) {
      image(led_red, 210, 80);
    } 


    if (adc2_state == OK) {
      image(led_green, 210, 130);
    } else   if (adc2_state == NOK) {
      image(led_red, 210, 130);
    }


    if (adc3_state == OK) {

      image(led_green, 210, 180);
    } else   if (adc3_state == NOK) {

      image(led_red, 210, 180);
    }

    if (adc4_state == OK) {
      image(led_green, 210, 230);
    } else   if (adc4_state == NOK) {
      image(led_red, 210, 230);
    }


    if (adc5_state == OK) {
      image(led_green, 210, 280);
    } else   if (adc5_state == NOK) {
      image(led_red, 210, 280);
    }


    if (adc6_state == OK) {
      image(led_green, 210, 330);
    } else   if (adc6_state == NOK) {
      image(led_red, 210, 330);
    }


    if (adc7_state == OK) {
      image(led_green, 210, 380);
    } else   if (adc7_state == NOK) {
      image(led_red, 210, 380);
    }

    if (adc8_state == OK) {
      image(led_green, 210, 430);
    } else   if (adc8_state == NOK) {
      image(led_red, 210, 430);
    }


    if (dac1_state == OK) {
      image(led_green, 410, 80);
    } else if (dac1_state == NOK) {
      image(led_red, 410, 80);
    } 


    if (dac2_state == OK) {
      image(led_green, 410, 130);
    } else   if (dac2_state == NOK) {
      image(led_red, 410, 130);
    }


    if (dac3_state == OK) {

      image(led_green, 410, 180);
    } else   if (dac3_state == NOK) {

      image(led_red, 410, 180);
    }

    if (dac4_state == OK) {
      image(led_green, 410, 230);
    } else   if (dac4_state == NOK) {
      image(led_red, 410, 230);
    }


    if (dac5_state == OK) {
      image(led_green, 410, 280);
    } else   if (dac5_state == NOK) {
      image(led_red, 410, 280);
    }


    if (dac6_state == OK) {
      image(led_green, 410, 330);
    } else   if (dac6_state == NOK) {
      image(led_red, 410, 330);
    }


    if (dac7_state == OK) {
      image(led_green, 410, 380);
    } else   if (dac7_state == NOK) {
      image(led_red, 410, 380);
    }

    if (dac8_state == OK) {
      image(led_green, 410, 430);
    } else   if (dac8_state == NOK) {
      image(led_red, 410, 430);
    }
  }



  if (gui_state ==EXTERNAL_TEST_COMMAND_GIVEN )
  {

    println("state = EXTERNAL_TEST_COMMAND_GIVEN");

    byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_EXTERNAL_CC ^ 0x00);
    myPort.write(START_PACKET);
    myPort.write(0x05);
    myPort.write(ACK_EXTERNAL_CC);
    myPort.write(0x00);
    myPort.write(checksum);
    myPort.write(END_PACKET);

    cp5_internal_test.show();
    cp5_external_test.hide();
    background(#323F4B);
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 

    textAlign(LEFT);
    text("CH1", 75, 110);  
    text("CH2", 75, 160); 
    text("CH3", 75, 210); 
    text("CH4", 75, 260); 
    text("CH5", 75, 310);
    text("CH6", 75, 360);  
    text("EEPROM", 75, 410);

    image(led_white, 10, 80);
    image(led_white, 10, 130);
    image(led_white, 10, 180);
    image(led_white, 10, 230);
    image(led_white, 10, 280);
    image(led_white, 10, 330);
    image(led_white, 10, 380);

    text("ADC1", 275, 110);  
    text("ADC2", 275, 160); 
    text("ADC3", 275, 210); 
    text("ADC4", 275, 260); 
    text("ADC5", 275, 310);
    text("ADC6", 275, 360);  
    text("ADC7", 275, 410);
    text("ADC8", 275, 460);

    image(led_white, 210, 80);
    image(led_white, 210, 130);
    image(led_white, 210, 180);
    image(led_white, 210, 230);
    image(led_white, 210, 280);
    image(led_white, 210, 330);
    image(led_white, 210, 380);
    image(led_white, 210, 430);

    text("DAC1", 475, 110);  
    text("DAC2", 475, 160); 
    text("DAC3", 475, 210); 
    text("DAC4", 475, 260); 
    text("DAC5", 475, 310);
    text("DAC6", 475, 360);  
    text("DAC7", 475, 410);
    text("DAC8", 475, 460);

    image(led_white, 410, 80);
    image(led_white, 410, 130);
    image(led_white, 410, 180);
    image(led_white, 410, 230);
    image(led_white, 410, 280);
    image(led_white, 410, 330);
    image(led_white, 410, 380);
    image(led_white, 410, 430);
  }


  if (gui_state == EXTERNAL_TEST ) {
    //byte checksum = (byte) (START_PACKET ^ 0x05 ^ EXTERNAL_CC ^ DAC_knob_value);
    //myPort.write(START_PACKET);
    //myPort.write(0x05);
    //myPort.write(EXTERNAL_CC);
    //myPort.write(DAC_knob_value);
    //myPort.write(checksum);
    //myPort.write(END_PACKET);


    background(#323F4B);
    cp5_internal_test.hide();
    cp5_external_test.show();
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 
    textAlign(LEFT);
    text("DAC External Test", 25, 150);
    text("ADC 1 External Test", 25, 250);
    text("ADC 2 External Test", 25, 280);
    text("ADC 3 External Test", 25, 310);
    text("ADC 4 External Test", 25, 340);
    text("ADC 5 External Test", 25, 370);
    text("ADC 6 External Test", 25, 400);
    text("ADC 7 External Test", 25, 430);
    text("ADC 8 External Test", 25, 460);
  }

  

  if ( gui_state ==DISCONNET_COMMAND_GIVEN)
  {

    println("state = DISCONNET_COMMAND_GIVEN");

    //    byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_DISCONNECT_CC ^ 0x00);
    //    myPort.write(START_PACKET);
    //    myPort.write(0x05);
    //    myPort.write(ACK_DISCONNECT_CC);
    //    myPort.write(0x00);
    //    myPort.write(checksum);
    //    myPort.write(END_PACKET);


    cp5_internal_test.show();
    cp5_external_test.hide();
    background(#323F4B);
    fontHead = createFont("AdobeHeitiStd-Regular", 25);  
    fill(#9FB3C8);
    textFont(fontHead); 
    textAlign(CENTER);
    text("Test Jig Interface", width/2, 25);  
    fill(#BCCCDC);
    font = createFont("AdobeHeitiStd-Regular", 15);  
    textFont(font); 

    textAlign(LEFT);
    text("CH1", 75, 110);  
    text("CH2", 75, 160); 
    text("CH3", 75, 210); 
    text("CH4", 75, 260); 
    text("CH5", 75, 310);
    text("CH6", 75, 360);  
    text("EEPROM", 75, 410);

    image(led_white, 10, 80);
    image(led_white, 10, 130);
    image(led_white, 10, 180);
    image(led_white, 10, 230);
    image(led_white, 10, 280);
    image(led_white, 10, 330);
    image(led_white, 10, 380);

    text("ADC1", 275, 110);  
    text("ADC2", 275, 160); 
    text("ADC3", 275, 210); 
    text("ADC4", 275, 260); 
    text("ADC5", 275, 310);
    text("ADC6", 275, 360);  
    text("ADC7", 275, 410);
    text("ADC8", 275, 460);

    image(led_white, 210, 80);
    image(led_white, 210, 130);
    image(led_white, 210, 180);
    image(led_white, 210, 230);
    image(led_white, 210, 280);
    image(led_white, 210, 330);
    image(led_white, 210, 380);
    image(led_white, 210, 430);

    text("DAC1", 475, 110);  
    text("DAC2", 475, 160); 
    text("DAC3", 475, 210); 
    text("DAC4", 475, 260); 
    text("DAC5", 475, 310);
    text("DAC6", 475, 360);  
    text("DAC7", 475, 410);
    text("DAC8", 475, 460);

    image(led_white, 410, 80);
    image(led_white, 410, 130);
    image(led_white, 410, 180);
    image(led_white, 410, 230);
    image(led_white, 410, 280);
    image(led_white, 410, 330);
    image(led_white, 410, 380);
    image(led_white, 410, 430);
  }
}


void dropdown(int n) {
  /* request the selected item based on index n */
  println(Serial.list()[n], "Selected");
  serialPortDD.setItems(Serial.list());
  serialPortDD.close();
  portSelected = n;
  serialPortDD.setLabel(Serial.list()[portSelected]);
}






void CONNECT() {


  if (connected==false) {
    gui_state = JUST_STARTED;
    connectButton.setLabel("DISCONNECT");
    connected= true;
    try {
      myPort = new Serial(this, Serial.list()[portSelected], 115200); // change baud rate to your liking
    }

    catch (Exception e)
    { //Print the type of error
      JOptionPane.showMessageDialog(frame, "Please select a COM Port");
      //println("Error:", e);
      connectButton.setLabel("CONNECT");
      connected= false;
      //exit();
    }
    if (connected ==true) {

      gui_state = CONNECT_COMMAND_GIVEN;

      byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_INTERNAL_CC ^ 0x00);
      myPort.write(START_PACKET);
      myPort.write(0x05);
      myPort.write(ACK_INTERNAL_CC);
      myPort.write(0x00);
      myPort.write(checksum);
      myPort.write(END_PACKET);
    }
  } else if (connected == true ) {
    font = createFont("AdobeHeitiStd-Regular", 12);  
    connectButton.setLabel("CONNECT");
    connected = false;
    gui_state = DISCONNET_COMMAND_GIVEN;

    byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_DISCONNECT_CC ^ 0x00);
    myPort.write(START_PACKET);
    myPort.write(0x05);
    myPort.write(ACK_DISCONNECT_CC);
    myPort.write(0x00);
    myPort.write(checksum);
    myPort.write(END_PACKET);
  }
}


void External_Test() {
  if (gui_state == INTERNAL_TEST)
    gui_state = EXTERNAL_TEST_COMMAND_GIVEN;
}


void Back() {
  gui_state = BACK_COMMAND_GIVEN;
}

void DAC_knob(byte theValue) {
  DAC_knob_value = theValue;
}




void serialEvent(Serial myPort) {  // when a serial char is received
  char startMarker = 0xAA;
  char endMarker = 0xAB;
  char rc=0;

  while (myPort.available() > 0 && SerialTransferComplete == false) {
    rc = myPort.readChar();
    //println(rc);
    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedData[ndx] = rc;
        ndx++;
        if (ndx >= MAX_PACKET_SIZE) {
          ndx = MAX_PACKET_SIZE - 1;
        }
      } else {
        receivedData[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        SerialTransferComplete = true;
        processSerialData();
      }
    } else if (rc == startMarker) {
      recvInProgress = true;
      SerialDataReady = false;
    }
  }
}






int processSerialData() {
  if (SerialTransferComplete == true) {
    SerialDataReady = true;
    SerialTransferComplete = false;
    rx_packet = new ProcessedData(receivedData);
    //check first if checksum has matched or not

    if (gui_state == CONNECT_COMMAND_GIVEN) {
      if (rx_packet.CC == ACK_INTERNAL_CC) {
        gui_state = INTERNAL_TEST;
        //println("ACK");
      }
    } else if (gui_state == INTERNAL_TEST) {
      if (rx_packet.CC == INTERNAL_CC) {
        //      < BYTE1 BYTE2 BYTE3>
        //      <eeprom ch6 ch5 ch4 ch3 ch2 ch1 0 > <adc8 adc7 adc6 adc5 adc4 adc3 adc2 adc1> <dac8 dac7 dac6 dac5 dac4 dac3 dac2 dac1>

        ch1_state      = ((rx_packet.data[0]     & 2)!=0 ) ? OK : NOK;  //0B00000010
        ch2_state      = ((rx_packet.data[0]     & 4)!=0 ) ? OK : NOK;  //0B00000100;
        ch3_state      = ((rx_packet.data[0]     & 8)!=0 ) ? OK : NOK;  //0b00001000;
        ch4_state      = ((rx_packet.data[0]     & 16)!=0 ) ? OK : NOK;  //0b00010000;
        ch5_state      = ((rx_packet.data[0]     & 32)!=0 ) ? OK : NOK;  //0b00100000;
        ch6_state      = ((rx_packet.data[0]     & 64)!=0 ) ? OK : NOK;  //0b01000000;
        eeprom_state   = ((rx_packet.data[0]     & 128)!=0 ) ? OK : NOK;  //0b10000000;

        adc1_state     = ((rx_packet.data[1]     & 1)!=0 ) ? OK : NOK;  //0b00000001;
        adc2_state     = ((rx_packet.data[1]     & 2)!=0 ) ? OK : NOK;  //0b00000010;
        adc3_state     = ((rx_packet.data[1]     & 4)!=0 ) ? OK : NOK;  //0b00000100;
        adc4_state     = ((rx_packet.data[1]     & 8)!=0 ) ? OK : NOK;  //0b00001000;
        adc5_state     = ((rx_packet.data[1]     & 16)!=0 ) ? OK : NOK;  //0b00010000;
        adc6_state     = ((rx_packet.data[1]     & 32)!=0 ) ? OK : NOK;  //0b00100000;
        adc7_state     = ((rx_packet.data[1]     & 64)!=0 ) ? OK : NOK;  //0b01000000;
        adc8_state     = ((rx_packet.data[1]     & 128)!=0 ) ? OK : NOK;  //0b10000000;

        dac1_state     = adc1_state;
        dac2_state     = adc2_state;
        dac3_state     = adc3_state;
        dac4_state     = adc4_state;
        dac5_state     = adc5_state;
        dac6_state     = adc6_state;
        dac7_state     = adc7_state;
        dac8_state     = adc8_state;
        //further data processing
      }
    } else if (gui_state == EXTERNAL_TEST_COMMAND_GIVEN) {
      if (rx_packet.CC == ACK_EXTERNAL_CC) {
        gui_state = EXTERNAL_TEST;
      }
    } else if (gui_state == EXTERNAL_TEST) {
      if (rx_packet.CC == EXTERNAL_CC) {
        //further data processing for adc data rx
      }
    } else if (gui_state == BACK_COMMAND_GIVEN) {
      if (rx_packet.CC == ACK_BACK_CC) {
        gui_state = INTERNAL_TEST;
      }
    } else if (gui_state == DISCONNET_COMMAND_GIVEN) {
      if (rx_packet.CC == ACK_DISCONNECT_CC) {
        gui_state = DISCONNECTED;
        myPort.stop();
      }
    }
  }
  return 0;
}

Hi,
This is my code, although I am not sure anyone would have time to go through the whole code so I will put the parts that seem like a problem below.
I am very new to processing and OOP in general. This is my 2nd code so please forgive if there is any silly blunder.

I am making a GUI to test a PCB.
It communicates over a serial port with the board and the board sends its status.

I start in gui_state == JUST_STARTED.
When the CONNECT button is pressed, the GUI enters gui_state == CONNECT_COMMAND_GIVEN.
In draw function, when the gui_state is CONNECT_COMMAND_GIVEN I want to repeatedly send the data_packet to initialise the board under test until I get an ack.

  if ( gui_state ==CONNECT_COMMAND_GIVEN)				//INSIDE DRAW()
  { 

    println("state = CONNECT_COMMAND_GIVEN");

    byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_INTERNAL_CC ^ 0x00);
    myPort.write(START_PACKET);
    myPort.write(0x05);
    myPort.write(ACK_INTERNAL_CC);
    myPort.write(0x00);
    myPort.write(checksum);
    myPort.write(END_PACKET);

Now when I repeatedly send the data_packet until I receive the ack, i do not receive ack at all. For testing I have set my board to continously sending the ack without waiting for my message.

When I place the data_packet in the place where I enter CONNECT_COMMAND_GIVEN state, so that it is sent only once, I get the data from the board under test. and get connected to it.

void CONNECT() {
  if (connected==false) {
    gui_state = JUST_STARTED;
    connectButton.setLabel("DISCONNECT");
    connected= true;
    try {
      myPort = new Serial(this, Serial.list()[portSelected], 115200); // change baud rate to your liking
    }

    catch (Exception e)
    { //Print the type of error
      JOptionPane.showMessageDialog(frame, "Please select a COM Port");
      //println("Error:", e);
      connectButton.setLabel("CONNECT");
      connected= false;
      //exit();
    }
    if (connected ==true) {

      gui_state = CONNECT_COMMAND_GIVEN;

      byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_INTERNAL_CC ^ 0x00);
      myPort.write(START_PACKET);
      myPort.write(0x05);
      myPort.write(ACK_INTERNAL_CC);
      myPort.write(0x00);
      myPort.write(checksum);
      myPort.write(END_PACKET);
    }
  } else if (connected == true ) {
    font = createFont("AdobeHeitiStd-Regular", 12);  
    connectButton.setLabel("CONNECT");
    connected = false;
    gui_state = DISCONNET_COMMAND_GIVEN;

    byte checksum = (byte) (START_PACKET ^ 0x05 ^ ACK_DISCONNECT_CC ^ 0x00);
    myPort.write(START_PACKET);
    myPort.write(0x05);
    myPort.write(ACK_DISCONNECT_CC);
    myPort.write(0x00);
    myPort.write(checksum);
    myPort.write(END_PACKET);
  }
}

1 Like

Hi @mohitSingh, Interesting project, nice looking GUI. As you say there’s more there than anyone can immediately analyse.

For following the sequence of what happens I suggest you print out everything that is sent and received, and your program states. Run it for a few moments After a few moments run, copy the console window to an editor, and study what happened.

Are you using a real com port? that is always there on the PC? or a software created one, like the Arduino drivers create? If the software type, are you disconnecting the board with the sketch still running? Then reconnecting it or another. If the order of actions is wrong the port won’t work until you fix it. Let me know and I’ll give more details if relevant.

(You really should convert some of your code to use arrays. This would eliminate lots of lines, and make small adjustments to e.g. size and spacing of objects much easier.)

Hi,

Thanks alot for the reply.

I debugged it for a while and got to understand the problem(somewhat).

When the serial.write() was inside the draw() function it was writing the port as soon as it was opened.
And I was using Arduino to simulate my Board under Test.
So as soon as port was opened, the arduino got reset and started expecting program code and got confused with my serial.write commands.
So I added a delay after I opened the port and the problem got away.
This problem wouldn’t have occurred in my actual board under test.
I should have added that I was using arduino for testing

I fixed this and many other bugs and now my GUI is working pretty great.

And about the arrays… I agree with you… I just started very small and it scaled up very much more than expected. I will do that when I start optimizing the code.It was a great learning experience for me to work on processing. It is pretty nice.

Thanks again for your guidance.

1 Like