New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add functionality to set DTR/RTS line status on serial port open #813
Conversation
fe36f54
to
6c4d32b
Compare
Translated by cs
de
en
es
fr
hu
it
ja
nb
nl
pt-br
pt-pt
ru
tr
zh-cn
zh-tw
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good from out here. Unit tests complain about some evaluate-expression
things but it ends with 483 passing ok
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code seems solid but I think there is a better approach. Some MCUs, including ESP32, require a pulse on RTS with DTR held in a particular state. To handle this, I propose a string whose characters define a sequence of states:
- d means DTR low
- D means DTR high
- r means RTS low
- R means RTS high
- nnn (a decimal number) means delay that many milliseconds
- optional spaces for readability
An ESP32 reset would thus be "Dr50R". The existing cases could be handled with, for example, "", "d", "D", "r", "R", "dR", etc. There would only need to be one variable that could handle all situations. It could even handle delay-after-open with just a number, like "1000" for wait one second before proceeding.
A little more refinement:
- empty string means "perform the controller-specific default reset sequence"
- "0" means "do nothing, overriding the controller's default sequence". It is equivalent to "delay 0 milliseconds", so does not require a special case in the parser
Here is an untested SWAG at a parser for a reset string. I haven't written any Javascript lately so I might be missing something or doing something in a dumb or obsolete way, but this is the basic idea: let delayval = null;
function doDelay() {
if (delayval !== null) {
await delay(delayval);
delayval = null;
}
}
function parseResetString(str) {
delayval = null;
if (str === "") {
controller.resetSequence();
return;
}
for (let c of str) {
switch (c) {
case ' ':
break;
case 'd':
doDelay();
await this.connection.port.set({ ...setOptions, dtr: false});
break;
case 'D':
doDelay();
await this.connection.port.set({ ...setOptions, dtr: true});
break;
case 'r':
doDelay();
await this.connection.port.set({ ...setOptions, rts: false});
break;
case 'R':
doDelay();
await this.connection.port.set({ ...setOptions, rts: true});
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (delayval == null) {
delayval = delayval * 10 + (c.charCodeAt() - '0');
}
break;
default:
// Some form of error message goes here
break;
}
}
doDelay();
} |
The error message that was generated was intentional and can be safely disregarded. |
It sounds like a good approach for handling MCU reset with RTS and DTR sequence. I have created an issue to add this to the backlog for further improvement. |
Overview
This PR adds two options that allows users to set or clear DTR and RTS line status when the serial connection is opened. The process is intended for the Arduino Due board, but may also work for certain other boards.
cncjs-pr-813.mp4
References
https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/
https://arduino.stackexchange.com/questions/38012/major-difference-between-dtr-signal-and-rts-signal
https://www.geeksforgeeks.org/difference-between-rts-cts-and-dtr-dsr-flow-control/
https://github.com/serialport/node-serialport/blob/6f453b60ac22909334dd128832d7b5eceecbbca6/examples/reset.js
https://forum.arduino.cc/t/disable-auto-reset-by-serial-connection/28248/4
https://forum.arduino.cc/t/software-disable-auto-reset-on-connection-with-the-arduino-due/369222
https://forum.arduino.cc/t/how-to-make-a-dtr-reset/385699
https://forum.arduino.cc/t/does-arduino-reset-when-first-connected-to-usb/544278/9
https://github.com/Fazecast/jSerialComm/blob/master/src/main/c/Windows/SerialPort_Windows.c#L693-L701