Pages: [1]   Go Down
Author Topic: Serial.Println(i) to Visual Basic 5.0 and MSComm  (Read 1581 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am trying to send values from arduino (to keep it simple a count-up in steps of 10) to a VB5.0 application.
In the VB display the value both in a textbox1 (multi-line) as list. In a label1 as last value.
The increasing list of values in the textbox1 including autoscroll is working fine.

However only the last value in the label-field gives me a headache.
Sometimes the complete value is displayed correctly but randomly (?) there is nothing or only the last digit or last two digits.
For example the last value in textbox is 270 and in label 70. Or textbox 380 and label 0. Or textbox 50 and label empty...
MScomm properties: inputmode:text, inputlen:0, rtreshold:1, inbuffersize:1024.

I gues it has something to do with handling of the string and/or buffer.
Already checked a lot of topics but didn't got the clue...:-(

Anyone who can give me a hint or solution to get the values correctly in VB5 ??
 
Best regards, Dries


VB5.0 code:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
MSComm1.PortOpen = True
Sleep 2000
End Sub

Private Sub MSComm1_OnComm()
Dim ReceivedData As String
ReceivedData = MSComm1.Input
Text1.Text = Text1.Text & ReceivedData
Label1.Caption = ReceivedData
End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub

Private Sub Command1_Click()
MSComm1.Output = "s"
End Sub

Private Sub Text1_Change()
Text1.SelStart = Len(Text1.Text)
Text1.SelLength = 0
End Sub

Arduino code:

// Definitie van de variabelen--------------------------------------------
int i=0;

//Setup----------------------------------------------------------------------
void setup() {
Serial.begin(9600);                 // Set up Serial library at 9600 bps
}

//Loop----------------------------------------------------------------------
void loop(){
while (Serial.available()==0){}
Serial.flush();
do {
Serial.println(i);
i=i+10;
delay(1000);
} while (Serial.available()==0);
Serial.flush();
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45960
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The MSComm1_OnComm() method is called whenever there is serial data, whether that serial data contains a carriage return/line feed or not. You are reading whatever data is available, whether it constitutes a complete packet, or not.

You are then assuming that what you read IS a complete packet, which it isn't necessarily.

You need to change what happens in the callback, to read and store the data. When the data contains a CR/LF, extract the portion up to the CR/LF and add that to the Text1 object and set it as the Label1 caption.
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I have to think about how to do this. What I understand is that every time you use ReceivedData = MSComm1.Input
that the content from the buffer is copied into the ReceivedData and the buffer is cleared ?


 


Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45960
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What I understand is that every time you use ReceivedData = MSComm1.Input
that the content from the buffer is copied into the ReceivedData and the buffer is cleared ?
True. But think about the data in terms of bytes. A message being sent consists of several characters, just like the words in this message.

Humans know how to read data (words) because we recognize spaces (and other punctuation) between words. Computers don't. They just collect data into buffers.

It is up to you to get the data out of the buffer a character at a time until you have a word, or the equivalent in your process.

What you are doing now is just like reading while I am typing. If you try to make sense of what you read, while I am in the middle of typing a word, the last (partial) word read in one glance and the first (partial) word read in the next glance won't make sense.
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help. For getting the last actual value in the label1 field I came to the following code.
The variables are global defined strings or integers in a separate module1.

Private Sub MSComm1_OnComm()
ReceivedData = MSComm1.Input
i = Len(ReceivedData)
For x = 1 To i
Pos = Mid(ReceivedData, x, 1)
If Asc(Pos) = 10 Then
Label1.Caption = Result
Result = ""
Else
Result = Result & Pos
End If
Next x
End Sub

As far as I can see during a short test it's working correctly.
Any comments, suggestions or corrections ?

BR, Dries
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
willing to share knowledge to anyone is an expression of love for all mankind
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can adapt the application that I created.
http://bsiswoyo.lecture.ub.ac.id/2012/08/arduino-tutorial-how-to-turn-onoff-8-led-using-visual-basic/
Logged

Bambang Siswoyo

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to do something similar using Visual Basic Express 2010, unfortunately lost my copies of VB6 (&5), but really struggling with the VB Express - I only want to  send a 1 or 0 back to the PC if a switch is pressed  on one of the Arduino Digital input pins. Can anyone point me in the direction of the VB Express code to use.:


Here is the Arduino Code I am using:

/**
* Read the state of the button from the Digital In pin and
* then switch off the LED when the button is pressed and communicate
* the button state over the Serial interface
*/

#define LEDPIN 13
#define INPIN 2

int state = LOW;

void setup() {
  Serial.begin(9600); // setup serial connection speed
  pinMode(LEDPIN, OUTPUT);
  pinMode(INPIN, INPUT);
}

void loop() {
  delay(10); // debounces switch
  int PinValue = digitalRead(INPIN);
  if(state != PinValue) {
    state = PinValue;
    digitalWrite(LEDPIN, PinValue); // turns the LED on or off
    Serial.println(PinValue, DEC);
  }
}
Logged

Pages: [1]   Go Up
Jump to: