firmata / arduino

Firmata firmware for Arduino
GNU Lesser General Public License v2.1
1.54k stars 515 forks source link

Firmata and RS485 #159

Open soundanalogous opened 9 years ago

soundanalogous commented 9 years ago

from @pooledge:

I am using RS485 for communication between Arduino and host computer. My RS485 is half-duplex, therefore readout/writing is possible only on demand.

Now I'd like to use one of the pins, let's say DigitalPin3 to act as transmission enabler permanently, being initialized as LOW (listen for incoming signal, like Serial.available() > 0). As soon as any readout query to host has been initiated by Firmata:

  1. DigitalPin3 must be set to HIGH to enable transmission
  2. Wait until data has been sent over Serial (flush)
  3. DigitalPin3 set back to LOW to disable transmission and enter the "listen" mode

Obviously, doing this for every "case" with Firmata.write in it inside sysexCallBack function does nothing. What would be the best way to achieve this?

pooledge commented 9 years ago

Thank you Jeff,

i guess it's not only about RS485 but basically about combining Firmata with any protocol demanding half-duplex behaviours. I've done pretty less in programming Arduino till know but would like to collaborate in any case you or somebody else can need me.

soundanalogous commented 9 years ago

Due to the coordination between the data pin and the read an write cycles this is not going to be an easy fix. However you have a couple of options:

Option A: Fork this repository and change Firmata.cpp to handle the digital pin at the lower level. You then need to change StandardFirmata to ignore digital pin 3 because it will be set to a digital output each time the board is reset (see this line). Skip that line when i == 3 and make sure the user can never set pin mode for digital pin 3 from the Firmata client.

Option B: Write a new class that extends HardwareSerial (call if "RS485Serial"). In this new class, override the read and write functions to add the digital pin toggle. You'll still need to update StandardFirmata as described above to ignore digital pin 3. You'll use the extended serial class (RS485Serial) in StandardFirmata like this:

// at the top of StandardFirmata (below the constants):
#define SerialRX  0
#define SerialTX  1
RS485Serial RS485(SerialRX, SerialTX); // could also add toggle pin as 3rd parameter
...

// in the setup function
RS485.begin(57600); // change the baud if you use something different
Firmata.begin(RS485); // instead of Firmata.begin(57600)
Davis4Create commented 8 years ago

@soundanalogous Hi Jeff, If I would like to control an Arduino UNO board in snap4arduino by a wireless module, what should I do and consider? Can you please give me an example of updating StandardFirmata to achieve this? Thanks.

soundanalogous commented 8 years ago

@Davis4Create you do not need to make any changes to StandardFirmata if you use a Serial-based wireless solution such as this: https://www.adafruit.com/products/1588. Just make sure you configure it for 57600 baud. Edit - reading more closely it sounds like it sets the proper baud rate automatically. Very cool.