Receive and send datas to the same port. Possible?

Hello ladies and gentlemen;

I read and receive datas to and from twos differents serial usb ports.

Is it possible to do this on the same serial port.?

I tried but if i read data on the same serial.port, Processing tell me port busy.

In the setup my ports are managed like that

  teensyport = new Serial(this,ports[2],115200); //  si port connecté
  //*************** WITHOUT TEENSY connected
    DueSerialNativeUSBport101 = new Serial(this, Serial.list()[3], 115200);

  // Read bytes into a buffer until you get a linefeed (ASCII 10):
    DueSerialNativeUSBport101.bufferUntil('\n');

I read datas from an arduino with serial event

void serialEvent(Serial DueSerialNativeUSBport101) { // receive 2 datas splited with , and the last is send with println

  // read the serial buffer:
  String myString = DueSerialNativeUSBport101.readStringUntil('\n');

  // if you got any bytes other than the linefeed:
  myString = trim(myString);

  // split the string at the commas
  // and convert the sections into integers:
  int values[] = int(split(myString, ','));

  if (values.length > 0) {// v1 de 0 a 4000
int v1; int v2; int v3; int v4; int v5; int v6;

    v1 = (int) map (values[0], 0, 4000, 0, 400);
     v2 = (int) map (values[0], 0, 4000, 0, 400);
      v3 = (int) map (values[0], 0, 4000, 0, 400);
       v4 = (int) map (values[0], 0, 4000, 0, 400);
        v5 = (int) map (values[0], 0, 4000, 0, 400);
         v6 = (int) map (values[0], 0, 4000, 0, 400);
       
    println (" v1 " + v1 ); println (" v1 " + v1 ); println (" v1 " + v1 ); println (" v1 " + v1 ); print (mouseY);   print (" v1 ");   println (v1);  
}

I send data to the arduino like that <28643,32469,36099,34103,31316,30462,0,0,0,0,10,3,-3,0,0,0,0,0,0,1,1,0,1,1,1>

Thanks for helping!

Hello @bking,

You most certainly can send and receive on the same port.
This is called full-duplex.

I provided some no frills code to demonstrate.

Arduino code:

void setup()
  {
  Serial.begin(9600);
  }

void loop()
  {
  if (Serial.available() > 0) 
    {
    // read the incoming byte:
    int incomingByte = Serial.read();
    Serial.write(incomingByte);
    }
  }

Processing code:

import processing.serial.*; 
 
Serial myPort;
String inString;  // Input string from serial port
int lf = '\n';      // ASCII linefeed 
 
void setup() 
  { 
  size(400, 200); 
 
  printArray(Serial.list());
  
  myPort = new Serial(this, Serial.list()[4], 9600); 
  myPort.bufferUntil('\n'); 
  } 
 
void draw() 
  { 
  background(0);
  
  if (frameCount%30 == 0)
    myPort.write(str(frameCount) + '\n');
  
  textSize(24);
  text("Received: " + inString, 20, 100); 
  }   
 
void serialEvent(Serial p) 
  {  
  inString = p.readString();
  println(inString);
  } 

References:
https://learn.sparkfun.com/tutorials/serial-communication/
https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
https://processing.org/reference/libraries/serial/Serial_write_.html
https://docs.arduino.cc/hardware/due
https://open4tech.com/arduino-simple-serial-echo/
:)

Hi @bking, you might remember this. Data was coming from the Arduino, and we used a character to Arduino as a data request. That’s data going both ways. If you wanted to use the request as well as other data, you’d have to use logic in the Arduino to separate one from the other.

1 Like