error disabling serialEvent ?

edited June 2017 in Arduino

i am trying to talk both way round from processing to arduino as well as from arduino to processing. while processing to arduino link is absolutely fine but i guess it shows the error for arduino to processing and it reads somewhat this: "error disabling serialEvent() for COM3 null". well i have posted my code below please tell me where i am going wrong.the prime idea is to create a GUI for bluetooth controlled bot and to send feeback from arduino to processing regarding the motor encoder to plot the movement graph(closed loop drive).

processing:
        import processing.serial.*;
        Serial port;
             int index=0;
            String lmotor,rmotor,data,status;
            float lm=0,rm=0,xpos=0,ypos=height;
            void settings()
            {
              size(550,600);
            }
            void setup()
            {
               SecondApplet sa= new SecondApplet();
              String[] args = {"MY Sketch"};
              PApplet.runSketch(args, sa);
              port= new Serial(this, "COM3", 9600);
               port.bufferUntil('.');

        }

        void serialEvent (Serial port)
        {
          println(data);
           data=port.readStringUntil('.');
           data=data.substring(0,data.length()-1);
           index=data.indexOf(",");
           lmotor= data.substring(0, index);
           rmotor= data.substring(index+1 , data.length());
           lm=float(lmotor);
           rm=float(rmotor);
           lm=map(lm,0,9999,0,width);
           rm=map(rm,0,9999,0,height);



        }

        void octagon(int x, int y, int radius)
        {
          float angle= TWO_PI/ 8;
          beginShape();
          for(float a=0; a<TWO_PI; a+= angle)
          {
            float sx= x + cos(a) * radius;
            float sy= y + sin(a) * radius;
            vertex(sx,sy);
          }
          endShape(CLOSE);
        }
        void draw() {
          background(245, 255, 245);
          fill(20 , 160, 133);
          stroke(33);
          strokeWeight(3);
          rect(50, 150, 200, 100, 10);
          rect(300, 150, 200, 100, 10);
          fill(255);
          textSize(34);
          text("FORWARD" , 70,205);
          text("BACKWARD", 305, 205);
          textSize(28);
          fill(33);
          text("CURRENT STATUS:", 180, 300);
          //stop button
          fill(176, 28, 46);
          octagon(60,60, 50);
          fill(255);
          rect(30,50,60,17);
          textSize(24);
          fill(33);
          text("STOP", 30,135);
          //left arrow
           ellipseMode(CORNER);
          fill(255,77,0);
          ellipse(35,440,220,120);
          fill(255);
          beginShape();
          {
            vertex(40, 500);
            vertex(100, 450);
            vertex(100, 475 );
            vertex(240,475 );
            vertex(240,525);
            vertex(100, 525);
            vertex(100,550);
          }
          endShape(CLOSE);
           // right arrow
           ellipseMode(CORNER);
           fill(255,77,0);
           ellipse(295,440,220,120);
           fill(255);
           beginShape();
          {
            vertex(510, 500);
            vertex(450, 450);
            vertex(450, 475 );
            vertex(310,475 );
            vertex(310,525);
            vertex(450, 525);
            vertex(450,550);
          }
          endShape(CLOSE);
          if(mousePressed && mouseX>10 && mouseX<110 && mouseY>10 && mouseY<110)
          {
             port.write('0');
             stroke(255,0,0);
            strokeWeight(2);
            noFill();
            octagon(60,60, 50);
            status="STOP";
          }
          if(mousePressed && mouseX>50 && mouseX<250 && mouseY>150 && mouseY<250)
          {
            port.write('F');
             stroke(255,0,0);
            strokeWeight(2);
            fill(255);
            rect(50,150,200,100,10);
            status="FORWARD";
            }
           if(mousePressed && mouseX>300 && mouseX<500 && mouseY>150 && mouseY<250)
          {
             port.write('B');
             stroke(255,0,0);
            strokeWeight(2);
            fill(255);
            rect(300,150,200,100,10);
            status="BACKWARD";
          }
           if(mousePressed && mouseX>35 && mouseX<255 && mouseY>440 && mouseY<560)
          {
              port.write('L');
             stroke(255,0,0);
            strokeWeight(2); 
           ellipseMode(CORNER);
             fill(255);
          ellipse(35,440,220,120);  
          status="LEFT";
         }
           if(mousePressed && mouseX>295 && mouseX<515 && mouseY>440 && mouseY<560)
          {
             port.write('R');
             stroke(255,0,0);
            strokeWeight(2); 
           ellipseMode(CORNER);
             fill(255);
         ellipse(295,440,220,120);
         status="RIGHT";
         }
          if(status=="FORWARD")
          {
            textSize(70);
          fill(random(0,255),0,random(0,255));
          text(status,140,380);
          }
          if(status=="BACKWARD")
          {
            textSize(70);
          fill(random(0,255),0,random(0,255));
          text(status,120,380);
          }
          if(status=="RIGHT")
          {
            textSize(70);
          fill(random(0,255),0,random(0,255));
          text(status,200,380);
          }
          if(status=="LEFT")
          {
            textSize(70);
          fill(random(0,255),0,random(0,255));
          text(status,230,380);
          }
          if(status=="STOP")
          {
            textSize(70);
          fill(random(0,255),0,random(0,255));
          text(status,210,380);
          }



        }
        public class SecondApplet extends PApplet {




          public void settings() {
            size(550, 600);
          }
          public void setup()
          {
            background(0);
          }
          public void draw()
          {
             stroke(255);
          strokeWeight(5);
           line(xpos,ypos,lm,height-rm);
          xpos=lm;
          ypos=height-rm;
          if(lm>=width-1)
          {
            xpos=0;
            lm=0;
            background(0);
          }
          if(rm>=height-1)
          {
            ypos=0;
            rm=0;
            background(0);
          }



      }
    }

arduino code: #include <avr/io.h> #include <util/delay.h>

#define SETBIT(ADDRESS, BIT) (ADDRESS|=(1<<BIT));
#define CLEARBIT(ADDRESS, BIT) (ADDRESS&=~(1<<BIT));
#define CHECKBIT(ADDRESS, BIT) (ADDRESS & (1<<BIT));
char val,c;
int sfl=0,sfr=0,temp,y=0;

void interrupt_init()
{
  cli();
  EIMSK=0x03;
  EICRA=0x0A;
  sei();

}
void encoder_pin_config_init()
{
  DDRD=DDRD & 0xF3;
  //INTERNALPULLUPS
  SETBIT(PORTD, PD3);
  SETBIT(PORTD, PD2);
}
void byte_init (int baud)
{
    UBRR0H = (baud>>8);                      // shift the register right by 8 bits
    UBRR0L = baud;                           // set baud rate
    UCSR0B|= (1<<TXEN0)|(1<<RXEN0);                // enable receiver and transmitter
    UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01);   // 8bit data format
}
void byte_transmit (unsigned char data)
{
    while (!( UCSR0A & (1<<UDRE0)));                // wait while register is free
    UDR0 = data;                                   // load data in the register
}
unsigned char byte_receive (void)
{
    while(!(UCSR0A) & (1<<RXC0));                   // wait while data is being received
    return UDR0;                                   // return 8-bit data
}


int main(void)
{
  byte_init(103);
  interrupt_init();
  encoder_pin_config_init();
  DDRB = (1<<PB1) | (1<<PB2); // PWM pins && Enable pins
DDRD = (1<<PD4) | (1<<PD5) | (1<<PD6) | (1<<PD7) ; //Motor Pins

SETBIT(PORTB, PB1);
SETBIT(PORTB,PB2);
while(1)
{
val=byte_receive();
if(val=='0')
{
  Stop();
}
if(val=='F')
{
  Forward();
}
if(val=='B')
{
  Backward();
}
if(val=='R')
{
  right();
} 
if(val=='L')
{
  left();
}
temp=sfl;
while(temp>0)
{
  y=temp%10;
  c=y+'0';
  byte_transmit(c);
  temp=temp/10;

}
temp=sfr;
while(temp>0)
{
  y=temp%10;
  c=y+'0';
  byte_transmit(c);
  temp=temp/10;
}
byte_transmit('.');
if(sfl==9999)
{
  sfl=0;
}
if(sfr==9999)
{
  sfr=0;
}
}

}
void Stop()
{
  CLEARBIT(PORTD,PD4);
  CLEARBIT(PORTD, PD5);
  CLEARBIT(PORTD, PD6);
  CLEARBIT(PORTD, PD7);
}
void Forward()
{
  SETBIT(PORTD, PD4);
  CLEARBIT(PORTD, PD5);
  SETBIT(PORTD, PD6);
  CLEARBIT(PORTD, PD7);
}
void Backward()
{
  SETBIT(PORTD, PD5);
  CLEARBIT(PORTD, PD4);
  SETBIT(PORTD, PD7);
  CLEARBIT(PORTD, PD6);
}
void left()
{
  CLEARBIT(PORTD, PD4);
  CLEARBIT(PORTD, PD5);
  SETBIT(PORTD, PD7);
  CLEARBIT(PORTD, PD6);
}

    void right()
    {
  SETBIT(PORTD, PD6);
  CLEARBIT(PORTD, PD6);
  CLEARBIT(PORTD, PD4);
  CLEARBIT(PORTD, PD5);
}

ISR(INT0_vect)
{
  sfl++;
}
ISR(INT1_vect)
{
  sfr++;
}`

Answers

  • My first guess without looking at your code is to add a delay in your Processing sketch in setup after you create your serial handler. Something like

    void setup(){
    
       ...YOUR setup commands here
    
      delay(1000);    
    }
    

    Kf

  • well can the problem be due to having a common pathway for serial communication in both ways i.e. from arduino to processing as well as processing to arduino?

  • Common pathway? The arduino is connected to COM3. They have to share a physical connection so they can communicate. Each arduino and processing need to create a serial handler. Each writes and the other listens and captures the information. For example, in Processing, you open the port using a serial object, and then you send data to your arduino via write or print and reads data from it via read, readBytes, readString, etc.

    Just make sure you are not using the serial monitor on your arduino unit while running the sketch.

    Also chec this previous post as a reference: https://forum.processing.org/two/discussion/comment/98969/#Comment_98969

    In your serialEvent, make sure you are not working on a null object. I am referring to your line 25. Also as an aside comment, using the character '.' for splitting your packages is not a good idea as the dot/period is used as the decimal point when working with float or double precision. I believe this is inconvenient either when processing your data in bytes or string format.

    Kf

  • edited June 2017

    @kfrajer well no i am not using serial monitor as all the communicstion is being done via bluetooth. as i can see my program that i have written is very big.therefore, i have written a simplified example for you where i am just using bluetooth to send the to processing via arduino but still the error is same.the simpleified program is written below processing: import processing.serial.*; Serial port; int index=0; String lmotor,rmotor,data,status;

    void setup()
    {
      size(500,500);
       port= new Serial(this, "COM3", 9600);
       port.bufferUntil('A');
    
    }
    void serialEvent(Serial port)
    {
      if(data!=null)
      {
      println(data);
       data=port.readStringUntil('A');
       data=data.substring(0,data.length()-1);
       index=data.indexOf(",");
       lmotor= data.substring(0, index);
       rmotor= data.substring(index+1 , data.length());
      }
    }
    void draw()
    {
      background(255);
      textSize(100);
      fill(random(0,255),0,random(0,255));
      text(lmotor,50,150);
      textSize(100);
      fill(0,random(0,255),random(0,255));
      text(rmotor, 300,150);
    
    }
    

    arduino/avr code: #include <avr/io.h> #include <util/delay.h>

        #define SETBIT(ADDRESS, BIT) (ADDRESS|=(1<<BIT));
        #define CLEARBIT(ADDRESS, BIT) (ADDRESS&=~(1<<BIT));
        #define CHECKBIT(ADDRESS, BIT) (ADDRESS & (1<<BIT));
        char c;
        int sfl=0,sfr=0,y=0,temp;
    
        void interrupt_init()
        {
          cli();
          EIMSK=0x03;
          EICRA=0x0A;
          sei();
    
        }
        void encoder_pin_config_init()
        {
          DDRD=DDRD & 0xF3;
          //INTERNALPULLUPS
          SETBIT(PORTD, PD3);
          SETBIT(PORTD, PD2);
        }
        void byte_init (int baud)
        {
            UBRR0H = (baud>>8);                      // shift the register right by 8 bits
            UBRR0L = baud;                           // set baud rate
            UCSR0B|= (1<<TXEN0)|(1<<RXEN0);                // enable receiver and transmitter
            UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01);   // 8bit data format
        }
        void byte_transmit (unsigned char data)
        {
            while (!( UCSR0A & (1<<UDRE0)));                // wait while register is free
            UDR0 = data;                                   // load data in the register
        }
        unsigned char byte_receive (void)
        {
            while(!(UCSR0A) & (1<<RXC0));                   // wait while data is being received
            return UDR0;                                   // return 8-bit data
        }
    
        void setup() {
          interrupt_init();
          byte_init(103);
          encoder_pin_config_init();
         Serial.begin(9600);
        pinMode(4,HIGH);
        pinMode(5,OUTPUT);
        pinMode(6,OUTPUT);
        pinMode(7,OUTPUT);
        pinMode(9,OUTPUT);
        pinMode(10,OUTPUT);
        digitalWrite(9,HIGH);
        digitalWrite(10,HIGH);
        }
    
    void loop() {
    
      digitalWrite(4, HIGH);
      digitalWrite(5,LOW);
      digitalWrite(6, HIGH);
      digitalWrite(7,LOW); 
    temp=sfl;
    while(temp>0)
    {
      y=temp%10;
      c=y+'0';
      byte_transmit(c);
      temp=temp/10;
    
    }
    byte_transmit(',');
    temp=sfr;
    while(temp>0)
    {
      y=temp%10;
      c=y+'0';
      byte_transmit(c);
      temp=temp/10;
    
    }
    byte_transmit('A');
    
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
    digitalWrite(4,LOW);
    
    }
    
    
    ISR(INT0_vect)
    {
      sfl++;
    }
    ISR(INT1_vect)
    {
      sfr++;
    }
    
  • edited June 2017

    Your processing code should be more like this:

    void serialEvent(Serial port) {
    
      data=port.readStringUntil('A');
    
      if(data!=null)  {
       data=data.substring(0,data.length()-1);
       index=data.indexOf(",");
       lmotor= data.substring(0, index);
       rmotor= data.substring(index+1 , data.length());
      }
    }
    

    Another version using split():

    void serialEvent(Serial port) {
    
      String data=port.readStringUntil('A');
    
      if(data!=null)  {
       String[] vals = split(trim(data),',');  //Comma as dlimiter
    
        if(vals.length==2){
           lmotor=vals[0];
           rmotor=vals[1];
        }
      }
    }
    

    ***********EDIT: Also a small change in your setup and draw:

    void setup()
    {
      size(500,500);
       port= new Serial(this, "COM3", 9600);
       port.bufferUntil('A');
      textSize(100);
      fill(random(0,255),0,random(0,255));
    
    }
    
    void draw()
    {
      background(255);
      text(lmotor+"\t\t\t"+rmotor,50,150);
    }
    

    Kf

  • For the GUI, you could use controlP5 or G4P. You can install either library through the Library manager in the porcessing IDE.

    For the arduino BT, what board are you using? If the board doesn't come with a simple example, I'd recommend making a simple sketch where you send and receive data from Processing to Arduino and visce-versa as this kind of simple sketches come handy for debugging, or at least to ensure the connection between devices is working properly.

    Kf

  • edited June 2017

    @Kfrajer well perfect it is working fine now the simplified example. one thing i understood that the error which is coming in my actual project is due to because i am using multiple window and transferring the data to another window from serial event have a look at the processing code and please tell me any way to send the data from serial event to the other window and please check what is wrong in this code now:

       import processing.serial.*;
        Serial port;
        int index=0;
        String data,status;
        float lmotor=0,rmotor=0;
        void settings()
        {
          size(550,600);
        }
        void setup()
        {
           SecondApplet sa= new SecondApplet();
          String[] args = {"MY Sketch"};
          PApplet.runSketch(args, sa);
           port= new Serial(this, "COM3", 9600);
           port.bufferUntil('A');
    
        }
        void serialEvent(Serial port)
        {
          println(data);
          data=port.readStringUntil('A');
          if(data!=null)
          {
           data=data.substring(0,data.length()-1);
           index=data.indexOf(",");
           lmotor= float(data.substring(0, index));
           rmotor= float(data.substring(index+1 , data.length()));
          }
        }
        void draw()
        {
          background(255);
          textSize(100);
          fill(random(0,255),0,random(0,255));
          text(lmotor,50,150);
    
    
        }
        public class SecondApplet extends PApplet {
           public void settings() {
            size(550, 600);
          }
          public void draw()
          {
            background(255);
            textSize(100);
          fill(0,random(0,255),random(0,255));
          text(rmotor, 200,150);
          }
        }`                       
    
  • @kfrajer thanks buddy you have been very much helpful this is the last thing i want to know regarding the topic.

  • @drocobeth

    One thing i understood that the error which is coming in my actual project is due to because i am using multiple window and transferring the data to another window from serial event

    What makes you think the error is because you have multiple windows? What error do you get? Can you copy and past it here? From what I see, you are loading your global variables and your windows should be able to access these data with no issues.

    Kf

  • @kfrajer "error disabling serialEvent() for COM3 null" this is the error which i have been getting? what can be its cause then? if not the multiple windows?

  • Answer ✓

    Two things. If two sketches is causing the problem, you can try always and reduce all the operations to one sketc. this way, you can try and see if this is indeed the problem. On the other hand, I think you should try running your code with the version I provided before using split and ensure you have two element array from the split operation before accessing the data.

    Kf

  • @kfrajer well you were right the error was not due to multiple windows. it is working completely fine in the multiple windows. infact, the problem was due to transmission of data from encoder motors. thanks for your help.

  • @drocobeth You are very welcome.

    Kf

Sign In or Register to comment.