Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен) ⇐ C#
Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен)
I am making a simple visual studio C# interface, which the user will write a text, and then the text the user wrote it will be send to the arudino trhough a LCD 1602 module (with a header). The arduino code seems to be working correctly, but when addressig to the visual studio text, it says the access to the port is deneid. 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()
Источник: https://stackoverflow.com/questions/780 ... e-port-com
I am making a simple visual studio C# interface, which the user will write a text, and then the text the user wrote it will be send to the arudino trhough a LCD 1602 module (with a header). The arduino code seems to be working correctly, but when addressig to the visual studio text, it says the access to the port is deneid. 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()
Источник: https://stackoverflow.com/questions/780 ... e-port-com
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение