2

I am currently struggling with getting a picture from a camera connected to the uart circuit of the raspberry pi. I am trying to do so with the aid of pi4j. Since I was able to initialize the camera, I do not think that the problem is related to the commands I am sending. However, when I try to open the generated .jpg the file is corrupt.

Has anyone an idea what I am doing wrong or has managed to get a picture form a camera connected to uart of the raspberry pi with java?

Camera: Grove - Serial Camera Kit

Datasheet: Grove - Serial Camera Datasheet PDF

Example Code: Python Code

    private void getPicture(long pictureLength) {
        try {
            byte[] receiveDataPackageCommand = { (byte) 0xaa, (byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00 };
            byte[] ackPackageEndCommand = { (byte) 0xaa, (byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0xf0,
                    (byte) 0xF0 };

            File picture = new File(getFileName());
            console.println("created file " + picture.getName());

            if (picture.createNewFile()) {
                FileOutputStream stream = new FileOutputStream(picture.getName());

                int i = 0;

                while (pictureLength > 0) {

                    receiveDataPackageCommand[4] = (byte) (i & 0xff);
                    receiveDataPackageCommand[5] = (byte) ((i >> 8) & 0xff);

                    serial.write(receiveDataPackageCommand);
                    byte[] bytes = pictureLength >= 128 ? serial.read(128) : serial.read((int) pictureLength);
                    stream.write(bytes);
                    pictureLength = pictureLength - 128;
                    i++;
                }

                stream.close();
                serial.write(ackPackageEndCommand);
                console.println("picture received and saved");
            } else {
                console.println("file already exists");
            }
        } catch (Exception ex) {
            console.println(ex);
        }
    }
  • should not you decrement remaining size by the size of received array, i.e ```pictureLength = pictureLength - bytes.length;``` ? Also there is no packets in uart. There is a srteam. Your control flow acks does not make any sense to me – Maxim Sagaydachny Apr 22 '20 at 11:21
  • @MaximSagaydachny Thank you for your answer. According to chapter 4.1 in the datasheet of the camera module I need to acknowledge every "package" I received from camera. At least this is how I interpreted the diagram. The datasheet can be found here: https://github.com/SeeedDocument/Grove-Serial_Camera_Kit/raw/master/res/cj-ov528_protocol.pdf – Jonathan James Baettig Apr 23 '20 at 06:18

0 Answers0