Pages: [1]   Go Down
Author Topic: Problems receiving data with Visual Studio 2010  (Read 993 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

I want to write a small software that sends data to the Arduino and receives data. At the moment, there are two buttons that send either a 1 or a 2, and the Arduino responses with the same number to check.

Sending the data to the Arduino is no problem at all. But receiving is. Unfortunately, I'm not sure if it's a problem with receiving data, or just with handling the data (something with threading). I hope someone could help me. I swear, I googled for hours, and found lots of solutions, but most of them caused various problems for the compiler or didn't work.

I'm sure the Arduino code is correct (the TX LED flashes when the data is sent), so here's the Visual Studio part:

Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com11"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default

        SerialPort1.Open()

    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SerialPort1.Write(1)


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        SerialPort1.Write(0)
   
    End Sub


    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim message As Integer

        message = SerialPort1.ReadLine()

        Label1.Text = message

    End Sub


End Class

Thank you!
Logged

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

Quote
Unfortunately, I'm not sure if it's a problem with receiving data, or just with handling the data (something with threading).
You haven't said WHAT the problem is. What happens? What doesn't happen? Where's the Arduino code?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, yeah I forgot.

The problem is, just nothing happens. The Label doesn't change. I didn't show the Arduino code since it is straightforward.

Code:
void setup(){
 pinMode(13,OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600);

}

void loop(){
  if (Serial.available()>0){
int message;
   
message = Serial.read()-'0';
digitalWrite(13,message);
delay(1000);
Serial.print(message);
}
   
}

The longer I think about it, I think it's just a thread-problem, although I don't get any error message, just nothing happens.
Logged

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

Quote
The longer I think about it, I think it's just a thread-problem, although I don't get any error message, just nothing happens.
So, add another label that you update when/if the SerialPort1_DataReceived function is called. At least you'll know whether there is data coming in.

Now, since the serial data is a stream of characters, I'm not sure why you define message to be an integer. You certainly can't store an array of bytes in an integer variable.
Logged

Pages: [1]   Go Up
Jump to: