Serial Processing does not read or write anything to controller

Hi Team,

I have written a processing software for the first time for my project.
The concept is to read data from and write commands to pulser controller over the serial port.
However, even though I see the COM port getting connected I am not able to send or receive any communication to and from the upulser over Serial port.
I have verified the uPulser and it is in working condition through the firmware option.
But I need to develop this GUI software for QA-QC purpose.
Kindly provide your input what would be the reason and how would I resolve it.

Please find below two files of the code. I don’t know how to attach the code so copy pasting the code below.

------------------Tester file where set up occurs ------------------------

import controlP5.*;
import processing.serial.*;
import java.util.*;

ControlP5 cp5;

float rev=0.2;

boolean connected = false;
byte port_num=-1;  // the Serial.list() call below will list all the available serial ports on your host...Enter the port number here
boolean setup = true;

int BitTimes=0;
int Attenuation=0;
int iBittime=3;
int iAttn=5;
int iRGD=1500;
int iSamples=10;
int iThreshold=70;
int Samples=1;
int Threshold=0;
int iPulsedelay=0;
int iStarttime = 0;
int iPeakMin;
int iPeakMax;
float strikeRefl;
float strikeInt;
int reflLimit = 80;
int intLimit = 80;
float reflDist = 41;
float colDepth;
String timeName;
String dateName;
int savedTime;
int totalTime = 1500;
PrintWriter sample_file;  // Vector output tofile

PFont f, b, s;  
Serial port;  // Create object from Serial class
int[] values; // Vector array received from the serial port


// Variable Gain unit specific change here.
int mode_reg=0x0C;  // VGA type

int x1, x2, y1, y2;

int iPause = 0;  // state variable to control the draw method 0 = draw, 1 = pause
int iScreenX;
int iScreenY;

int iVectorWindowX;
int iVectorWindowY = 512;
float fVelocity = 1.497; // water mm/us = 1497 m/s
//float fVelocity = 6.100; // steel mm/us = 6100 m/s

int iControlX = 0; // location of controls
int iControlY = 550;
int c1, c2, c3, c4;
void setup() 
{
  size(1024, 768);
  iScreenX = width;
  iScreenY = height;
  iVectorWindowX = iScreenX;
  iVectorWindowY = iScreenY*512/768;
  iControlX = 0;
  iControlY = iScreenY*550/768;
  c1 =10;
  c2 = iScreenX/3-150;
  c3 = iScreenX/3*2-200;
  c4 = iScreenX-310;
  println("c1: "+c1+"  c2: "+c2+"  c3: "+c3+" c4: "+c4);


String[] availablePorts = Serial.list();
  printArray(Serial.list());
  for(int i = 0;i<=availablePorts.length-1;i++){
    if (availablePorts[i].contains("tty.usbserial")||
        availablePorts[i].contains("COM7")||
        availablePorts[i].contains("ACM0")){
      port_num = (byte)i;    
      println(i);
      connected=true;
}
  }
  if (port_num!=-1){
  port = new Serial(this, Serial.list()[port_num], 9600);
  }

println("Connected");
  f = loadFont("GillSans-24.vlw");
  b = loadFont("GillSans-14.vlw");
  s= loadFont("GillSans-12.vlw");
  textFont(f);
  textFont(b);
  textFont(s);
  fill(255, 255, 255);
  println("Make Buttons");
  cp5 = new ControlP5(this);
  cp5.addButton("Stop")
    .setFont(b)
    //.setValue(0)
    .setPosition(c1, iControlY+90)
    .setSize(100, 39)
    ;
  cp5.addButton("Pause")
    .setFont(b)
    //.setValue(0)
    .setPosition(c1, iControlY+130)
    .setSize(100, 39)
    ;
  cp5.addButton("Resume")
    .setFont(b)
    //.setValue(0)
    .setPosition(c1, iControlY+170)
    .setSize(100, 39)
    ;
println("MakeSliders");

  cp5.addSlider("Attenuation")
    .setPosition(c2, iControlY+105)
    .setSize(200, 29)
    .setRange(0, 127)
    .setSliderMode(Slider.FLEXIBLE)
    .setFont(s)
    ;
  cp5.getController("Attenuation").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(2);
  cp5.getController("Attenuation").getValueLabel().align(ControlP5.RIGHT, ControlP5.TOP_OUTSIDE).setPaddingX(2);

  cp5.addSlider("BitTimes")
    .setPosition(c2, iControlY+170)
    .setSize(200, 29)
    .setRange(0, 3)
    .setNumberOfTickMarks(4)
    .setSliderMode(Slider.FLEXIBLE)
    .setValue(iBittime)
    .setFont(s)
    ;
  cp5.getController("BitTimes").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(2);
  cp5.getController("BitTimes").getValueLabel().align(ControlP5.RIGHT, ControlP5.TOP_OUTSIDE).setPaddingX(2);

  cp5.addSlider("Samples")
    .setPosition(c3, iControlY+105)
    .setSize(200, 29)
    .setRange(1, 25)
    .setSliderMode(Slider.FLEXIBLE)
    .setValue(Samples)
    .setFont(s)
    ;
  cp5.getController("Samples").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(2);
  cp5.getController("Samples").getValueLabel().align(ControlP5.RIGHT, ControlP5.TOP_OUTSIDE).setPaddingX(2);

  cp5.addSlider("Threshold")
    .setPosition(iControlX+c3, iControlY+170)
    .setSize(200, 29)
    .setRange(0, 255)
    .setSliderMode(Slider.FLEXIBLE)
    .setValue(iThreshold)
    .setFont(s)
    ;
  cp5.getController("Threshold").getCaptionLabel().align(ControlP5.LEFT, ControlP5.TOP_OUTSIDE).setPaddingX(2);
  cp5.getController("Threshold").getValueLabel().align(ControlP5.RIGHT, ControlP5.TOP_OUTSIDE).setPaddingX(2);
   println("Set Frame rate and background");

  frameRate(10);
  background(0);
  Setbaud(9600/*230400*/);
  setup = true;
}

