1. 程式人生 > >網頁控制聚英繼電器JY-DAM3200代碼

網頁控制聚英繼電器JY-DAM3200代碼

result 部分 $.ajax bsp end bin index dex 連接

第一次使用web控制一個實體硬件,需要通過網頁調用串口,摸索了一陣子,終於選定用python+php+jQuery+html實現了。

第一部分:web首頁界面 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>繼電器調控測試</title>
<script src="/public/js/jquery.js"></script>
<script>
$(document).ready(function(){
$("input").each(function(){
$(this).bind("click",function(){
com($(this)[0].checked,$(this).attr("lu"))
})
})
})

function com(s,lu){
$.ajax({
type:‘get‘,
async:true,
timeout : 20000,//20秒
url: "http://bsfx.longsunhd.com:60096/jdq.php?lu="+lu+"&s="+s,
data: "",
dataType: "html",
success: function(html){
}
});
}
</script>

</head>
<body>
<input type=checkbox on="FE 05 00 00 FF 00 98 35" off="FE 05 00 00 00 00 D9 C5" lu="0">第1路<br>
<input type=checkbox on="FE 05 00 01 FF 00 C9 F5" off="FE 05 00 01 00 00 88 05" lu="1">第2路<br>
<input type=checkbox on="FE 05 00 02 FF 00 39 F5" off="FE 05 00 02 00 00 78 05" lu="2">第3路<br>
<input type=checkbox on="FE 05 00 03 FF 00 68 35" off="FE 05 00 03 00 00 29 C5" lu="3">第4路<br>
<input type=checkbox on="FE 05 00 04 FF 00 D9 F4" off="FE 05 00 04 00 00 98 04" lu="4">第5路<br>
<input type=checkbox on="FE 05 00 05 FF 00 88 34" off="FE 05 00 05 00 00 C9 C4" lu="5">第6路<br>
<input type=checkbox on="FE 05 00 06 FF 00 78 34" off="FE 05 00 06 00 00 39 C4" lu="6">第7路<br>
<input type=checkbox on="FE 05 00 07 FF 00 29 F4" off="FE 05 00 07 00 00 68 04" lu="7">第8路<br>
<input type=checkbox on="FE 05 00 08 FF 00 19 F7" off="FE 05 00 08 00 00 58 07" lu="8">第9路<br>
<input type=checkbox on="FE 05 00 09 FF 00 48 37" off="FE 05 00 09 00 00 09 C7" lu="9">第10路<br>
<input type=checkbox on="FE 05 00 0A FF 00 B8 37" off="FE 05 00 0A 00 00 F9 C7" lu="10">第11路<br>
<input type=checkbox on="FE 05 00 0B FF 00 E9 F7" off="FE 05 00 0B 00 00 A8 07" lu="11">第12路<br>
<input type=checkbox on="FE 05 00 0C FF 00 58 36" off="FE 05 00 0C 00 00 19 C6" lu="12">第13路<br>
<input type=checkbox on="FE 05 00 0D FF 00 09 F6" off="FE 05 00 0D 00 00 48 06" lu="13">第14路<br>
<input type=checkbox on="FE 05 00 0E FF 00 F9 F6" off="FE 05 00 0E 00 00 B8 06" lu="14">第15路<br>
<input type=checkbox on="FE 05 00 0F FF 00 A8 36" off="FE 05 00 0F 00 00 E9 C6" lu="15">第16路<br>
<input type=checkbox on="FE 05 00 10 FF 00 99 F0" off="FE 05 00 10 00 00 D8 00" lu="16">第17路<br>
<input type=checkbox on="FE 05 00 11 FF 00 C8 30" off="FE 05 00 11 00 00 89 C0" lu="17">第18路<br>
<input type=checkbox on="FE 05 00 12 FF 00 38 30" off="FE 05 00 12 00 00 79 C0" lu="18">第19路<br>
<input type=checkbox on="FE 05 00 13 FF 00 69 F0" off="FE 05 00 13 00 00 28 00" lu="19">第20路<br>
<input type=checkbox on="FE 05 00 14 FF 00 D8 31" off="FE 05 00 14 00 00 99 C1" lu="20">第21路<br>
<input type=checkbox on="FE 05 00 15 FF 00 89 F1" off="FE 05 00 15 00 00 C8 01" lu="21">第22路<br>
<input type=checkbox on="FE 05 00 16 FF 00 79 F1" off="FE 05 00 16 00 00 38 01" lu="22">第23路<br>
<input type=checkbox on="FE 05 00 17 FF 00 28 31" off="FE 05 00 17 00 00 69 C1" lu="23">第24路<br>
<input type=checkbox on="FE 05 00 18 FF 00 18 32" off="FE 05 00 18 00 00 59 C2" lu="24">第25路<br>
<input type=checkbox on="FE 05 00 19 FF 00 49 F2" off="FE 05 00 19 00 00 08 02" lu="25">第26路<br>
<input type=checkbox on="FE 05 00 1A FF 00 B9 F2" off="FE 05 00 1A 00 00 F8 02" lu="26">第27路<br>
<input type=checkbox on="FE 05 00 1B FF 00 E8 32" off="FE 05 00 1B 00 00 A9 C2" lu="27">第28路<br>
<input type=checkbox on="FE 05 00 1C FF 00 59 F3" off="FE 05 00 1C 00 00 18 03" lu="28">第29路<br>
<input type=checkbox on="FE 05 00 1D FF 00 08 33" off="FE 05 00 1D 00 00 49 C3" lu="29">第30路<br>
<input type=checkbox on="FE 05 00 1E FF 00 F8 33" off="FE 05 00 1E 00 00 B9 C3" lu="30">第31路<br>
<input type=checkbox on="FE 05 00 1F FF 00 A9 F3" off="FE 05 00 1F 00 00 E8 03" lu="31">第32路<br>
<br>
<input type=checkbox on="FE 05 00 1F FF 00 A9 F3" off="FE 05 00 1F 00 00 E8 03" lu="32">全路<br>
</body>
</html>


