-
Mar 3rd, 2021, 01:28 AM
#1
Thread Starter
New Member
Fuel Consumption Meter via Serial Port
Hi,
I am trying to create a program in Visual Basic wherein the data is coming from Arduino via Serial port. A program for a fuel consumption meter (gravimetric method) that I am doing.
The idea of the program is this, when I connect to the serial port of my Arduino the serial will be immediately open. Then when I click another button, it will start a timer which will run for 5 secs. The data read at the end of 5 secs will be saved as the initial weight of the fuel and be used later on. After 5 seconds the same button will be clicked and once again run another timer but this time for 10 secs. Again, the data read at the end of time will be saved as the final weight.
The initial weight, final weight, and time (10 secs) will be used to compute the fuel consumption.
I am only able to do this, receive data from serial port and run timer. However, I don't know how to retrieve the data at the end of the time. How do I do that?
The code for my serial port dataReceived is this:
Code:
Private Sub spObj_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles spObj.DataReceived
Dim txt As String
txt = ShowData(spObj.ReadLine)
tbox_iw.Text = txt 'the data will appear in a textbox dedicated for the initial weight'
End Sub
Function ShowData(ByVal [text] As String)
If Me.tbox_iw.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ShowData)
Me.Invoke(x, New Object() {(text)})
End If
Return [text]
End Function
As of now the code for my button contains the code to run the timer.
Thank you.
-
Mar 3rd, 2021, 06:27 AM
#2
Re: Fuel Consumption Meter via Serial Port
That appears to be VB.Net code.
I started typing a response thinking it was VB 6 but of course that would be completely different here.
I've not actually used the serial port stuff in VB.Net.
For some reason there was no serial port support in VB.2003 so I had to buy a 3rd party tool to handle serial i/o back then and have used that in the rare cases I need to do serial i/o.
-
Mar 3rd, 2021, 10:21 AM
#3
Re: Fuel Consumption Meter via Serial Port
My usual boring signature: Nothing
-
Mar 3rd, 2021, 10:31 AM
#4
Re: Fuel Consumption Meter via Serial Port
What causes the Arduino to send data? What is the protocol and format of the data?
Last edited by dbasnett; Mar 3rd, 2021 at 12:40 PM.
-
Mar 3rd, 2021, 11:13 AM
#5
Re: Fuel Consumption Meter via Serial Port
I suppose the data is just one value ? store the data in a public list (of double), the first item of the list will be the initial weight and the second will be the final weight.
Code:
Dim Incoming As String = Nothing
Incoming = SerialPort_arduino.ReadLine()
Incoming = Incoming.Replace(vbCr, "").Replace(vbLf, "") 'optionnal, it depends how the arduino send the data . I usually do it with an Serial.println(value)
If Incoming <> Nothing Then
Dim valeur = CDbl(Incoming) * conversion '( if needed)
data_list.Add(valeur)
End If
Last edited by Delaney; Mar 4th, 2021 at 03:13 AM.
Reason: correcting a missundertanding comment in the code
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Mar 3rd, 2021, 07:19 PM
#6
Thread Starter
New Member
Re: Fuel Consumption Meter via Serial Port
Hi,
sorry for the confusion about the version I am using and posting my query in the wrong section.
Here's the Arduino code that I am using.
Code:
#include <SoftwareSerial.h>
SoftwareSerial ssPort(6, 7); // rx, tx
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
char *strings[5];
char *ptr = NULL;
boolean newData = false;
void setup() {
Serial.begin(9600);
ssPort.begin(9600);
}
void loop() {
recvWithEndMarker();
parseData();
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (ssPort.available() > 0 && newData == false)
{
rc = ssPort.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void parseData()
{
if (newData == true)
{
byte index = 0;
ptr = strtok(receivedChars, ",");
while (ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ",");
}
//Serial.println(index);
// the separate pieces
float number = atof(strings[2]);
Serial.println(number);
newData = false;
}
}
Thank you.
-
Mar 3rd, 2021, 09:46 PM
#7
Thread Starter
New Member
Re: Fuel Consumption Meter via Serial Port
Also, I am converting the incoming string to double. However, it experience an error if it receives a negative value. Sometimes, I also receive weird values like "0.1-0.1" or "0.00.0" something like that.
Should I fix something in the Arduino code or I just need to add something in my VB code?
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|