--------------------------Communication file where I write command and read data back from the pulser controller-------------------

/*==============================================*/
/*================== Threshold =================*/
/*==============================================*/

// Set the value of the threshold for the comparitor
// this is used to detect echoes with out the need to analyze the vector
// retunred values or in bittimes 

public void Threshold(int tsv)
{
  if (setup) {
    println("Threshold slider: "+tsv);
  }
  iThreshold = tsv;
  if (connected)
  {
    port.write(0x02);
    port.write(0x26);
    println("Port Available: "+port.available());
    while (port.available () < 1) {
      println(port.read());
    }
    port.write(0x02);
    port.write(0x01);
    port.write(iThreshold);
    port.write(0x04);

    while (port.available () < 1) {
      port.read();
    }
  }
}


/*==============================================*/
/*================== Attenuation ===============*/
/*==============================================*/

// set attenuation of receiver
// Valid values 0-127
// 0 = max attn
// 127 = min attn
public void Attenuation(int asv)
{
  int reply;
  if (setup) {
    println("Attenuation slider: "+asv);
  }
  iAttn = asv;
  if (connected)
  {
    if (iAttn < 0)
      iAttn = 0;
    else if (iAttn > 127)
      iAttn = 127;
println("contact uPulser");
    port.write(0x02);
    println("send Register selection");
    port.write(0x24);

    while (port.available () < 1) {
      reply = port.read();
      println(reply);
    }
    
    port.write(0x02);
    port.write(0x01);
    port.write(iAttn);
    port.write(0x04);

    while (port.available () < 1) {
      reply = port.read();
      println(reply);
    }
  }
}

------------------------Draw file where I get output on GUI--------------------------------------------------------------------------

