Do you have a question, or were you just updating us to keep the topic open?
Yes. How to convert the byte to float back in the host machine after its read by the program?
Which program would that be?
That Cpp library that I found to RW on the serial port.
SerialPort
But what does your code look like?
Arduino
void setup() {
float cd = 1.0;
float arrBuff[10];
for (int i =0; i<10;++i)
{
arrBuff[i] =cd+3.45;
cd++;
}
Serial.begin(9600);
for(int i=0; i<10; ++i)
{
Serial.println(arrBuff[i]);
}
}
void loop() {
}
And for the main CPP:
#include "includes/serial/serial.hpp"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
const char* portName = "\\\\.\\COM3";
#define MAX_DATA_LEN sizeof(float)
char incomingData[MAX_DATA_LEN];
SerialPort* arduino;
void exampleReceiveData(void)
{
float readResult = (float)arduino->readPort(incomingData, MAX_DATA_LENGTH);
printf("%.2f\n", readResult);
Sleep(1000);
}
void autoConnect()
{
while (1)
{
std::cout << "search in progress" << std::endl;
while (!arduino->isConnected())
{
Sleep(1);
std::cout << " \b\\" << std::flush;
Sleep(1);
std::cout << "\b|" << std::flush;
Sleep(1);
std::cout << "\b/" << std::flush;
Sleep(1);
std::cout << "\b-" << std::flush;
arduino = new SerialPort(portName);
}
if (arduino->isConnected())
{
std::cout << "Connectd to port: " << portName << std::endl;
}
while (arduino->isConnected())
{
exampleReceiveData();
}
}
}
int main()
{
arduino = new SerialPort(portName);
autoConnect();
return 0;
}
rest of the code is from the Github link:
https://github.com/manashmandal/SerialPort
This sends the value as text.
This reads the value as binary.
Text and binary are not the same thing. Try sending binary:
Serial.write((char *)&arrBuff[i], sizeof (float));
Does readPort() return the address of the buffer? If not you may need to fix the receiver code:
arduino->readPort(incomingData, MAX_DATA_LENGTH);
float readResult = *(float *) incomingData;
To troubleshoot, you can't look at end to end results. You have to split the problem in two so you can decide whether the problem is with your sending or receiving code. On the PC side, use a terminal emulator to capture and display the raw serial data from the Arduino.
Now, I have switched to C# since it already has a Library available (System.IO.Ports). But When I am trying to convert the Serial.println data to float on C#, it returns a blank console.
Why are you fixated on floating point numbers?
Integers are much easier to handle, and don't suffer the loss of precision multiple conversions will almost inevitably bring using floats.
facing the same with int as well.
Perhaps your underlying method is wrong then.
code from ino:
void setup() {
Serial.begin(9600);
float data = 1.23;
for (int i =0; i<10;++i)
{
float fin = (float)i/data;
Serial.println(String(fin));
}
}
one thing i need to add, the data transfers successfully only when the red button is pressed and hold for 5-6 sec and releasing.
The red button?
You pressed the red button?
Are you crazy?!?
What red button?
@Robin2 has written an excellent introduction to serial handling
You should read it
Seriously, the numbers start as integers - you may as well leave them as integers, and do the processing on the host.
Red is the reset button on my board
Please first be sure you're actually sending what you think you are sending. See reply #50.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