第二部分 jdq.php
<?php
$_REQUEST[s]=($_REQUEST[s]=="true")?0:1;
$result=shell_exec("D:/Python/python.exe com.py $_REQUEST[lu] $_REQUEST[s]");

第三部分 COM.py

from sys import argv
import serial
import string
import time
import binascii
import sys
import struct
import re
f,lu,s = argv

cm=[]
cm.append([[0xFE,0x05,0x00,0x00,0xFF,0x00,0x98,0x35],[0xFE,0x05,0x00,0x00,0x00,0x00,0xD9,0xC5]])
cm.append([[0xFE,0x05,0x00,0x01,0xFF,0x00,0xC9,0xF5],[0xFE,0x05,0x00,0x01,0x00,0x00,0x88,0x05]])
cm.append([[0xFE,0x05,0x00,0x02,0xFF,0x00,0x39,0xF5],[0xFE,0x05,0x00,0x02,0x00,0x00,0x78,0x05]])
cm.append([[0xFE,0x05,0x00,0x03,0xFF,0x00,0x68,0x35],[0xFE,0x05,0x00,0x03,0x00,0x00,0x29,0xC5]])
cm.append([[0xFE,0x05,0x00,0x04,0xFF,0x00,0xD9,0xF4],[0xFE,0x05,0x00,0x04,0x00,0x00,0x98,0x04]])
cm.append([[0xFE,0x05,0x00,0x05,0xFF,0x00,0x88,0x34],[0xFE,0x05,0x00,0x05,0x00,0x00,0xC9,0xC4]])
cm.append([[0xFE,0x05,0x00,0x06,0xFF,0x00,0x78,0x34],[0xFE,0x05,0x00,0x06,0x00,0x00,0x39,0xC4]])
cm.append([[0xFE,0x05,0x00,0x07,0xFF,0x00,0x29,0xF4],[0xFE,0x05,0x00,0x07,0x00,0x00,0x68,0x04]])
cm.append([[0xFE,0x05,0x00,0x08,0xFF,0x00,0x19,0xF7],[0xFE,0x05,0x00,0x08,0x00,0x00,0x58,0x07]])
cm.append([[0xFE,0x05,0x00,0x09,0xFF,0x00,0x48,0x37],[0xFE,0x05,0x00,0x09,0x00,0x00,0x09,0xC7]])
cm.append([[0xFE,0x05,0x00,0x0A,0xFF,0x00,0xB8,0x37],[0xFE,0x05,0x00,0x0A,0x00,0x00,0xF9,0xC7]])
cm.append([[0xFE,0x05,0x00,0x0B,0xFF,0x00,0xE9,0xF7],[0xFE,0x05,0x00,0x0B,0x00,0x00,0xA8,0x07]])
cm.append([[0xFE,0x05,0x00,0x0C,0xFF,0x00,0x58,0x36],[0xFE,0x05,0x00,0x0C,0x00,0x00,0x19,0xC6]])
cm.append([[0xFE,0x05,0x00,0x0D,0xFF,0x00,0x09,0xF6],[0xFE,0x05,0x00,0x0D,0x00,0x00,0x48,0x06]])
cm.append([[0xFE,0x05,0x00,0x0E,0xFF,0x00,0xF9,0xF6],[0xFE,0x05,0x00,0x0E,0x00,0x00,0xB8,0x06]])
cm.append([[0xFE,0x05,0x00,0x0F,0xFF,0x00,0xA8,0x36],[0xFE,0x05,0x00,0x0F,0x00,0x00,0xE9,0xC6]])
cm.append([[0xFE,0x05,0x00,0x10,0xFF,0x00,0x99,0xF0],[0xFE,0x05,0x00,0x10,0x00,0x00,0xD8,0x00]])
cm.append([[0xFE,0x05,0x00,0x11,0xFF,0x00,0xC8,0x30],[0xFE,0x05,0x00,0x11,0x00,0x00,0x89,0xC0]])
cm.append([[0xFE,0x05,0x00,0x12,0xFF,0x00,0x38,0x30],[0xFE,0x05,0x00,0x12,0x00,0x00,0x79,0xC0]])
cm.append([[0xFE,0x05,0x00,0x13,0xFF,0x00,0x69,0xF0],[0xFE,0x05,0x00,0x13,0x00,0x00,0x28,0x00]])
cm.append([[0xFE,0x05,0x00,0x14,0xFF,0x00,0xD8,0x31],[0xFE,0x05,0x00,0x14,0x00,0x00,0x99,0xC1]])
cm.append([[0xFE,0x05,0x00,0x15,0xFF,0x00,0x89,0xF1],[0xFE,0x05,0x00,0x15,0x00,0x00,0xC8,0x01]])
cm.append([[0xFE,0x05,0x00,0x16,0xFF,0x00,0x79,0xF1],[0xFE,0x05,0x00,0x16,0x00,0x00,0x38,0x01]])
cm.append([[0xFE,0x05,0x00,0x17,0xFF,0x00,0x28,0x31],[0xFE,0x05,0x00,0x17,0x00,0x00,0x69,0xC1]])
cm.append([[0xFE,0x05,0x00,0x18,0xFF,0x00,0x18,0x32],[0xFE,0x05,0x00,0x18,0x00,0x00,0x59,0xC2]])
cm.append([[0xFE,0x05,0x00,0x19,0xFF,0x00,0x49,0xF2],[0xFE,0x05,0x00,0x19,0x00,0x00,0x08,0x02]])
cm.append([[0xFE,0x05,0x00,0x1A,0xFF,0x00,0xB9,0xF2],[0xFE,0x05,0x00,0x1A,0x00,0x00,0xF8,0x02]])
cm.append([[0xFE,0x05,0x00,0x1B,0xFF,0x00,0xE8,0x32],[0xFE,0x05,0x00,0x1B,0x00,0x00,0xA9,0xC2]])
cm.append([[0xFE,0x05,0x00,0x1C,0xFF,0x00,0x59,0xF3],[0xFE,0x05,0x00,0x1C,0x00,0x00,0x18,0x03]])
cm.append([[0xFE,0x05,0x00,0x1D,0xFF,0x00,0x08,0x33],[0xFE,0x05,0x00,0x1D,0x00,0x00,0x49,0xC3]])
cm.append([[0xFE,0x05,0x00,0x1E,0xFF,0x00,0xF8,0x33],[0xFE,0x05,0x00,0x1E,0x00,0x00,0xB9,0xC3]])
cm.append([[0xFE,0x05,0x00,0x1F,0xFF,0x00,0xA9,0xF3],[0xFE,0x05,0x00,0x1F,0x00,0x00,0xE8,0x03]])
cm.append([[0xFE,0x0F,0x00,0x00,0x00,0x10,0x02,0xFF,0xFF,0xA6,0x64],[0xFE,0x0F,0x00,0x00,0x00,0x10,0x02,0x00,0x00,0xA7,0xD4]])
#需要將硬件RTU格式命令(十六進制)如 FE 05 00 00 FF 00 98 35,轉成字節碼形式,即在前面加0x,變成數組形式[0xFE,0x05,0x00,0x00,0xFF,0x00,0x98,0x35]輸送給硬件串口,以實現指令過程。

try:
ser = serial.Serial(‘COM3‘, 9600) #需要連接串口線後,通過在硬件管理裏查看串口的名字(這裏是COM3)。
except:
print(‘open serial failed.‘)
exit(1)

cmd_send=cm[int(lu)][int(s)]
s=ser.write(cmd_send)
print(cmd_send)
ser.close()

網頁控制聚英繼電器JY-DAM3200代碼