Управление Arduino через локальный IP - адрес из приложения VB net.
Всем привет.
Что-то очень странное происходит в моем коде, и мне действительно нужна ваша помощь.
Итак, моя настройка-это arduino uno с экраном ethernet и приложением vb net. Что делает приложение, так это дает некоторые данные arduino в локальной сети . Это работает, но есть проблема. Иногда я получал быстрый ответ от arduino некоторых других с задержкой, которая составляет максимум около 4 секунд. Я не могу понять, почему это происходит.
Когда я пробую ту же процедуру через веб-браузер или свой мобильный телефон, она работает без каких-либо задержек.
Что я уже пробовал:
Here is my codes. arduino code : <pre lang="c#"><pre>//zoomkat 12-8-11 //simple button GET with iframe code //for use with IDE 1.0 //open serial monitor to see what the arduino receives //use the \ slash to escape the " in the html (or use ') //address will look like http://192.168.1.102:84 when submited //for use with W5100 based ethernet shields #include <SPI.h> #include <Ethernet.h> #define HC12 Serial byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address byte ip[] = { 192, 168, 1, 102 }; // ip in lan byte gateway[] = { 192, 168, 1, 1 }; // internet access via router byte subnet[] = { 255, 255, 255, 0 }; //subnet mask EthernetServer server(84); //server port String readString; ////////////////////// void setup(){ pinMode(5, OUTPUT); //pin selected to control //start Ethernet Ethernet.begin(mac, ip, gateway, gateway, subnet); server.begin(); //enable serial data print Serial.begin(9600); HC12.begin(9600); Serial.println("server LED test 1.0"); // so I can keep track of what is loaded } void loop(){ // Create a client connection EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read(); //read char by char HTTP request if (readString.length() < 200) { //store characters to string readString += c; //Serial.print(c); } //if HTTP request has ended if (c == '\n') { ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // String stringOne = readString; //Serial.println(stringOne); // replace() changes all instances of one substring with another: // first, make a copy of the original string: String stringTwo = stringOne; // then perform the replacements: stringTwo.replace("HTTP/1.1", ""); // print the original: //Serial.println("Original string: " + stringOne); // and print the modified string: //Serial.println("Modified string: " + stringTwo); // String stringOne = readString; //Serial.println(stringOne); // replace() changes all instances of one substring with another: // first, make a copy of the original string: String stringThree = stringTwo; // then perform the replacements: stringThree.replace("%20", " "); // print the original: //Serial.println("Original string: " + stringOne); // and print the modified string: //Serial.println("Modified string: " + stringTwo); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////// HC12.println(stringThree); //print to serial monitor for debuging //now output HTML data header if(readString.indexOf('?') >=0) { //don't send new page client.println("HTTP/1.1 "); client.println(); client.println(); } else { client.println("HTTP/1.1 200 OK"); //send new page client.println("Content-Type: text/html"); client.println(); client.println("<HTML>"); client.println("<HEAD>"); client.println("<TITLE>Arduino GET test page</TITLE>"); client.println("</HEAD>"); client.println("<BODY>"); client.println("<H1>Zoomkat's simple Arduino button</H1>"); client.println("<a href=\"/?on1\" target=\"inlineframe\">ON</a>"); client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">"); client.println("<IFRAME name=inlineframe style=\"display:none\" >"); client.println("</IFRAME>"); client.println("</BODY>"); client.println("</HTML>"); } delay(1); //stopping client client.stop(); ///////////////////// control arduino pin if(readString.indexOf("on1") >0)//checks for on { digitalWrite(5, HIGH); // set pin 4 high Serial.println("Led On"); } if(readString.indexOf("off") >0)//checks for off { digitalWrite(5, LOW); // set pin 4 low Serial.println("Led Off"); } //clearing string for next read readString=""; } } } } }
и код vb :
<pre lang="vb"><pre> Imports System.IO Imports System.IO.Ports Imports System.Threading Public Class Form1 Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click If SerialPort1.IsOpen Then SerialPort1.WriteLine("m") End If TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "m" WebBrowser1.Navigate(TextBox10.Text) End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "c" & Label10.Text WebBrowser1.Navigate(TextBox10.Text) ' Threading.Thread.Sleep(20) TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "a" & Label11.Text WebBrowser1.Navigate(TextBox10.Text) ' Threading.Thread.Sleep(20) TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "k" WebBrowser1.Navigate(TextBox10.Text) ' Threading.Thread.Sleep(20) End Sub Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll Label8.Text = TrackBar1.Value If SerialPort1.IsOpen Then SerialPort1.WriteLine("z" & Val(Label8.Text)) End If TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "z" & Val(Label8.Text) WebBrowser1.Navigate(TextBox10.Text) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Label5.Text = "ΕΞΕΛΙΞΗ" Label10.Text = TextBox1.Text * 3600 + TextBox2.Text * 60 TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "c" & Label10.Text WebBrowser1.Navigate(TextBox10.Text) If Label10.Text > 0 Then Timer2.Start() End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Label15.Text = "ΕΞΕΛΙΞΗ" Label11.Text = TextBox3.Text * 3600 + TextBox4.Text * 60 TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "a" & Label11.Text WebBrowser1.Navigate(TextBox10.Text) 'Label11.Text = Label11.Text + 1 If Label11.Text > 0 Then Timer3.Start() End If End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click TextBox10.Text = TextBox11.Text TextBox10.Text = TextBox10.Text & "s" & RichTextBox1.Text WebBrowser1.Navigate(TextBox10.Text) End Sub End Class
Gerry Schmitz
Слишком много кода, чтобы с ним возиться. Вам нужно "сузить кругозор".