Es handelt sich um ein Arduino Uno, der über die USB Schnittstelle mit dem Computer verbunden ist.
Arduino :
Programm :Code:int serialComunication() { if (Serial.available()) { String comand = Serial.readStringUntil(';'); Serial.println(); if (comand == "Test") { Serial.println("Test:ok"); } else if (comand.startsWith("Comand:") == 1) { String t = comand.substring(7); Serial.println(t + ":ok"); return stoi(t); } else { Serial.println(comand); } } return -1; } int stoi(String str) { int temp = 0; for (int i = 0; i < str.length(); i++) { temp = temp * 10 + (str[i] - '0'); } return temp; } void setup() { Serial.begin(9600); pinMode(LED_BUILTIN, OUTPUT); PORTB |= 0x20; } // the loop function runs over and over again until power down or reset void loop() { int c = serialComunication(); ...
PS: ich habe mit Baudraten rum probiert, des wegen ist im c# Code jetzt nicht die 9600 drin.Code:internal class SerialHelper { SerialPort port; public SerialHelper(string comPort) { port = new SerialPort(comPort, 115200, Parity.None, 8, StopBits.One); } public bool Test() { if (!port.IsOpen) { port.Open(); } port.WriteLine("Test;"); String t = port.ReadLine(); while (true) { Debug.WriteLine(t); if (t.StartsWith("Test:ok")) { return true; } else if (t.StartsWith("Test")) { return false; } t = port.ReadLine(); } } public bool Comand(int code) { if (!port.IsOpen) { port.Open(); } port.WriteLine($"Comand:{code};"); String t = port.ReadLine(); while (true) { Debug.WriteLine(t); if (t.StartsWith(code+":ok")) { return true; } else if (t.StartsWith($"Comand:{code}")) { return false; } t = port.ReadLine(); } } public void Close() { if (port.IsOpen) { port.Close(); } port.Dispose(); }







Zitieren

Lesezeichen