from Mega to VB console using serial

Hello,
I’m having trouble receiving a float sent from my Mega to a Vb console I developped, using serial.

I would like to store the reading from serial in a table, with the time of reception.

In a next stage, I would like to build a graph using these data.

Unfortunately, I’m not able to store the variable properly in my table. Some variables are too short, others too long.

I think I should change the datatype of the transmission, or tell the VB console to read only groups of 4 bytes, but after days of attempts, I was wondering if someone on the forum could give me a hand ?

Here is the code I loaded on the Mega :

#include <VirtualWire.h>
void setup()
{
  //vw_set_ptt_inverted(true); // Required for DR3100
  vw_set_rx_pin(12);
  vw_setup(1200);  // Bits per sec
  pinMode(8, OUTPUT); //pour faire clignoter la led du recepteur
  Serial.begin(9600);
  Serial.println("listening");
  
  vw_rx_start();       // Start the receiver PLL running
  
}
//int i=0;
void loop()
{
  //Serial.println(i); // Affiche le compteur de secondes
  //i++;
  float valeur;
  byte taille_message = sizeof(float);
    vw_wait_rx();
    if (vw_get_message((byte *) &valeur, &taille_message)) {
 
    Serial.println(valeur, BIN); // Affiche le message
    Serial.println('\n');
  }
  //delay(1000);
  
}

And here is the code I get for reading the serial :

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel


Public Class Form1
    Dim buffer As Byte()
    Dim offset As Integer
    Dim count As Integer
    Dim returnValue As Integer
    Dim myPort As Array


    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet1.T1' table. You can move, or remove it, as needed.
        Me.T1TableAdapter.Fill(Me.Database1DataSet1.T1)



        myPort = IO.Ports.SerialPort.GetPortNames()
        portComboBox.Items.AddRange(myPort)

        writebutton.Enabled = False

    End Sub
    Private Sub initbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles initbutton.Click
        Try
            SerialPort1.PortName = portComboBox.Text
            SerialPort1.BaudRate = baudComboBox.Text
            SerialPort1.Open()
            initbutton.Enabled = False
            writebutton.Enabled = True
            closebutton.Enabled = True

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try


    End Sub



    Private Sub writebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles writebutton.Click
        SerialPort1.Write(inputTextBox.Text & vbCr) 'concatenate with \n
    End Sub

    Private Sub closebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebutton.Click
        Try

            SerialPort1.Close()
            initbutton.Enabled = True
            writebutton.Enabled = False
            closebutton.Enabled = False

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'val = CInt(text)

        If Me.outputTextBox.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
            '&= est un opérateur de concatenation qui requiert des String
        Else
            Me.outputTextBox.Text &= [text]
            Me.Database1DataSet1.T1.Rows.Add(TimeOfDay, text)
        End If
    End Sub



    Private Sub T1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1BindingNavigatorSaveItem.Click
        Me.Validate()
        Me.T1BindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Database1DataSet1)

    End Sub
End Class

I suspect that Serial.println(valeur, BIN); is not doing what you think. It will just produce the value as a human readable stream of 1 and 0 characters. It does NOT send the value in the format of the binary value in which it is coded in the Arduino memory.

My advice would be to send the data like this

Serial.print('<');
Serial.print(valeur);
Serial.print('>');

and write a VB program that expects to receive (for example) <123.98>, discards the <> and parses the numbers.

Have a look at the third example in Serial Input Basics and apply the same technique in your VB code.

…R

Hello,

I use the following instructions to send data to my VB programs.

void Transfert_VB(void) { String mot=""; String mot1="";

mot1=mot+degre; mot1=mot1+" "+ temp; mot1=mot1+" "+ hum; mot1=mot1+" "+ hzz; Serial.println(mot1); }

Hoping that this can help you...

Hoping that this can help you...

Do what? Piss away memory uselessly?

There is NO reason to be pissing away memory using the String class in that code.

void Transfert_VB(void)
{
  Serial.print(degre);
  Serial.print(" ");
  Serial.print(temp);
  Serial.print(" 
  Serial.print(hum);
  Serial.print(" ");
  Serial.println(hzz);
}

The VB app can NOT tell the difference.

Sure VB app CAN tell the difference if you know how to work with VB.

I use these few lines of code in many VB programs (with Arduino) and it work nicely. There is NOT only one way of programming!!!!

As long as it work, then fine. If your program is small; you don't care about memory.

Sure VB app CAN tell the difference if you know how to work with VB.

The VB app can NOT tell how the bytes were put into the output buffer on the Arduino end, because the bytes are shifted out of the buffer one at a time.

There is NOT only one way of programming!!!!

Some are better than others. Your snippet was the worst possible way to do it, because 1) It unnecessarily wastes resources 2) It has NO advantage over my way.

To: PaulS

If the VB app can not tell how the bytes were put, then how come I am able to get many datas from Arduino in my VB program? It's probably magic!!!!

"It unnecessarily wastes resources" : according to Arduino IDE, your way of programming is 6 bytes smaller then mine!!!

WOW a huge lost of memory space.................

I agree that your way is better but all that discussion is useless as it probably do not help "pietouf" with his problem. I will then be the first to stop it.

Regards

If the VB app can not tell how the bytes were put, then how come I am able to get many datas from Arduino in my VB program? It's probably magic!!!!

What I said was that the VB app can NOT tell if the data was output by 150 calls to Serial.print() that each wrote one byte, or by 15 statements that each wrote 10 bytes, or by one statement that output 150 bytes.

You don't seem to understand that.

"It unnecessarily wastes resources" : according to Arduino IDE, your way of programming is 6 bytes smaller then mine!!!

WOW a huge lost of memory space.................

You need to prove that. One sketch should use Strings. One should not. Then, you will see that there IS a significant difference.