Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: reading stream of bytes from serial port

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Dublin
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default reading stream of bytes from serial port

    Hello.
    I'm currently working on a project where I have to connect a camera to microcontroller and send a picture to PC.
    The interfacing part is done but now I'm struggling with capturing the data on the PC side. Microcontroller sends a complete bmp file to PC in chunks: firstly header then info header and then data. And this is my problem I don't know how to capture all that and save it into bmp.

    I appreciate any help
    Regards
    Kris


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: reading stream of bytes from serial port

    Can you just append each data chunk onto the end of an array or file?

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Location
    Dublin
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: reading stream of bytes from serial port

    Hi
    Thank for the reply.

    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.

    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:
            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;
            }

    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

Similar Threads

  1. Reading an int from bytes(binary file) - HELP
    By yogiyogi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 02:03 PM
  2. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 12:46 PM
  3. List serial com ports of computer
    By mickey in forum Java Networking
    Replies: 2
    Last Post: August 6th, 2010, 01:44 AM
  4. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM
  5. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM