Many Serial Com Opened... how to manage them?

Hi, I need to connect via serial com many uart devices. There are two piece of code in my project.

The first is the open serial channel

    // initialize the serial port selected in the listBox
    byte initSerial(String portName, int portIDX, int portWhat) {
      byte init_com = 0;
      if (portWhat==1) {
        try {
          Serial_Port[portIDX] = new Serial(this, portName, 115200, 'N', 8, 1);
          init_com=1;
          Serial_Port_WatchDog[portIDX] = millis() + CONTROLLER_WATHCDOG;
        } 
        catch (RuntimeException e) {
          init_com=0;
        }
      } else {
        init_com=0;
        Serial_Port[portIDX].stop();
      }
      return init_com;
    }

The second part of code is here:

void serialEvent(Serial p) {
      if (p.available() == Frame_Size_Read) {
        Serial_Buffer_Discr++;
        if (Serial_Buffer_Pointer < Serial_Buffer_Size) { 
          Serial_Buffer_Pointer++;
          if (Serial_Buffer_Max < Serial_Buffer_Pointer) { Serial_Buffer_Max = Serial_Buffer_Pointer; }
          if (Serial_Buffer_Act < Serial_Buffer_Pointer) { Serial_Buffer_Act = Serial_Buffer_Pointer; }
        } else { 
          Serial_Buffer_Pointer = 0; 
        }
        inBufRegister[Serial_Buffer_Pointer] = p.readBytes();
      }
    }

in other part of code I read all data in inBufRegister array.

The first question: I need to know from which com port the data become in serialEvent() routine, like COM1, COM2... and so on. The second question: during data receiving I often (I think due to a high amount of data incoming) receive a "Error, disabling serialEvent() for COMx". Whats happen? And somethime seems to have a desyncronization between the uart device and the processing serialEvent routine. There is a method to empty serial buffer and force syncronization? Many thanks to all.

Answers

  • If you thoroughly read ALL of the information in the link below it will answer most of your questions:

    https://processing.org/reference/libraries/serial/index.html

  • Format your code. Press the gear icon to edit your post, then select your code and press ctrl+o. Leave an empty line above and below your code.

    Kf

  • I readed all the official Processing Reference, but I do not found any help. Any other help?

  • edited February 2017

    Hopefully some folks with more expertise on the Serial library will chime in but in the meantime you might try simplifying your code. Here are some very simple code fragments to give you some ideas??

        Serial A_Port;
        Serial B_Port;
        Serial C_Port; 
        ...
        ...
        ...
    
        A_Port = new Serial(this, "COMxx", 115200);
        B_Port = new Serial(this, "COMxx", 115200);
        C_Port = new Serial(this, "COMxx", 115200);
    
        ...
        ...
        ...
    
        void serialEvent(Serial p) {
          code to detect which port is sending
          and to parse it correctly.
        }
    
  • No matter how many Serial instances we create, there's only 1 serialEvent() for all of them. L-)

  • Better look up older forum threads about the subject: :-B
    https://forum.Processing.org/two/discussions/tagged?Tag=readbytes()

  • edited February 2017

    Ok, thanks for correcting my mistake. I modified my code snippets for this.

  • @mikearding

    Not sure about your second question. For the first question and adding to @pxgator code:

    void serialEvent(Serial p) {
    
      if(p==A_port)  { 
         println("It got data from port A. Now get the data using any of the readBuffer functions");}
    }
    

    Kf

Sign In or Register to comment.