MAX485 and Espruino

Posted on
  • Hello,

    I'm working on a project involving driving stepper motors via RS485.
    I've found a nice converter board:

    http://arduino-info.wikispaces.com/SoftwareSerialRS485Example

    I converted this example to JavaScript and I got it working.

    digitalWrite(B8, 0);
    Serial1.setup(9600, {tx:B6,rx:B7,bytesize:8,parity:'none',stopbits:1});
    Serial1.on('data', function (data) { print("<Serial1> "+data); });
    
    digitalWrite(B8, 1); digitalWrite(LED2, 1);
    setTimeout(function() {Serial1.print("@01STOP\r");}, 5);
    setTimeout(function() {digitalWrite(B8, 0);}, 15);
    
    

    The only problem is that the stepper motors have encoders, which will reply (very fast) when I send a serial command. This means I have to make my driver enable output LOW as fast as possible after sending my command.

    Timeouts may work, but the timings vary, as different commands require more or less time to print.
    Does anyone have any idea how to make sure the driver enable pin goes to LOW as soon as the Espruino is done sending?

    Sorry if my English is not that good, but I hope you guys can understand my question :)

    Thanks in advance,

    Eric

  • Sounds like an interesting project...

    Unfortunately Espruino doesn't have a callback when something is finished sending, but you could try:

    Setting the timeout based on the length of the string that you send:

    var s = "@01STOP\r";
    digitalWrite(B8, 1); 
    Serial1.print(s);
    setTimeout(function() {digitalWrite(B8, 0);}, s.length/0.96); // chars * 10 bits / 9600 baud
    

    Clearing the enable when you get data back

    I don't know if this'll work for you (it depends on the controller), but if it sends a reply when you send the command (and not anything else before), then you can use the reply to turn the enable off?

    Serial1.on('data', function (data) { digitalWrite(B8, 0);print("<Serial1> "+data); });
    
  • Hi Gordon,

    the first option seems to have done it, thanks! I had to add 7 ms to the timeout to get it to work though. It seems to work with all the strings I try to send.

    Thanks a lot for the quick reply!

    Eric

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

MAX485 and Espruino

Posted by Avatar for user49360 @user49360

Actions