Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello mates
im now making a source code of visual basic and also arduino..
and communicate with each other by serial port
i have successfully make 2 arduino code.. one is for sending data to vb.. and one is for retrieve data from vb
but the problem is when i try to combine it
and run the program.. my arduino's system hang and do nothing
so what should i do ? need some suggestion

C++
void setup()
{
  pinMode(A0 && A1 && 2 && 3, INPUT);
 pinMode(12 && 13, OUTPUT);
 Serial.begin(9600); 
 while(Serial.available() <= 0) {
  establishContact();
 }
}

void loop()
{
  if (Serial.available() > 0)
  {
    int rdval = Serial.read();
    if(rdval == '11')
    {
      digitalWrite(13, 1);
    }
    delay(100);
  }
}
void establishContact() {
  int val1 = analogRead(A0);
  else if( val1 !=0 )
  {
   Serial.println("a"); 
  }
  delay(100);
}

for vb code

VB
Private Sub ReceivedText(ByVal [text] As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(d, New Object() {[text]})
        Else
            If [text] = "a" Then
                serialport1.write("11")
            End If
        End If
End Sub
Posted
Updated 20-Sep-13 0:39am
v2
Comments
Jonathan Davies 20-Sep-13 7:09am    
You don't say what the VB is running on, but is it's Windows use PortMon http://technet.microsoft.com/en-gb/sysinternals/bb896644.aspx to see what's really going on underneath your code at the serial port. It should at least let you decide which end of the wire the problem is. With hardware like your board you need it to have a means of letting you know what's happening, or rather why it's not happening, to ensure you can get things working: output to Log file, output to a display, leds etc.
[no name] 20-Sep-13 9:33am    
This is a non trivial question that should be posted to the Arduino forum. Firstly you haven't given all your Arduino program (please don't) but without the rest of the code it really is not possible to debug your program. You have not given which library which version or which Arduino. Writing a whole bunch of code for two devices and throwing it together is doomed to fail. You don't know whether your cable works for example. Break the problem down into manageable pieces. Get your Arduino program working first using a terminal program like Putty and go from there. The establishContact() fn looks very strange.
Perhaps best to start with a working example from Arduino.cc

1 solution

Here is a brief checklist for communicating via serial lines:
- You need to make sure that each side is communicating with the same speed, number of start/stop bits, and flow control. If CTS/RTS etc. is used, the details are trickier. These details absolutely have to be setup correctly.
- If the speeds on either side change (ie. during initialization), some garbage characters are likely to show up. Your inter-device protocol should be tolerant of this and discard characters/messages that are out of context... Especially the initial messages when your program starts.
- When each side starts communicating, your code should accommodate the idea that the other side is not ready yet or that the message might get mangled or lost (see previous point). Don't just send a message and sit forever in a wait loop looking for a response.

There are a lot of other points that could be made. This is just a brief list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900