Can't read data from Arduino.

Hi

I ghot a proble over here. I found a JAVA code that can read data from arduino. But can somebody show me what i need to put in arduino? Serial.write() dosen’t help at all.

Java code:

public void beginListenForData()   {
		 try {
				inStream = btSocket.getInputStream();
			} catch (IOException e) {
			}
		 
	        Thread workerThread = new Thread(new Runnable()
	        {
	            public void run()
	            {                
	               while(!Thread.currentThread().isInterrupted() && !stopWorker)
	               {
	                    try 
	                    {
	                        int bytesAvailable = inStream.available();                        
	                        if(bytesAvailable > 0)
	                        {
	                            byte[] packetBytes = new byte[bytesAvailable];
	                            inStream.read(packetBytes);
	                            for(int i=0;i<bytesAvailable;i++)
	                            {
	                                byte b = packetBytes[i];
	                                if(b == delimiter)
	                                {
	                                    byte[] encodedBytes = new byte[readBufferPosition];
	                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
	                                    final String data = new String(encodedBytes, "US-ASCII");
	                                    readBufferPosition = 0;
	                                    handler.post(new Runnable()
	                                    {
	                                        public void run()
	                                        {
	                                        	
	                                        	if(Result.getText().toString().equals("..")) {
	                                        		Result.setText(data);
	                                        	} else {
	                                        		Result.append("\n"+data);
	                                        	}
	                                        	
	                                        	/* You also can use Result.setText(data); it won't display multilines
	                                        	*/
	                                        	
	                                        }
	                                    });
	                                }
	                                else
	                                {
	                                    readBuffer[readBufferPosition++] = b;
	                                }
	                            }
	                        }
	                    } 
	                    catch (IOException ex) 
	                    {
	                        stopWorker = true;
	                    }
	               }
	            }
	        });

	        workerThread.start();
	    }

Serial.write() dosen't help at all.

Perhaps because it is outputting binary data, and you are reading ASCII data. Use Serial.print().

PaulS:

Serial.write() dosen’t help at all.

Perhaps because it is outputting binary data, and you are reading ASCII data. Use Serial.print().

Thank you, i will try it ASAP

PaulS:

Serial.write() dosen’t help at all.

Perhaps because it is outputting binary data, and you are reading ASCII data. Use Serial.print().

Hi i tried this code,

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(5, 3);
int led=8;
void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);
  bluetooth.begin(9600);
  pinMode(led, OUTPUT);   
}

void loop()
{
  bluetooth.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (bluetooth.available() > 0) {
    char inByte = bluetooth.read();
    Serial.write(inByte);
}
}

But unfortunately it, didn’t work ;(

Does it work if you get rid of the f**king bluetooth crap and run a wire?

Do you KNOW that python is listening to the right serial port?

PaulS:
Does it work if you get rid of the f**king bluetooth crap and run a wire?

Do you KNOW that python is listening to the right serial port?

No I can't if i see there is possible to do it without a wire then i want to do it. I found a great example. But without a arduino code.

http://forum.arduino.cc/index.php?topic=157621.0

I wrote a demo here to illustrate Python-Arduino communication. It shouldn't be too difficult to adapt it to Java.

While trying to figure out how to communicate with Arduino using a PC program it's a good idea to write an Arduino program that you can control from the Arduino Serial Monitor so you know the Arduino side works OK. Then write your PC program to do the same thing as you were doing in the serial monitor.

When that works you can try something more elaborate.

...R

No I can't if i see there is possible to do it without a wire then i want to do it.

Well, that's certainly clear as mud.