Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var intervar;
- // var tcom = null;
- $(function () {
- comlist();
- })
- //获取端口的列表
- function comlist() {
- try {
- $.get(
- "http://127.0.0.1:8333/comlist",
- function (result) {
- // console.log(result);
- if (result.ports.length > 0) {
- $(result.ports).each(function (i) {
- $("<option value='" + result.ports[i].name + "'>" + result.ports[i].name + "</option>").insertBefore($("#comli option:first"))
- })
- document.getElementById("comli").selectedIndex = 0;
- }
- }
- )
- } catch (err) {
- console.log(err);
- }
- }
- //关闭端口
- function closeCom() {
- try {
- clearInterval(intervar);
- console.log("close");
- $.get(
- "http://127.0.0.1:8333/close",
- function (result) {
- return result;
- }
- )
- } catch (err) {
- //console.log(err);
- }
- }
- //打开端口
- var CUR_SJT = "";
- function selComPort() {
- try {
- CUR_SJT = $("#character_encode").val();
- var comNo = 0;
- if ($("#comli").val() != "-1") {
- comNo = $("#comli").val();
- } else {
- alert("请选择端口");
- return;
- }
- if ($("#btn_open").val() == "Close") {
- setTimeout(() => {closeCom()}, 0);
- $("#btn_open").val("Open");
- $("#com_setting_div").show();
- return;
- } else {
- setTimeout(() => {
- var port_info={
- "port":comNo,
- "baudrate":parseInt($("#baud_rate").val()),
- "databits":parseInt($("#data_bit").val()),
- "parity":$("#parity_bit").val(),
- "stopbits":$("#stop_bit").val(),
- "expected":"\\r",
- "timeout":5
- }
- $.ajax({
- url: "http://127.0.0.1:8333/open",
- data: JSON.stringify(port_info), //方法二 测试ok
- method: "post",
- dataType: "json",
- contentType: 'application/json',
- success: function (data) {
- if(data['status']=='success'){
- //ok
- $("#btn_open").val("Close");
- $("#com_setting_div").hide();
- intervar = setInterval(function(){
- $.get(
- "http://127.0.0.1:8333/read",
- function (result) {
- if(result.stat){console.log(result)};
- if(result['data']){
- var res=$.trim(result['data'].replace("+","").replace("'",""));
- var pattern=/[1-9]\d*.\d*|0.\d*[1-9]\d*/;
- res=res.match(pattern);
- if(current_elt_id){
- //$("#"+current_elt_id).val(res);
- if(current_elt_id=="weight_before_input"){
- vm.info.weight_before=res[0];
- vm.save_info("weight_before");
- }else if(current_elt_id=="weight_after_input"){
- vm.info.weight_after=res[0];
- vm.save_info("weight_after");
- }else if(current_elt_id=="weight_input"){
- vm.info.weight=res[0];
- vm.save_info("weight");
- }
- }
- }
- }
- )
- },200);
- }else{
- alert("打开端口失败!");
- }
- },
- error: function(){
- alert("打开端口失败!");
- }
- });
- }, 0);
- }
- } catch (err) {
- console.log(err);
- }
- }
- //退出时关闭串口
- $(document).unbind('bjui.beforeCloseDialog').on('bjui.beforeCloseDialog', function (e) {
- closeCom();
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement