I have written a program in vb.net using serialport component to communicate with my arudino nano every through usb port.
The problem is that I first need to start Arduino IDE Serial Monitor and then close it before I can run my program. Else it will no send or receive anything, just empty data. (the SerialPort1_DataReceived is trigged but the message is empty).
I can close the program and start it again, it still works. But if i disconnect my arduino from USB and reconnect it, I need to do the procedure again. Start serial monitor and close it again (No data needs to be sent, just open and close it).
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
I've never tried a Nano Every but on other Arduinos like the old Nano or UNO, opening the Serial port from the computer reboots the Arduino. So if you VB program sends stuff right after opening the Serial port and expects some sort of ACK then it won't work as the Nano would still be booting and the Serial line (UART 3 on the Nano Every) will not be ready immediately.
I'd suggest you write a minimum example to test things out. vb programs opens up the Serial connection waits for a message from the arduino that you would send in the setup(). if you get it it means the Arduino rebooted.
Thanks alot for replying! okey... If I got back to basics and using this code in vb.net:
Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
SerialPort1.Close()
SerialPort1.PortName = SerialPortComboBox.Text
SerialPort1.Open()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Write("Hello")
End If
End Sub
and this code in the arduino:
int incomingByte = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
I still get the same thing. No data is sent before I have started Serial Monitor...
I still get the same thing. No data is sent before I have started Serial Monitor...
As you know something is wrong. You should not be able to open the serial port with the Arduino IDE if VB is connected. Are you trying to connect to the correct COM port?
I have two snippets here that will help you further develop your application I hope. The Arduino code is based on an example written by Nick Gammon, here is a link to his page which I highly recommend you read. https://www.gammon.com.au/serial
The VB code uses the serial data received event handler which runs on a second thread so also included is a delegate to enable cross thread communication.
Imports System.IO.Ports
Public Class Form1
Public WithEvents myPort As New SerialPort("COM7", "9600")
Private Delegate Sub mydelegate(ByVal s As String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not myPort.IsOpen Then
myPort.DtrEnable = True
myPort.Open()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If myPort.IsOpen Then
myPort.Close()
TextBox1.Text = ""
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
myPort.WriteLine("Hello World")
End Sub
Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
Dim sData As String
myPort.ReadTimeout = 20
Do While myPort.BytesToRead > 0
Try
sData = myPort.ReadLine
Me.BeginInvoke((New mydelegate(AddressOf Text_Out)), sData)
Catch ex As Exception
End Try
Loop
End Sub
Private Sub Text_Out(ByVal sData As String)
TextBox1.Text = sData
End Sub
End Class
The VB app has 3 buttons for open port close port and one to send a message to the arduino, the arduino will echo back any string that you send, on the VB app there is also a text box to display the received string.
Initially if you repeatedly open and close the serial port you will see "rdy" displayed in the text box, this indicates that the arduino has been succesfully reset
This line fixed the problem: SerialPort1.DtrEnable = True
Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
SerialPort1.Close()
SerialPort1.PortName = SerialPortComboBox.Text
SerialPort1.Open()
SerialPort1.DtrEnable = True
End Sub