Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

reading stream of bytes from serial port in Java

Status
Not open for further replies.

Kaleborg

Member level 4
Joined
Nov 2, 2008
Messages
75
Helped
7
Reputation
14
Reaction score
5
Trophy points
1,288
Activity points
1,776
Hello.
In my project I have to read a stream of bytes that I'm sending from microcontroller. I'm basically sending a whole bmp file(header , infoheader and data). My problem is that I dont know how to capture those bytes. The serial communication is working and I can talk to micro and back. I just dont know how to go about capturing these bytes and saving it into a file.

Thanks in advance
 

Well you said you can talk to micro and back. How do you do it? is it through hyperterm/minicom kind of terminal emulator?

If no then you already have done the thing.
Else you have to write some code to read/write the RS232 /serial port.
There are several libs available for PC side, For microside you can get example codes too.

Now the problem is that the PROTOCOL.
You have to read the stream and according to the protocol separate out the data store it in you file.

Well it is easy to say ( like I am Doing:twisted: ) than doing.

Hope this helps.
 

"reading stream of bytes from serial port in Java"
U can do this simply by "javax.comm.*" i.e javacomm package by java
for Example
"
package rabin;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM5")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}
"
 

Hi
Thank for the reply.
@ rabin2801
Thanks but this is fine I have this part working

I am using java.comms package to communicate with microcontroller.
Yes my problem is with protocol.
I can read a single byte and send a byte but i have problems with reading loads of byte being sent at once. The data that are sent from microcontroller are image data that come from camera i connected to microcontroller so there is loads of data.

Code:
while(true){
            System.out.println("Enter command");
            while(in.hasNextInt()){
                i = in.nextInt();
                byte b = (byte)i;
                a.UARTputc(b);          //send command

                byte s = a.UARTgetb();  //check if something came back
                System.out.println(s);    //for debug
                if(s == 119){           //start of picture ascci for 'w'
                    do{
                        temp = a.UARTgetb();
                        pic[count] = temp;
                        count++;
                        System.out.println(count); //for debug
                    }while(temp != 10);//ascii for '\n'

this part i have now does not even get into the do-while loop

This is for reading the port:
Code:
        case SerialPortEvent.DATA_AVAILABLE:
            readBuffer = new byte[8];

                try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                }
                int byteCount = 0;
                for (int i = 0; i < currentWrite.length; i++) {
                    if (currentWrite[i] == readBuffer[i]) {
                        byteCount++;
                    }
                }
                if (byteCount != currentWrite.length) {
                    dataIn = true;
                }
            } catch (IOException e) {System.out.println(e);}
            break;
        }

Code:
public void UARTputc(byte b) throws Exception{
        byte[] data = {b};

        sPort.write(data);
        utils.pause(100);
    }

    public byte UARTgetb(){
        byte t=0;
        byte b[] = sPort.read();
        t = b[0];

        return t;
    }

Any advice is appreciated

Regards
 

ok
here is my code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package rabin;

/**
*
* @author Rabin
*/
import java.io.*;
import java.util.*;
import javax.comm.*;
public class rabin implements SerialPortEventListener
{
static CommPortIdentifier portId;
static Enumeration portList;
static char mn=0x0d;

static OutputStream outputStream;
static InputStream inputStream;
static SerialPort serialPort;
Thread readThread;
public rabin()
{
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {System.out.println("100000e");}

serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println("100000f");}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println("hello");}
}
public rabin(String arg1,String arg2,String arg3) {
portList = CommPortIdentifier.getPortIdentifiers();
String ms[]={
"AT+CFUN="+arg2+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn


};

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(arg1)) {
// if (portId.getName().equals("/dev/term/a")) {
new rabin();
try
{
for(int i=0;i<ms.length;i++)
{

new hi(ms,inputStream,outputStream);
Thread.sleep(50);
}
}catch(Exception e ){
}


}
}
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package rabin;

/**
*
* @author Rabin
*/
import java.io.*;
import java.util.*;
import javax.comm.*;

public class hi implements Runnable{
static CommPortIdentifier portId;
static char mn=0x0d;
static String messageString1 = null;
static Enumeration portList;
static OutputStream outputStream;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;



public hi(String messageString2,InputStream inputStream,OutputStream outputStream) {
this.outputStream=outputStream;
this.inputStream=inputStream;
this.messageString1=messageString2;


try
{
outputStream.write(messageString1.getBytes());
System.out.println("your sytem ok");
}catch(Exception e){

}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);//
} catch (InterruptedException e) {System.out.println(e);}
}


}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top