Visual basic - arduino serial communication reading frequencie ?

Hello folks,

I'm currently working on a small software programmed in Visual Basic. This software will receive an temperature information (sent by a arduino nano board) to fill a Excel sheet.

The software is actually almost finished. But i have no arduino board to test it yet.

To read the temperature I have set a timer (100 ms between two tick), and each time i have a tick, i read the com port and i put the value into a variable.

The code used to do that is below :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If start = 1 Then
temp = SerialPort1.ReadLine
End If
End Sub

Do you think that strategy will work ? 100 ms seems to be a good value ?

if someone want the soft, i can share it !

Thank you in advance.
Regard,
Maxime.

why use a timer event to receive serial data? you may miss some serial data and get overruns
use a SerialPort.DataReceived event, e.g.

   ' character received from serial port
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim ReceivedData As String = SerialPort1.ReadLine()
        ....
    End Sub

a line of data is in the String receivedData which can now parse as required

Thank you for your answer Horace.

So here, with your code, each time that the PC receive data, he change the value of "ReceivedData" ?
Another question, in this case i will just send number from 10 to 100 from the board to the PC. So Can i use a integer variable instead of a string variable ?

Maxime.

yes, the String ReceivedData will contain a complete line of text terminated with the NewLine character

for example, a simple terminal emulator receiving characters from a serial line and displaying in a TextBox
in this case I use SerialPort.ReadExisting()

' terminal emulator - in Visual Studio VB

Public Class Form1
    ' set up delegate to display data received from serial port
    Private Delegate Sub SerialDelegate(ByVal Buffer As String)
    Private adre As New SerialDelegate(AddressOf DisplayData)
    ' key pressed on terminal textbox
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        SerialPort1.Write(e.KeyChar)
    End Sub
    ' character received from serial port
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim ReceivedData As String = SerialPort1.ReadExisting()
        Me.Invoke(adre, ReceivedData)
    End Sub
    ' delegate to display string received
    Private Sub DisplayData(ByVal sdata As String)
        TextBox1.AppendText(sdata)
    End Sub
    ' on form load open serial port
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub

    Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        SerialPort1.Close()
    End Sub
End Class

If your objective is to write the VB program then you will have to think about threads and buffers and synchronisation of activities if you want a fast and robust solution.

If you want a tool to look at your variables and not want to bother writing the VB program, take a look at Enhanced Serial Monitor - ESM4 new error reporting system - Interfacing w/ Software on the Computer - Arduino Forum