|
@@ -8,6 +8,7 @@ import SerialPort from 'serialport';
|
|
|
import { setTimeout, setInterval } from 'timers';
|
|
|
import errHandler from './error_handle';
|
|
|
var iconv = require('iconv-lite');
|
|
|
+//const serialPort = new SerialPort();
|
|
|
|
|
|
// ------ Variabls ------ //
|
|
|
const platform = process.platform;
|
|
@@ -173,7 +174,7 @@ function getpythoval() {
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
const val = results[i];
|
|
|
const mf = val.manufacturer;
|
|
|
- if (mf && mf.toLocaleLowerCase().indexOf('silicon') > -1) {
|
|
|
+ if (mf && mf.toLocaleLowerCase().indexOf('FTDI') > -1) {
|
|
|
resolve(val);
|
|
|
break;
|
|
|
}
|
|
@@ -300,7 +301,49 @@ function detect(comName, locationId = '') {
|
|
|
* @param {string} board Board's name
|
|
|
* @param {string} code Compiled source code
|
|
|
*/
|
|
|
-export function upload(port, board, code) {
|
|
|
+export function upload(code, val, socket) {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ var serialPort = await new SerialPort(val, {
|
|
|
+ baudRate: 115200,
|
|
|
+ parity: "none",
|
|
|
+ stopBits: 1,
|
|
|
+ dataBits: 8
|
|
|
+ }, function (err) {
|
|
|
+ if (err) {
|
|
|
+ err = err.message;
|
|
|
+ reject(err);
|
|
|
+ let error = errHandler(err);
|
|
|
+ socket.emit('upload', { ProgrammerStatus: 'Error', Error: error });
|
|
|
+ if (serialPort.isOpen) {
|
|
|
+ serialPort.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ code = code.replace("\r", "\\r")
|
|
|
+ code = code.replace("\n", "\\n")
|
|
|
+ code = code.replace("\"", "\\\"").replace(/^\s+|\s+$/g, '')
|
|
|
+ var complete_command = "rm /home/user/upload.py && printf \"" + code + "\" >> /home/user/upload.py && python /home/user/upload.py" + "\r\n"
|
|
|
+ serialPort.write(complete_command, function (err, msg) {
|
|
|
+ if (err) {
|
|
|
+ err = err.message;
|
|
|
+ reject(err);
|
|
|
+ let error = errHandler(err);
|
|
|
+ socket.emit('upload', { ProgrammerStatus: 'Error', Error: error });
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ resolve();
|
|
|
+ socket.emit('repl', { code: msg });
|
|
|
+ }
|
|
|
+ if (serialPort.isOpen) {
|
|
|
+ serialPort.close();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
let err = null;
|
|
|
await mkNewDir(path).catch(e => err = e);
|
|
@@ -324,6 +367,7 @@ export function upload(port, board, code) {
|
|
|
);
|
|
|
}
|
|
|
});
|
|
|
+ */
|
|
|
}
|
|
|
|
|
|
// * 上传插件收到来自前端的 python 文件,将 python 代码通过 ampy 命令上传至模块中
|