void draw() // this runs at refresh rate, this is an autonomous operation...runs to completion before any other callbacks execute
{  
  if (iPause == 0) 
  {
    background(0);

    fill(75, 75, 75);
    rect(0, 0, 50, iVectorWindowY);
    rect(974, 0, 50, iVectorWindowY);
    fill(255, 255, 255);
    triangle(10, 10, 20, 0, 20, 20);
    triangle(1004, 0, 1014, 10, 1004, 20);
    textFont(f);
    fill(255); 

    text("EcoInsight Pulser Test rev:"+rev, iControlX+10, iControlY);

    textAlign(RIGHT);
    text("Gain", c4, iControlY+100);
    text("Zoom", c4, iControlY+130);
    text("Average", c4, iControlY+160);
    text("Thresold", c4, iControlY+190);
    textAlign(LEFT);

    stroke(204, 102, 0); // draw orange horizontal line
    line(0, ((255-iThreshold)*iVectorWindowY/256), 1023, ((255-iThreshold)*iVectorWindowY/256));
    println("here");

    if (connected) {
      port.write(0x02);  // <STX>

      port.write(0x00);  // send command to start probe sample

      while (port.available () < 1) {
        port.read();    // get <STX> from probe
      }
      while (port.available () < 1) {
        println("LSB: "+port.read()); // get LSB #bytes
        port.read();
      }
      while (port.available () < 1) {
        println("MSB: "+port.read());  // get MSB #bytes7
        port.read();
      }

      // ********* get vector from probe ************
      for (int x=0; x<1024; x++) {
        while (port.available () < 1) {
          values[x]= (port.read());  // read 1024 bytes
          sample_file.println(values[x]);
        }
      } // get vector for loop

      while (port.available () < 1) {
        port.read();  // get <EOT> from Probe
      }
      stroke(255);

      for (int x=0; x<1023; x++)
      {
        x1 = x;
        x2 = x+1;
        y1 = (255-values[x1])*iVectorWindowY/256; // scale the values
        y2 = (255-values[x2])*iVectorWindowY/256;
        line(x1, y1, x2, y2);
        println(x1+","+y1+","+x2+","+y2);
      } // draw vector for loop
      //******** Threshold Logic ***********
      //Compare each point in the vector to a threshold
      //When the compare triggers display the time of the Echo

      for (int x=0; x<1024; x++) 
      {

        if (iThreshold >= values[x])
        {
          //println(x);       
          println("Bittime in seconds:"+Bittime()+" Time:"+ (x*(Bittime())+(iRGD*Bittime()))*1000000);
          sample_file.println("Bittime in seconds:"+Bittime()+" Time:"+ (x*(Bittime())+(iRGD*Bittime()))*1000000);

          strikeRefl = (x*(Bittime())+(iRGD*Bittime()))*1000000;
          if (strikeRefl < 90) {
            text("Reflector Peak "+((x*(Bittime())+(iRGD*Bittime()))*1000000)+" us", iControlX+300, iControlY);
            println ("Strike Reflector = "+strikeRefl);
            sample_file.println ("Strike Reflector = "+strikeRefl);

            fVelocity = (reflDist*2)/strikeRefl;
            println ("fVelocity) = "+fVelocity);
            sample_file.println ("fVelocity) = "+fVelocity);

            text("In Situ Velocity: " +fVelocity +" mm/us", iControlX+300, iControlY+30);

            stroke(255, 0, 0);
            line(x, 0, x, iVectorWindowY);
            break;
          }
        }
      } // threshold for loop

      for (int x=0; x<1024; x++) 
      {
        if (iThreshold >= values[x])
        {
          strikeInt = (x*(Bittime())+(iRGD*Bittime()))*1000000;
          if (strikeInt > 90)
          {
            text("Interface Peak: ", iControlX+100, iControlY);  
            text(strikeInt +" us", iControlX+100, iControlY+30); 
            text("Oil Column Depth:", iControlX+100, iControlY+60);
            text((0.5*strikeInt*fVelocity)+" mm", iControlX+100, iControlY+90);
            stroke(255, 0, 0);
            line(x, 0, x, iVectorWindowY);

            sample_file.println ("Interface Peak: "+strikeInt);
            sample_file.println ("Oil Column Depth: "+(0.5*strikeInt*fVelocity));

            break;
          }
        }
      }

      // ************* PP Logic ****************
      iPeakMax = 0;
      iPeakMin = 512;      

      for (int x=0; x<1024; x++) 
      {
        if (values[x] > iPeakMax)
          iPeakMax = values[x];
        //println("iPeakMax: " +iPeakMax);
        if (values[x] < iPeakMin)
          iPeakMin = values[x];
        //println("iPeakMin: " +iPeakMin);
      }

      text("Relative Peak Attenuation: " +(iPeakMax-iPeakMin), iControlX+300, iControlY+60);

      // ************ draw vector ************
      stroke(255);

      for (int x=0; x<1023; x++)
      {
        x1 = x;
        x2 = x+1;
        y1 = (255-values[x1])*iVectorWindowY/256; // scale the values
        y2 = (255-values[x2])*iVectorWindowY/256;
        line(x1, y1, x2, y2);
        sample_file.println(x1+","+y1+","+x2+","+y2);
      } // draw vector for loop
    }

    // draw the vertical cursor

    if (mouseX<1024 && mouseY<iVectorWindowY) 
    {
      stroke(204, 102, 0);
      line(mouseX, 0, mouseX, iVectorWindowY);
      text(nf(Time(mouseX), 0, 2)+"us", mouseX, 500);
      if ((mousePressed == true) && (mouseX > 50) && (mouseX < 974)) 
      {// set start time to cursor position
        starttime(str(int(Time(mouseX))));
      } else if ((mousePressed == true) && (mouseX > 0) && (mouseX < 50)) 
      { // set start time earlier
        iStarttime = iStarttime - int((512.0*Bittime()*1000000.0));
        if (iStarttime < 0)
          iStarttime = 0;
        starttime(str(iStarttime));
      } else if ((mousePressed == true) && (mouseX > 974) && (mouseX < 1024))  
      {// set start time later
        iStarttime = iStarttime + int((512.0*Bittime()*1000000.0));
        starttime(str(iStarttime));
      }
    }


    // draw the x/y axis and label them
    stroke(128, 128, 128);
    line(0, iVectorWindowY/2, 1024, iVectorWindowY/2);
    line(0, iVectorWindowY, iVectorWindowX, iVectorWindowY);
    for (int x = 0; x< 1024; x= x+42) 
    {
      line(x, iVectorWindowY/2+5, x, iVectorWindowY/2-5);
    }
    text(nf(Time(0), 0, 2)+"µs", 10, iVectorWindowY/2+20); 
    textAlign(RIGHT);
    text(nf(Time(1024), 0, 2)+"µs", iVectorWindowX-10, iVectorWindowY/2+20);
    textAlign(LEFT);
  }
}

The issue is that if I edit the COM port in the first file to the com port the pulser is detected on, I get no communication between that and GUI and if I edit the COM port to a different number it is not getting connected (which is correct).
So even if it gets connected why the communication does not occur.

Please help.

Vikram

1 Like

sorry, not sure i got your concept,

but you can not run 2 programs connecting to one COM port,
only one will connect.

so what i did for Raspberry Pi talking to Arduino USB ( what runs a menu interface )

  • -a- service program
    … handle the communication, ( opens COM port and keeps it open! )
    … prints the ( evaluated ) incomming data to a RAM DISK file
    ( over writing mode )
    … checks for a command file in RAM DISK
    reads and empties it ( to confirm it got it already),
    and send the approprieate commands via COM port out to the arduino menu
    … uses the incomming data for a reduction and write ( append )
    to a historic data file.

  • -b- operator interface ( GUI ) program
    … reads the incomming RAM DISK file
    and make the visualization
    … on operator action writes commands to the command file.

the good thing with this concept is:
the service program will be autostart, background…

the operator program will be started ( and stopped ) when needed,
still ( like data collection ) will go on in the background by the service program.
old example at kll.engineering-news.org/kllfusion01/articles.php?article_id=92

//____________________
but all this is not needed if you have just one processing program
open COM port read and write…

1 Like

Please format your Code correctly. You can do so by pasting the preformatted (Press Str+T/CTRL+T in processing) Code in the space created with the </> sign.

1 Like