Pages: [1]   Go Down
Author Topic: VB.NET serial communication  (Read 3240 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to communicate with the Arduino board using the serialport function in VB.NET. I want to create a program that can enable the digital input pins on the arduino, so I plan on sending a 1 through 13 to the board and have code in the arduino read a value from the serial port and appropriately turn on the pin that is requested by VB.NET.

My problem is as follows:
I try to send a 1 to the arduino, and when I monitor the COM3 port the following appears,

Port opened by process "Serial Tester.vshost.exe" (PID: 5712)
01 01   ..

The "01" appears and to the right in a different column a "." appears as you see.

When I send a 50, this is what I monitor

Port opened by process "Serial Tester.vshost.exe" (PID: 5712)

 0D 0D                           ..

I see that VB.NET sends a hex value instead of an integer. But what is that "." on the right column?

Now my question is when I try to receive the value and print it using the arduino decimila, it does not print it to the serial port. The RX and TX lights flash but it does not print.

If I send a value less than 10 the value is an integer and hot a hex value, so the board should be able to use it as an int and print it out, but it does not.

I know that the board works because when I use HyperTerminal the board is able to take a key press and print the value of it.

How do I get visual basic to send a number, (1 through 13) to the arduino so it can use it?



Logged

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

Figured it out. VB was sending hex values through the serial port.

I added this line to VB; it changes the encoding on the serial port to ascii instead of hex.
SerialPort1.Encoding = Encoding.GetEncoding(28591)
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a VB .net app. too. i would be very interested in sharing experinces.

My app is mostly listening for incoming data from the Arduino board, and i did not have to change any encoding settings to read the incoming data (integer values) correctly.
Logged

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

What version of VB are you using? I have 2005.

I'm going to try to read values from arduino now that I am able to send them.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I use vb .net 2005 as well.

I found a small VB example program on the net, kind of a chat program working over the serial line, that i was able to modify to read from the arduino board.

The issues i found is with threading. I seems that the code taht makes up the eventhandler listening for incoming data and triggering the recieved event is running in one thread and the user interface components in another and you can't just do stuff across these thread boundries.

So from the recieved event handler you cant update say a textbox in a window directly. You have to use a delegate.
My understanding of delagates in .net is somewhat flakey, but i managed to make it work. I read the values of 16 potentiometers on the Arduino board, and send them as comma seperated values  followed by a CR LF, so i can read all the values as one line in the VB app. For now all i do is display them in 16 label controls in a window.

« Last Edit: December 04, 2007, 02:11:08 pm by MikMo » Logged

Amsterdam
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Xn=rX(1-X)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,
I see I am not the only one who is trying it's luck with VB. I've made a program that feeds a MySql database with sensoric input from the Arduino. The program searches the arduino and when found it keeps listining for input from the arduino. When senoric data comes in it's directly pushed to the database.

The only thing I am not familiar with is the use of char variables for string makeup. I am trying to construct  a string of different char variables but I am missing out an something I guess.

It needs to be a comma seperated string just like above here :smiley is explained.

can someone help me with this piece of code
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hey,
I see I am not the only one who is trying it's luck with VB. I've made a program that feeds a MySql database with sensoric input from the Arduino. The program searches the arduino and when found it keeps listining for input from the arduino. When senoric data comes in it's directly pushed to the database.

The only thing I am not familiar with is the use of char variables for string makeup. I am trying to construct  a string of different char variables but I am missing out an something I guess.

It needs to be a comma seperated string just like above here :smiley is explained.

can someone help me with this piece of code
Have a look at the Split()  method for parsing the string in VB.Net.  It's an easy way to do what you want.
see: http://www.homeandlearn.co.uk/net/nets7p7.html for an example
« Last Edit: December 18, 2007, 02:35:41 pm by mem » Logged

Amsterdam
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Xn=rX(1-X)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice thanks,
the link is indeed very helpful for me.
[some.thumbs.up.figure.thingie.icon]

ciao
Logged

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

I'm trying to make a GUI in VB which will display data coming from Arduino. The problem is I will have two Arduino sending the data. Plus I am using XBee to transmit the data from the two Arduino. But the XBee is not a problem coz it just works like a serial line. I am wondering if it's possible to read and distinguish the data from the two Arduino separately. Can anyone give me advice about this?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem is I will have two Arduino sending the data.
Why is this a problem? If you have two Arduinos connected to the PC, they are on different serial ports, so you need two instances of the SerialPort class - one for each port. Name the instances Fred and Sally, and you won't have any trouble keeping track of which data came from which Arduino.

The XBee is also connected to a serial port, so a third instance of the SerialPort class is required. Name it Fjrgoldowep, to keep is separate from Fred and Sally.
Logged

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

The XBee is the only one connected to the computer through one port, a USB-serial port. The XBee will receive the data from the two Arduino.,so it will be receiving two different data. Anyway, I thought of a way to distinguish where the data is coming from., that is to put an ID string along with the data being sent by the Arduino.

like

Serial.print("mote1");
Serial.print(",");
Serial.print(value);

where value is the actual data

then the string that will be received by VB would be
"mote1,value" which I have to parse to compare the ID string

for the other Arduino the string would be
"mote2,value"

Anyway, it's just a concept I devised. This is the first time I will be using XBee, Arduino and Visual Basic. I hope this will work.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I hope this will work.
It will. Keep in mind, though, that serial data transmission is slow and asynchronous. Keeping packets as small as possible is necessary. So, instead of "mote1", "1" would be better. Also, adding start and end-of-packet markers will make identification of a packet, and parsing of the packet, easier - "<1,xxx>".
Logged

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

Quote
It will. Keep in mind, though, that serial data transmission is slow and asynchronous. Keeping packets as small as possible is necessary. So, instead of "mote1", "1" would be better. Also, adding start and end-of-packet markers will make identification of a packet, and parsing of the packet, easier - "<1,xxx>".

Thanks I'll take your advice. so that's the use of the { } I saw on someone else's code.  smiley
Logged

Pages: [1]   Go Up
Jump to: