I am using an Arduino Uno and a couple of atmosferic sensors. Using DHT11 for humidity and BMP085 for pressure and temperature readings. My goal is to pass this information to a Visual basic program using serial port communication. To do this I concantenate the sensor readings into one string with the format:
"TXXXPYYYYYYHZZZZ"
where X will be the temperature, Y the pressure reading and Z humidity. The letters T,P and H are to facilitate getting the values from string in the visual basic with substring function.
THE PROBLEM:
My problem is that the string arrives fragmented at visual basic. For example I created a msgbox to prompt the message that arduino sent to me and instead of getting one msgbox with the entire message I get 2 or more msgbox with the fragmented message. BUT in the arduino serial monitor the message arrives entirely as one.
WHAT HAVE I TRIED SO FAR:
Reducing baudrate
Baudrate is the same in Vb as in arduino
Reducing the size of the message to only temperature(4chars) still it breaks
Using delays between serial.print
The code on arduino is below:
void loop() {
lcd.clear();
// put your main code here, to run repeatedly:
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
DHT.read11(dht_dpin);
lcd.home();
lcd.setCursor(1, 1);
lcd.print("T=");
lcd.print(temperature, DEC);
lcd.print("C");
lcd.print(" ");
lcd.print("H=");
lcd.print(DHT.humidity);
lcd.print("%");
lcd.setCursor(1, 0);
lcd.print("P=");
lcd.print(pressure, DEC);
lcd.print("Pa");
temperatura=String(temperature,DEC);
pressao=String(pressure,DEC);
humidade=String(DHT.humidity);
Serial.print("T"+temperatura+"P"+pressao+"H"+humidade);
delay(10000);
}
Serial.print("T");
Serial.print(temperature);
Serial.print("P");
Serial.print(pressure);
Serial.print("H");
Serial.println("humidity"); // note println() on this final line
It would probably make the PC side easier if you separate the variables with a comma.
"TXXX,PYYYYYY,HZZZZ"
and if they are always in the same order you won't need the T,P or H
If you send with println at the end and read with readln, it will probably OK.
There can also be a problem that if you send more than you read the buffer gets full. then you must empyt the buffer.
i dont normally do this but.. bam go ahead and keep this party going..
u would have to set appropriate variables but u can see whats going on???
I know there is room for improvement in my code i just seen u are having the same issues i was having a day or two ago... figured i would help out.. XD
suggestions on how to make it better??? please..
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
If SerialPort1.BytesToRead Then
Dim x As Integer = 0
Do
If SerialPort1.BytesToRead = False Then Exit Try
Incoming = SerialPort1.ReadLine
If Incoming Is Nothing Then
Exit Do
Else
addtostring(Incoming)
End If
x = x + 1
Loop
End If
Catch ex As TimeoutException
returnStr = "Error: Serial Port read timed out."
Finally
receivedtext(returnStr)
End Try
End Sub
Private Sub receivedtext(ByVal [text] As String)
If Me.outputBox.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf receivedtext)
Me.Invoke(x, New Object() {(text)})
Else
Me.OutputBox.Text = [text]
'(OutputBox.Lines.Count, OutputBox.Lines.Count)
Me.OutputBox.SelectionStart = OutputBox.Text.Length + 1
Me.outputBox.ScrollToCaret()
End If
End Sub
Private Sub addtostring(ByVal [text] As String)
If Me.OutputBox.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf addtostring)
Me.Invoke(x, New Object() {(text)})
Else
If InStr([text], vbCr, CompareMethod.Text) > 0 Then
If InStr([text], "/", CompareMethod.Text) > 0 Then
returnStr &= [text].Replace(vbCr, "").Replace("/", "") & ControlChars.NewLine
Else
returnStr &= [text].Replace(vbCr, "")
End If
Else
If InStr([text], "/", CompareMethod.Text) > 0 Then
returnStr &= [text].Replace("/", "") & ControlChars.NewLine
Else
returnStr &= [text]
End If
End If
End If
' End If
End Sub
I will predict your next issue will arise when u try and set the variable after your markers in the string on the arduino side.. :-/ lol jk im stuck there myself i cant get the next bye of data after the marker is found in the string. please enlighten me if u know of a serial.readnextbyte type of command..