Update: Arduino下自动根据代码中设置的串口波特率配置串口监视器 (#IAWTZG)
This commit is contained in:
@@ -29,6 +29,10 @@ class Serial {
|
|||||||
rts: true,
|
rts: true,
|
||||||
dtr: true
|
dtr: true
|
||||||
};
|
};
|
||||||
|
this.AVAILABEL_BAUDS = [
|
||||||
|
300, 600, 750, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600,
|
||||||
|
74880, 115200, 230400, 250000, 460800, 500000, 921600, 1000000, 2000000,
|
||||||
|
];
|
||||||
|
|
||||||
this.getSelectedPortName = function () {
|
this.getSelectedPortName = function () {
|
||||||
return Nav.getMain().getPortSelector().val();
|
return Nav.getMain().getPortSelector().val();
|
||||||
@@ -46,6 +50,10 @@ class Serial {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.portIsLegal = function (port) {
|
||||||
|
return this.portsName.includes(port);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function 重新渲染串口下拉框
|
* @function 重新渲染串口下拉框
|
||||||
* @param {array} 当前可用的所有串口
|
* @param {array} 当前可用的所有串口
|
||||||
@@ -186,6 +194,10 @@ class Serial {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baudRateIsLegal(baud) {
|
||||||
|
return Serial.AVAILABEL_BAUDS.includes(baud);
|
||||||
|
}
|
||||||
|
|
||||||
async setBaudRate(baud) {
|
async setBaudRate(baud) {
|
||||||
this.#baud_ = baud;
|
this.#baud_ = baud;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -333,18 +333,7 @@ class StatusBarSerial extends PageBase {
|
|||||||
const { id } = event.currentTarget.dataset;
|
const { id } = event.currentTarget.dataset;
|
||||||
const { data } = event.params;
|
const { data } = event.params;
|
||||||
if (id === 'baud') {
|
if (id === 'baud') {
|
||||||
const baud = data.id - 0;
|
this.setBaudRate(data.id - 0).catch(Debug.error);
|
||||||
if (!this.isOpened()) {
|
|
||||||
this.#config_.baud = baud;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.#serial_.setBaudRate(baud)
|
|
||||||
.then(() => {
|
|
||||||
this.#config_.baud = baud;
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
this.#$settingMenu_.filter('[data-id="baud"]').val(data).trigger('change');
|
|
||||||
});
|
|
||||||
} else if (id === 'send-with') {
|
} else if (id === 'send-with') {
|
||||||
if (data.id === 'no') {
|
if (data.id === 'no') {
|
||||||
this.#config_.sendWith = '';
|
this.#config_.sendWith = '';
|
||||||
@@ -521,6 +510,23 @@ class StatusBarSerial extends PageBase {
|
|||||||
this.#chart_.setStatus(isOpened);
|
this.#chart_.setStatus(isOpened);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setBaudRate(baud) {
|
||||||
|
if (!this.isOpened()) {
|
||||||
|
this.#config_.baud = baud;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.#serial_.baudRateIsLegal(baud)) {
|
||||||
|
try {
|
||||||
|
await this.#serial_.setBaudRate(baud);
|
||||||
|
this.#config_.baud = baud;
|
||||||
|
} catch (error) {
|
||||||
|
Debug.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.#$settingMenu_.filter('[data-id="baud"]').val(this.#config_.baud).trigger('change');
|
||||||
|
this.startRead();
|
||||||
|
}
|
||||||
|
|
||||||
isOpened() {
|
isOpened() {
|
||||||
return this.#opened_;
|
return this.#opened_;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,6 +138,9 @@ ArduShell.compile = (doFunc = () => {}) => {
|
|||||||
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
||||||
statusBarTerminal.setValue('');
|
statusBarTerminal.setValue('');
|
||||||
mainStatusBarTabs.changeTo("output");
|
mainStatusBarTabs.changeTo("output");
|
||||||
|
const mainWorkspace = Workspace.getMain();
|
||||||
|
const editor = mainWorkspace.getEditorsManager().getActive();
|
||||||
|
const code = editor.getCode();
|
||||||
ArduShell.compiling = true;
|
ArduShell.compiling = true;
|
||||||
ArduShell.uploading = false;
|
ArduShell.uploading = false;
|
||||||
const boardType = Boards.getSelectedBoardCommandParam();
|
const boardType = Boards.getSelectedBoardCommandParam();
|
||||||
@@ -164,44 +167,42 @@ ArduShell.compile = (doFunc = () => {}) => {
|
|||||||
$("#mixly-loader-btn").css('display', 'inline-block');
|
$("#mixly-loader-btn").css('display', 'inline-block');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setTimeout(() => {
|
statusBarTerminal.setValue(Msg.Lang['shell.compiling'] + "...\n");
|
||||||
statusBarTerminal.setValue(Msg.Lang['shell.compiling'] + "...\n");
|
|
||||||
|
|
||||||
let myLibPath = path.join(Env.boardDirPath, "/libraries/myLib/");
|
let myLibPath = path.join(Env.boardDirPath, "/libraries/myLib/");
|
||||||
if (fs_plus.isDirectorySync(myLibPath))
|
if (fs_plus.isDirectorySync(myLibPath))
|
||||||
myLibPath += '\",\"';
|
myLibPath += '\",\"';
|
||||||
else
|
else
|
||||||
myLibPath = '';
|
myLibPath = '';
|
||||||
const thirdPartyPath = path.join(Env.boardDirPath, 'libraries/ThirdParty');
|
const thirdPartyPath = path.join(Env.boardDirPath, 'libraries/ThirdParty');
|
||||||
if (fs_plus.isDirectorySync(thirdPartyPath)) {
|
if (fs_plus.isDirectorySync(thirdPartyPath)) {
|
||||||
const libList = fs.readdirSync(thirdPartyPath);
|
const libList = fs.readdirSync(thirdPartyPath);
|
||||||
for (let libName of libList) {
|
for (let libName of libList) {
|
||||||
const libPath = path.join(thirdPartyPath, libName, 'libraries');
|
const libPath = path.join(thirdPartyPath, libName, 'libraries');
|
||||||
if (!fs_plus.isDirectorySync(libPath)) continue;
|
if (!fs_plus.isDirectorySync(libPath)) continue;
|
||||||
myLibPath += libPath + ',';
|
myLibPath += libPath + ',';
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const configPath = path.join(ArduShell.shellPath, '../arduino-cli.json'),
|
}
|
||||||
defaultLibPath = path.join(ArduShell.shellPath, '../libraries'),
|
const configPath = path.join(ArduShell.shellPath, '../arduino-cli.json'),
|
||||||
buildPath = path.join(Env.clientPath, './mixlyBuild'),
|
defaultLibPath = path.join(ArduShell.shellPath, '../libraries'),
|
||||||
buildCachePath = path.join(Env.clientPath, './mixlyBuildCache'),
|
buildPath = path.join(Env.clientPath, './mixlyBuild'),
|
||||||
codePath = path.join(Env.clientPath, './testArduino/testArduino.ino');
|
buildCachePath = path.join(Env.clientPath, './mixlyBuildCache'),
|
||||||
const cmdStr = '\"'
|
codePath = path.join(Env.clientPath, './testArduino/testArduino.ino');
|
||||||
+ ArduShell.shellPath
|
const cmdStr = '\"'
|
||||||
+ '\" compile -b '
|
+ ArduShell.shellPath
|
||||||
+ boardType
|
+ '\" compile -b '
|
||||||
+ ' --config-file \"'
|
+ boardType
|
||||||
+ configPath
|
+ ' --config-file \"'
|
||||||
+ '\" --build-cache-path \"' + buildCachePath + '\" --verbose --libraries \"'
|
+ configPath
|
||||||
+ myLibPath
|
+ '\" --build-cache-path \"' + buildCachePath + '\" --verbose --libraries \"'
|
||||||
+ defaultLibPath
|
+ myLibPath
|
||||||
+ '\" --build-path \"'
|
+ defaultLibPath
|
||||||
+ buildPath
|
+ '\" --build-path \"'
|
||||||
+ '\" \"'
|
+ buildPath
|
||||||
+ codePath
|
+ '\" \"'
|
||||||
+ '\" --no-color';
|
+ codePath
|
||||||
ArduShell.runCmd(layerNum, 'compile', cmdStr, doFunc);
|
+ '\" --no-color';
|
||||||
}, 100);
|
ArduShell.runCmd(layerNum, 'compile', cmdStr, code, doFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -252,6 +253,9 @@ ArduShell.upload = (boardType, port) => {
|
|||||||
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
||||||
statusBarTerminal.setValue('');
|
statusBarTerminal.setValue('');
|
||||||
mainStatusBarTabs.changeTo('output');
|
mainStatusBarTabs.changeTo('output');
|
||||||
|
const mainWorkspace = Workspace.getMain();
|
||||||
|
const editor = mainWorkspace.getEditorsManager().getActive();
|
||||||
|
const code = editor.getCode();
|
||||||
const layerNum = layer.open({
|
const layerNum = layer.open({
|
||||||
type: 1,
|
type: 1,
|
||||||
title: Msg.Lang['shell.uploading'] + "...",
|
title: Msg.Lang['shell.uploading'] + "...",
|
||||||
@@ -327,14 +331,24 @@ ArduShell.upload = (boardType, port) => {
|
|||||||
+ codePath
|
+ codePath
|
||||||
+ '\" --no-color';
|
+ '\" --no-color';
|
||||||
}
|
}
|
||||||
ArduShell.runCmd(layerNum, 'upload', cmdStr,
|
ArduShell.runCmd(layerNum, 'upload', cmdStr, code, () => {
|
||||||
function () {
|
if (!Serial.portIsLegal(port)) {
|
||||||
mainStatusBarTabs.add('serial', port);
|
return;
|
||||||
mainStatusBarTabs.changeTo(port);
|
|
||||||
const statusBarSerial = mainStatusBarTabs.getStatusBarById(port);
|
|
||||||
statusBarSerial.open();
|
|
||||||
}
|
}
|
||||||
);
|
mainStatusBarTabs.add('serial', port);
|
||||||
|
mainStatusBarTabs.changeTo(port);
|
||||||
|
const statusBarSerial = mainStatusBarTabs.getStatusBarById(port);
|
||||||
|
statusBarSerial.open()
|
||||||
|
.then(() => {
|
||||||
|
const baudRates = code.match(/(?<=Serial.begin[\s]*\([\s]*)[0-9]*(?=[\s]*\))/g);
|
||||||
|
if (!baudRates.length) {
|
||||||
|
statusBarSerial.setBaudRate(9600);
|
||||||
|
} else {
|
||||||
|
statusBarSerial.setBaudRate(baudRates[0] - 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -466,12 +480,9 @@ ArduShell.writeLibFiles = (inPath) => {
|
|||||||
* @param cmd {String} 输入的cmd命令
|
* @param cmd {String} 输入的cmd命令
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
ArduShell.runCmd = (layerNum, type, cmd, sucFunc) => {
|
ArduShell.runCmd = (layerNum, type, cmd, code, sucFunc) => {
|
||||||
const { mainStatusBarTabs } = Mixly;
|
const { mainStatusBarTabs } = Mixly;
|
||||||
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
|
||||||
const mainWorkspace = Workspace.getMain();
|
|
||||||
const editor = mainWorkspace.getEditorsManager().getActive();
|
|
||||||
const code = editor.getCode();
|
|
||||||
const testArduinoDirPath = path.join(Env.clientPath, 'testArduino');
|
const testArduinoDirPath = path.join(Env.clientPath, 'testArduino');
|
||||||
const codePath = path.join(testArduinoDirPath, 'testArduino.ino');
|
const codePath = path.join(testArduinoDirPath, 'testArduino.ino');
|
||||||
const nowFilePath = Title.getFilePath();
|
const nowFilePath = Title.getFilePath();
|
||||||
|
|||||||
@@ -177,17 +177,19 @@ class ElectronSerial extends Serial {
|
|||||||
|
|
||||||
async setBaudRate(baud) {
|
async setBaudRate(baud) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.isOpened() || this.getBaudRate() === baud) {
|
if (!this.isOpened()
|
||||||
|
|| this.getBaudRate() === baud
|
||||||
|
|| !this.baudRateIsLegal(baud)) {
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#serialport_.update({ baudRate: baud - 0 }, (error) => {
|
this.#serialport_.update({ baudRate: baud }, (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
return;
|
||||||
super.setBaudRate(baud);
|
|
||||||
resolve();
|
|
||||||
}
|
}
|
||||||
|
super.setBaudRate(baud);
|
||||||
|
this.setDTRAndRTS(this.getDTR(), this.getRTS()).finally(resolve);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,7 +191,9 @@ class WebSerial extends Serial {
|
|||||||
|
|
||||||
async setBaudRate(baud) {
|
async setBaudRate(baud) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.isOpened() || this.getBaudRate() === baud) {
|
if (!this.isOpened()
|
||||||
|
|| this.getBaudRate() === baud
|
||||||
|
|| !this.baudRateIsLegal(baud)) {
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user