Problems with UART
I'm using two UART objects to communicate with a C328-RS camera module. For the most part it's working like it's supposed to but I'm starting to notice some weird behavior in the communication. It could quite possibly be the camera malfunctioning and not something wrong with my code but I just thought I'd get someone on here to take a look at it.
Here is the code that I am using with the camera:
Most of the time, when I first load the program onto the Javelin it will work just fine and I will get the entire picture that I am supposed to. After the program runs its first time, If I reset the Javelin, or power off and then power back on after a few seconds it will get all the way through the connection process, but I get no response from the first command I send within the getPicture() method. It seems weird that this almost always works the first time I program the Javelin but no other time. Any idea what I might be able to do to fix this from happening?
Here is the output I get on the times I get no response from the first command in getPicture()
Clearing buffers
Trying to sync with camera...
Receiving command:
170
14
13
0
0
0
Receiving command:
170
13
0
0
0
0
Succesfully Connected
Receiving command:
Thanks,
Blake Richardson
Here is the code that I am using with the camera:
import stamp.core.*; public class Camera{ static Uart send = new Uart(Uart.dirTransmit, CPU.pin0, Uart.dontInvert, Uart.speed9600, Uart.stop1); static Uart recv = new Uart(Uart.dirReceive, CPU.pin1, Uart.dontInvert, CPU.pin10, Uart.dontInvert, Uart.speed9600, Uart.stop1); //Command IDs static char SYNC = 0x0D; static char ACK = 0x0E; static char INIT = 0x01; static char SNAPSHOT = 0x05; static char GET_PICTURE = 0x04; static char DATA = 0x0A; static char POWER_OFF = 0x08; //Constant parameters static char GRAYSCALE_8BIT = 0x03; static char RES_80X60 = 0x01; static char RES_JPEG = 0x01; static char UNCOMPRESSED = 0x01; static char PREVIEW_TYPE = 0x02; static char CMD_START = 0xAA; static char NO_PARAM = 0x00; static int[noparse][[/noparse]] picture = new int[noparse][[/noparse]60*80]; public static void main(){ clearBuffer(); System.out.println("Trying to sync with camera..."); boolean connected = Camera.connect(); if(connected) { System.out.println("Succesfully Connected"); } else { System.out.println("Failed to connect"); return; } getPicture(); } //Establish a connection to the C328 camera module //@returns true if the connection was successfully made //@returns false if the connection was not made public static boolean connect() { int sync_count = 0; char[noparse][[/noparse]] recv_cmd; char recv_cmd_header = 0; char recv_cmd_id = 0; //Send SYNC command a maximum of 60 times //Check for exceptions (NACK returns) while(sync_count < 200 && !recv.byteAvailable()) { sendCommand(SYNC, NO_PARAM, NO_PARAM, NO_PARAM, NO_PARAM); sync_count++; //System.out.println("Sync count: " + sync_count + "\n"); } //Return false if nothing was received after 60 tries if(!recv.byteAvailable()) { return false; } //Clear next command from buffer //(should be ACK command) //Assume it is an ACK command getCommand(); //Clear next commnd from buffer //(should be SYNC command) //Assume it is a SYNC command getCommand(); //Send back an ACK command sendCommand(ACK, SYNC, NO_PARAM, NO_PARAM, NO_PARAM); //Make sure there was no response to the SYNC commmand if(recv.byteAvailable()) { return false; } //Return true since connection was successfully made return true; } //Pre: Successful connection must be made first with the camera //using the connect method. //Returns a Picture object. Uses a constant resolution and color type //with the C328 camera. Picture will contain raw, uncompressed //image data. //Resolution is 80x64 //256 grayscale //Raw image data //Returns null if there was an error public static void getPicture() { CPU.delay(10000); //Send INIT command //Color Type = 8-bit grayscale //Preview Resolution = 160x120 //SNAPSHOT Resolution sendCommand(INIT, NO_PARAM, GRAYSCALE_8BIT, RES_80X60, RES_JPEG); //Clear next command from buffer //(should be ACK command) //Assume it is an ACK command getCommand(); //Send GET_PICTURE command sendCommand(GET_PICTURE, PREVIEW_TYPE, NO_PARAM, NO_PARAM, NO_PARAM); //Clear next command from buffer //(should be ACK command) //Assume it is an ACK command getCommand(); //Clear next command from buffer //(should be DATA command) //Assume it is an DATA command getCommand(); //Build grayscale picture with incoming image data //Will pause waiting for the next byte if this part fails int count = 0; int size = 80*60; do { //System.out.println("Receiving byte" + count + ": " + temp); //snapshot.setGrayscale(count, (char)recv.receiveByte()); picture[noparse][[/noparse]count] = recv.receiveByte(); count++; //}while(count < size && recv.byteAvailable()); }while(recv.byteAvailable()); //}while(count < size && recv.byteAvailable()); System.out.println(count + " bytes received"); } //Sends a command of the given ID using the given parameters to the C328 private static void sendCommand(char cmdID, char param1, char param2, char param3, char param4) { send.sendByte(CMD_START); send.sendByte(cmdID); send.sendByte(param1); send.sendByte(param2); send.sendByte(param3); send.sendByte(param4); } //Clears the next received command from the buffer private static void getCommand() { //wait until byte is available //while(!recv.byteAvailable()){} System.out.println("Receiving command: "); int discard; int recv_count = 1; do { discard = recv.receiveByte(); System.out.println(discard); recv_count++; }while(recv.byteAvailable() && recv_count <= 6); System.out.println("\n\n"); } //Clear buffer private static void clearBuffer() { System.out.println("Clearing buffers"); int discard; while(recv.byteAvailable()) { discard = recv.receiveByte(); System.out.println(discard); } } }
Most of the time, when I first load the program onto the Javelin it will work just fine and I will get the entire picture that I am supposed to. After the program runs its first time, If I reset the Javelin, or power off and then power back on after a few seconds it will get all the way through the connection process, but I get no response from the first command I send within the getPicture() method. It seems weird that this almost always works the first time I program the Javelin but no other time. Any idea what I might be able to do to fix this from happening?
Here is the output I get on the times I get no response from the first command in getPicture()
Clearing buffers
Trying to sync with camera...
Receiving command:
170
14
13
0
0
0
Receiving command:
170
13
0
0
0
0
Succesfully Connected
Receiving command:
Thanks,
Blake Richardson