本文介绍了这可以用Javascript完成吗?微控制器/ TCP IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我对Javascript很新,所以也许我的问题可能很愚蠢。 我正在玩微控制器和TCP / IP组件。 这意味着我可以通过TCP / IP,UDP等控制电子设备。 但是,我的问题出在客户端。 我想要一个程序,或者一个包含按钮的网页,当按下时,会向TCP / IP组件发送信号并等待 回复。 也许这看起来很奇怪,但是让我说我在端口80上有一个连接我单击网页上的一个单选按钮,新信息是发送 (与get命令一样)到TCP / IP组件。 分析新命令并发回确认。 实际上,这将是一个网页,有2个8个单选按钮, 发送8个灯的信息。 信息的方式是由Javascriot发送并不重要。 TCP / IP组件在处理完信息后回复。 让我们说有人点击lamp1。来自OFF toON 脚本应该发送一个代码按钮状态的代码 页面 " A1,B0 ,C1,E0,F0,G0H0,I0" 这意味着灯A,C为ON,其余为OFF。 TCP / IP组件检查是否可以这样做并回复: " A1,B0,C1,E0,F0,G0H0,I0" 与命令发送的命令相同脚本。 脚本通过可视化新状态做出反应。 在稍后阶段,输入应为8字节。 所以8个灯应该成为可以包含从0 到255的数字的文本框。 这只是这个问题的草稿版本,如果它好像是 Javascript不是最好的方法,我不再打扰你了 :-) 感谢您的帮助 解决方案 这取决于你所说的最佳方式。对于 程序员的最佳方式,我会说不。防止黑客打开或关闭你家中灯光的最佳方法,我会说不。最好的方式是 能够告诉你的客户哦,只需打开设备的网页 127.0.0.11然后点击红色按钮,我我想也许..取决于 你最好的意思是什么... 事情是,当涉及到沟通时,javascript可以做只有 HTML可以做什么:发出HTTP请求。所以首先你必须确保你的设备可以解析HTTP。请记住,浏览器不会发送简单和最小的HTTP,而是发送类似的内容: GET / set_buttons?A = 1& B = 0& C = 1& E = 0& F = 0& GH = 00& I = 0 HTTP / 1.1 主持人:127.0.0.11 User-Agent:Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.8.1.17)Gecko / 20061201 Firefox / 2.0.0.17(Ubuntu-feisty) 接受:text / xml,application / xml,application / xhtml + xml,text / html; q = 0.9,text / plain; q = 0.8,image /png,*/*;q=0.5 接受语言:en-us,en; q = 0.5 接受编码:gzip,deflate Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.7 保持活力:300 连接:保持活力 内容类型:application / x-www-form-urlencoded message = 1; current_style = 1 Pragma:no-cache 缓存控制:无缓存 实际上解析起来并不难。您只对GET线路感兴趣,而您的设备可以忽略其余部分。为了测试这个,我会建议先使用一个简单的HTML表单发送数据并且不要用b $ b来触摸javascript直到你可以让你的设备可靠地通话HTTP。 然后,如果你愿意,可以使用javascript发出一个AJAX请求,以避免 重新加载整个页面。 是的,没问题。这可以通过几种方式完成。您可以发送/ 请求任何资源的http请求的URL中的信息 (通常是< imgor a< script>)作为查询参数' '?q = theData''(或 not),你也可以通过XMLHttprequest发送/请求它,或者在GET / POST /的标题中输入 HEAD请求或在POST 请求的正文中。 HTH, - Jorge。 我有一个设备可以对GET 命令发送的所有内容作出反应。 所以如果浏览器发送这个: radiobuttons.html?A = on& C = on& G = on& Zend = Submit I可以过滤出按钮的名称及其状态。 所以我的所有设备都是: A =关于C =关于G = on 其余部分被控制器中的程序忽略。 但是,我的主要问题是控制器是否可以提供反馈如果 点亮戴上了,并在网页上显示。 所以,如果我发送请求打开灯A,设备检查是否 灯实际燃烧,这应该在页面上显示。 这应该是一种双向沟通。 让我们说开关关掉了灯在房间里, 控制器知道灯没亮,即使 网页重新开始在命令之后打开它,显示灯亮了,因为 变暗了。 感谢您的回复!!! Hi, I''m very new to Javascript so maybe my question could be stupid.I''m playing with microcontrollers and a TCP/IP component.This means that I can control electronics via TCP/IP, UDP and so on.But, my problem is on the client side.I want to have a program, or a web page containing buttons that, whenpressed sends a signal to the TCP/IP component and waits for aresponse back.Maybe this seems strange, but let''s say I have a connection on port 80and I click a radio button on a web page, the new information is send(like with a "get" command) to the TCP/IP component.That analyses the new command en sends a confirmation back.In real this would be a web page that has 2 times 8 radio buttons thatsends information for 8 lamps.The way the information is send by the Javascriot doesn''t matter.The TCP/IP component replies after the information is processed.Lets say someone clicks "lamp1" from "OFF" to "ON"The script should send a code that represents the state of the buttonson the page"A1,B0,C1,E0,F0,G0H0,I0"This means that lamp A, C are ON, the rest is OFF.The TCP/IP component checks if this can be done and replies:"A1,B0,C1,E0,F0,G0H0,I0"The same as the command send by the script.The script reacts by visualize the new state.In a later stage, the input should be a 8byte word.So the 8 lamps should become textboxes that can contain numbers from 0to 255.This is but a draft version of this question, if it seems thatJavascript is not the best way to do this, I won''t bother you anymore:-)Thanks for your help 解决方案It depends on what you mean by "best way". Best way for theprogrammer, I''d say no. Best way for preventing a hacker fromswitching lights in your house on or off, I''d say no. Best way to beable to tell your client "Oh, just open the device''s web page at127.0.0.11 and click the red button", I''d say maybe.. depending onwhat you mean by best...The thing is, when it comes to communication, javascript can do onlywhat HTML can do: make HTTP requests. So first you''ll have to makesure your device can parse HTTP. And remember, the browser will notsend simple-and-minimal HTTP, rather it would send something like:GET /set_buttons?A=1&B=0&C=1&E=0&F=0&GH=00&I=0 HTTP/1.1Host: 127.0.0.11User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/20061201 Firefox/2.0.0.17 (Ubuntu-feisty)Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5Accept-Language: en-us,en;q=0.5Accept-Encoding: gzip,deflateAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive: 300Connection: keep-aliveContent-Type: application/x-www-form-urlencodedmessage=1; current_style=1Pragma: no-cacheCache-Control: no-cacheWhich is actually not that hard to parse. You''d only be interested inthe GET line and your device can ignore the rest. To test this out I''dsuggest first sending the data using a simple HTML form and don''ttouch javascript until you can get your device to reliably talk HTTP.Then, if you want, use javascript to make an AJAX request to avoidhaving to reload the whole page.YES, no problem. This can be done in several ways. You can send/request the info in the url of the http request of any resource(usually an <imgor a <script>) as a query parameter ''?q=theData'' (ornot), and you can send/request it via an XMLHttprequest as well eitherin the headers of a GET/POST/HEAD request or in the body of a POSTrequest.HTH,--Jorge.I have a device that can react on everything that is send wit the GETcommand.So if the browser send this:radiobuttons.html?A=on&C=on&G=on&Zend=SubmitI can filter out the names of the buttons and their state.So all my device makes of it is:A=on C=on G=onThe rest is ignored by the program in the controller.But, my main question is if the controller can give feedback if thelights were put on, and show this on the webpage.So if I send the request to turn light A on, and the device checks ifthe light is actually burning, this should be showed on the page.This should be a two way communication.Let''s say that the lights were switched off by a switch in the room,the controller knows that the light didn''t turn on, and even if thewebpage requested to turn it on, after the command, shows the light asdimmed.Thanks already for your reply!!! 这篇关于这可以用Javascript完成吗?微控制器/ TCP IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 05:41