Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен) ⇐ C#
Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен)
I am making a simple Visual Studio C# interface, in which the user will write a text, and then the text will be sent to the Arduino through an LCD 1602 module (with a header).
The Arduino code seems to be working correctly, but when addressing to the Visual Studio text, it says the access to the port is denied.
I think the main issue is with C# Code, but it could be as well the Arduino code, just in case I will post both codes and the methods of the Visual Studio which I think have the problem.
C# Code:
private void ConnectB_Click(object sender, EventArgs e) { if (!isConnected) { connectToArudino(); } else { disconnectFromArduino(); } } private void connectToArudino() { try { isConnected = true; MessageBox.Show("The value is: " + isConnected.ToString()); string selectedPort = COMComboBox.GetItemText(COMComboBox.SelectedItem); port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); port.Open(); port.Write("#STAR\n"); ConnectB.Text = "Disconnected"; Safe.Enabled = true; } catch (Exception err) { MessageBox.Show("An error has Ocurred: " + err.Message); } } private void disconnectFromArduino() { try { isConnected = false; port.Write("#STOP\n"); port.Close(); ConnectB.Text = "Connected"; Safe.Enabled = false; } catch (Exception err) { MessageBox.Show("Another Error: " + err.Message); } } Arduino Code:
#include LiquidCrystal_I2C lcd(0x27, 16, 2); String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete String commandString = ""; boolean isConnected = false; void setup() { Serial.begin(9600); lcd.begin(); lcd.backlight(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Ready to Connect"); } void loop() { if (stringComplete) { stringComplete = false; getCommand(); if (commandString.equals("STAR")) { lcd.clear(); } if (commandString.equals("STOP")) { lcd.clear(); lcd.print("Connected"); } else if (commandString.equals("TEXT")) { String text = getTextToPrint(); printText(text); } inputString = ""; } } void getCommand() { if (inputString.length() > 0) { commandString = inputString.substring(1, 5); } } String getTextToPrint() { String value = inputString.substring(5, inputString.length() - 2); return value; } void printText(String text) { lcd.clear(); lcd.setCursor(0, 0); if (text.length() < 16) { lcd.print(text); } else { lcd.print(text.substring(0, 16)); lcd.setCursor(0, 1); lcd.print(text.substring(16, 32)); } } void serialEvent() { while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; if (inChar == '\n') { stringComplete = true; } } } I am expecting that the visual code will connect correctly to the arduino, and the text written in the program is correctly displayed in the LCD module.
Источник: https://stackoverflow.com/questions/780 ... e-port-com
I am making a simple Visual Studio C# interface, in which the user will write a text, and then the text will be sent to the Arduino through an LCD 1602 module (with a header).
The Arduino code seems to be working correctly, but when addressing to the Visual Studio text, it says the access to the port is denied.
I think the main issue is with C# Code, but it could be as well the Arduino code, just in case I will post both codes and the methods of the Visual Studio which I think have the problem.
C# Code:
private void ConnectB_Click(object sender, EventArgs e) { if (!isConnected) { connectToArudino(); } else { disconnectFromArduino(); } } private void connectToArudino() { try { isConnected = true; MessageBox.Show("The value is: " + isConnected.ToString()); string selectedPort = COMComboBox.GetItemText(COMComboBox.SelectedItem); port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); port.Open(); port.Write("#STAR\n"); ConnectB.Text = "Disconnected"; Safe.Enabled = true; } catch (Exception err) { MessageBox.Show("An error has Ocurred: " + err.Message); } } private void disconnectFromArduino() { try { isConnected = false; port.Write("#STOP\n"); port.Close(); ConnectB.Text = "Connected"; Safe.Enabled = false; } catch (Exception err) { MessageBox.Show("Another Error: " + err.Message); } } Arduino Code:
#include LiquidCrystal_I2C lcd(0x27, 16, 2); String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete String commandString = ""; boolean isConnected = false; void setup() { Serial.begin(9600); lcd.begin(); lcd.backlight(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Ready to Connect"); } void loop() { if (stringComplete) { stringComplete = false; getCommand(); if (commandString.equals("STAR")) { lcd.clear(); } if (commandString.equals("STOP")) { lcd.clear(); lcd.print("Connected"); } else if (commandString.equals("TEXT")) { String text = getTextToPrint(); printText(text); } inputString = ""; } } void getCommand() { if (inputString.length() > 0) { commandString = inputString.substring(1, 5); } } String getTextToPrint() { String value = inputString.substring(5, inputString.length() - 2); return value; } void printText(String text) { lcd.clear(); lcd.setCursor(0, 0); if (text.length() < 16) { lcd.print(text); } else { lcd.print(text.substring(0, 16)); lcd.setCursor(0, 1); lcd.print(text.substring(16, 32)); } } void serialEvent() { while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; if (inChar == '\n') { stringComplete = true; } } } I am expecting that the visual code will connect correctly to the arduino, and the text written in the program is correctly displayed in the LCD module.
Источник: https://stackoverflow.com/questions/780 ... e-port-com
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен)
Anonymous » » в форуме C# - 0 Ответы
- 103 Просмотры
-
Последнее сообщение Anonymous
-