Active5 years, 2 months ago
I would like to use multiple (three) serial ports on an Arduino Due. Instead of polling the ports continuously, I would like to use a Serial.Event function for each of the ports. These handler functions will read out the incoming string from the port and parse the string (i.e. check for a pre-defined command and call a corresponding sub-routine).
I was wondering what is going to happen when serial data arrives at the same time at two different ports? For example, if the program is in the process of handling/parsing the first serial message, will it interrupt/abandon this process as soon as another SerialEvent on another port is triggered? Or will it complete the first SerialEvent routine before going to the second one?
hobieIam currently porting my software for the ardunio mega to the new arduino Due. Iam struggling currently with the simple thing 'serialevent', however, it wont work. I tested the blanko serialevent example. I load it to the due board, opened the console and test to write some stuff to the board. The Software Serial Library; TwoPortReceive – Two serial ports that receive data switching from one to the other one when a special character is received. MultiSerialMega - Use two of the serial ports available on the Arduino and Genuino Mega. Serial Call Response - Send multiple vairables using a call-and-response (handshaking) method.
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. Serial Event example When new serial data arrives, this sketch adds it to a String. When a newline is received, the loop prints the string and clears it. A good test for this is to try it with a GPS receiver that sends out NMEA 0183 sentences. NOTE: The serialEvent feature is not available on the Leonardo, Micro, or other ATmega32U4 based boards. Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board. Serial Event example When new serial data arrives, this sketch adds it to a String. When a newline is received, the loop prints the string and clears it.
hobie3241 gold badge4 silver badges11 bronze badges
2 Answers
The built in serialEvent() function on arduino only responds to serial port 0, called just 'Serial' in the code. To respond to messages on 'Serial1', use serialEvent1() on the Arduino Due there also exists serialEvent2() and so on.
EDIT: the serialEvent() handlers are not on interrupts; they run sequentially in-between loops. If the loop is blocking they do not get run, and they do not interrupt eachother.
BrettAMBrettAM4,0421 gold badge7 silver badges21 bronze badges
That should be fairly simple to implement.
Each serial port has TX and RX interrupts. You just need to tie into the various interrupts.
Here is an example for a mbed, but it should be similar (both use ARM NVIC interrupt controllers).
Arduino Serial Event Example
To be clear, this wouldn't involve touching the
serialEvent()
function stuff at all, but rather implementing what it does using your own code.If you set up your own interrupt handlers, you also get the benefit of being able to control interrupt priority yourself. The ATSAM3X has 16 interrupt priority levels, and a interrupt of a higher priority can pre-empt a lower-level interrupt.
I Strongly suggest you read the ATSAM3X datasheet. It describes the interrupt controller, as well as the rest of the processor peripherals.
Connor WolfConnor Wolf