Пример #1
0
		public void initialize_channels(){
			if (is_initialized == 0 ) 
			{
				CHANNEL_T chan_ptr;
				channel_list[0] = new CHANNEL_T();
				chan_ptr = channel_list[0];
				chan_ptr.channel = 0x00; // unframed "channel" always slot 0
				chan_ptr.len = 0;
				chan_ptr.start = 0;
				chan_ptr.buff = new byte[CHANNEL_BUF_SIZ];
				channel_list[1] = new CHANNEL_T();
				chan_ptr = channel_list[1];
				chan_ptr.channel = 0x00; // command channel
				chan_ptr.len = 0;
				chan_ptr.start = 0;
				chan_ptr.buff = new byte[CHANNEL_BUF_SIZ];
				channel_list[2] = new CHANNEL_T();
				chan_ptr = channel_list[2];
				chan_ptr.channel = 0x99; // "0x99" debug channel
				chan_ptr.len = 0;
				chan_ptr.start = 0;
				chan_ptr.buff = new byte[CHANNEL_BUF_SIZ];
				channel_list[3] = new CHANNEL_T();
				chan_ptr = channel_list[3];
				chan_ptr.channel = 0x63; // "99" alt debug channel
				chan_ptr.len = 0;
				chan_ptr.start = 0;
				chan_ptr.buff = new byte[CHANNEL_BUF_SIZ];
				
				is_initialized = 1;
			}
		}
Пример #2
0
		public Int16 get_channel(byte channel_ID, ref CHANNEL_T return_channel){
			Int16 i;
			for (i=1;i<NUM_CHANNELS;i++){
				if (channel_ID == channel_list[i].channel){
					return_channel = channel_list[i];
					return channel_ID;
				}
			}
			return -1;
		}		
Пример #3
0
		public Int32 get_byte(out byte outbyte, CHANNEL_T channel_ptr){
			//return remaining length if success, -1 if channel already empty
			if (channel_ptr.len == 0) {
				outbyte = 0x00;
				return -1;
			} else {
				outbyte = (channel_ptr.buff)[(channel_ptr.start)];
				(channel_ptr.start)++;
				return --(channel_ptr.len);
			}
		}
Пример #4
0
		public void add_byte(byte inbyte, CHANNEL_T channel_ptr){
			UInt32 index;
			UInt16 start = (channel_ptr.start);
			if (channel_ptr.len != CHANNEL_BUF_SIZ){
				index = (UInt32) start + channel_ptr.len;
				(channel_ptr.buff)[index] = inbyte;
				(channel_ptr.len)++;
			} else {
				channel_ptr.buff[start] = inbyte;
				channel_ptr.start = (UInt16) ((start + 1) % CHANNEL_BUF_SIZ);
			}
		}
Пример #5
0
		public void read_frame(SerialPort comport,byte channel_ID,UInt16 start_byte_ms, ref UInt32 receiveBytes, byte[] receiveBuffer)
		{
			Int32 byte_idx = 0;
			Int32 tempval = 0;
			byte c,found_complete_frame=0, in_escape=0;
			UInt16 calc_crc =0x1D0F;
			UInt16 timeout = start_byte_ms;
			UInt32 max_timeout = (UInt32)start_byte_ms + (UInt32)FRAME_TIMEOUT_MSEC;
#if DEBUGPRINT
			Console.WriteLine("Read Timeout = {0} ms", timeout);
#endif
			tempval = get_channel(channel_ID, ref chan_ptr);
			get_unframed(ref unframed_ptr);

			if (tempval > 0 && chan_ptr.len > 0){
#if VERBOSE_PRINT
				int i,j;
				Console.Write("POLL buffer start = {0}, len = {1} \n",chan_ptr.start,chan_ptr.len);
				Console.Write("current buffer[20] : ");
				for (j=0; j<20; j++){
					i = ( (chan_ptr.start + j ) % CHANNEL_BUF_SIZ);
					Console.Write(" {0:X02}",chan_ptr.buff[i]);
				}
				Console.Write("\n");
#endif
				while(chan_ptr.len > 0){
					tempval = get_byte(out c,chan_ptr);
					if (tempval < 0){
						break;
					}
					if (c == START_FRAME_BYTE)
					{	
#if VERBOSE_PRINT || DEBUGPRINT
						Console.Write("     POLL_RX_BUF StartByte \n ");
#endif
						timeout = FRAME_TIMEOUT_MSEC;
						byte_idx = 0;
						am_in_frame = FRAME_STATUS.CORRECT_FRAME;
						continue;
					}
					if (am_in_frame != FRAME_STATUS.UNFRAMED) {
						
						if (c == ESCAPE_BYTE)
						{
							in_escape = 1;
							tempval = get_byte(out c,chan_ptr);
							if (tempval < 0){
								break;
							}
							switch (c)
							{
								case ESCAPED_END_FRAME_BYTE:
									c = END_FRAME_BYTE;
									break;
								case ESCAPED_START_FRAME_BYTE:
									c = START_FRAME_BYTE;
									break;
								case ESCAPED_ESCAPE_BYTE:
									c = ESCAPE_BYTE;
									break;
								default:
									break;
							}
							in_frame_buf[byte_idx++] = c;
#if VERBOSE_PRINT
							Console.Write(" {0:X02}",c);
#endif
							continue;
						}

						if (c == END_FRAME_BYTE)
						{
							am_in_frame = FRAME_STATUS.UNFRAMED;
							/*
							 * Check CRC: if good end loop.  If bad, start looking for start byte.
							 * Check Frame Status:  If zero, end loop.  If non-zero, start looking for start byte.
							 */
							 
							/*
							 * byte_idx - 3 = total frame length - channel_ID byte - crc_bytes[2]
							 * &(frame_buf[1]) is first byte after channel_ID byte
							 */
							calc_crc = calcFlirCRC16Bytes((UInt32)(byte_idx - 2), CRC_START_IDX,in_frame_buf);

							if ( (((calc_crc >> 8) &0xFF) != in_frame_buf[byte_idx - 2]) || ((calc_crc &0xFF) != in_frame_buf[byte_idx - 1]) )
							{
								Console.Write("\nFailed packet integrity check (calc) {0:X02}{1:X02} !=  (recd) {2:X02}{3:X02}\n",((calc_crc >> 8) &0xFF),(calc_crc&0xFF),in_frame_buf[byte_idx - 2],in_frame_buf[byte_idx - 1]);
								Console.Write("RAW Receive Packet: ");
								for (tempval=0;tempval<byte_idx;tempval++){
									Console.Write(" {0:X02}",in_frame_buf[tempval]);
								}
								Console.Write("\n");
								// Console.Write("     Failed time %f:\n",elapsed_sec);
								// failedcrc = 1;
								byte_idx = 0;
								
								timeout=start_byte_ms;
								continue;
							}
#if VERBOSE_PRINT
							else {
								Console.Write("\n");
							}
#endif
							found_complete_frame = 1;
							break;
						}
						
						in_frame_buf[byte_idx++] = c;
#if VERBOSE_PRINT
						Console.Write(" {0:X02}",c);
#endif
						continue;
					}
				}
			}
			
			if (found_complete_frame != 0) {
#if VERBOSE_PRINT || DEBUGPRINT
				Console.Write("Found Complete Frame in channel_buf_{0:G02}\n",chan_ptr.channel);
				Console.Write("current buffer start = {0}, len = {1} \n",chan_ptr.start,chan_ptr.len);
#endif
				extract_payload(in_frame_buf,FRAME_START_IDX, (UInt32)(byte_idx - NUM_FRAMING_BYTES), receiveBuffer, ref receiveBytes);
				return;
			} 
#if VERBOSE_PRINT
			Console.Write("Entering serial loop...\n");
#endif
			
			systimer.Restart();
			while(true)
			{
				if (byte_idx == FRAME_BUF_SIZ)
				{
					// Buffer overrun
					receiveBytes = 0;
					return;
				}

				
				if (check_timeouts(systimer.ElapsedMilliseconds, timeout, max_timeout))
				{
					if (am_in_frame == FRAME_STATUS.CORRECT_FRAME) {
						Console.Write("ReadFrameTimeout after: {0} ms\n",systimer.ElapsedMilliseconds);
						Console.Write("partial rx frame[{0}] : ",byte_idx);
						for (tempval=0; tempval<byte_idx; tempval++){
							Console.Write(" {0:X02}",in_frame_buf[tempval]);
						}
						Console.Write("\n");
					} else {
#if VERBOSE_PRINT || DEBUGPRINT
						Console.Write("Timed out after {0} ms, waited {1} ms\n",systimer.ElapsedMilliseconds,timeout);
#endif
					}
					am_in_frame = FRAME_STATUS.UNFRAMED;
					receiveBytes =  0;
					return;
				} 
				
				if ((in_escape!=0) && (am_in_frame==FRAME_STATUS.CORRECT_FRAME)){
					//carryover escape character from buffer
					tempval = ESCAPE_BYTE;
				} else {
					tempval = read_byte(comport);
				}
				
				if (tempval<0) {
//#if VERBOSE_PRINT
//					Console.Write("ReadByteTimeout: {0}\n",systimer.ElapsedMilliseconds);
//#endif
					continue; //byte read timeout.
				} else {
					c = (byte) tempval;
				}
				
				if ((c & 0xFF) == (START_FRAME_BYTE & 0xFF))
				{
#if VERBOSE_PRINT || DEBUGPRINT
					Console.Write("     StartByte time {0}:\n     ",systimer.ElapsedMilliseconds/1000.0);
#endif
					byte_idx = 0;
					timeout = (UInt16)(FRAME_TIMEOUT_MSEC + systimer.ElapsedMilliseconds);
					// frame_buf[byte_idx++] = c;
					do {
						if (check_timeouts(systimer.ElapsedMilliseconds, timeout, max_timeout)) continue;
						tempval = read_byte(comport);
					} while (tempval < 0);
					c = (byte) (tempval & 0xFF);
					
					byte needs_escape = 0;
					if (c == ESCAPE_BYTE){
						needs_escape = 1;
						do {
							if (check_timeouts(systimer.ElapsedMilliseconds, timeout, max_timeout)) continue;
							tempval = read_byte(comport);
						} while (tempval < 0);
						c = (byte) (tempval & 0xFF);
					}
					
					if (c == channel_ID) {
						//found correct frame
						am_in_frame = FRAME_STATUS.CORRECT_FRAME;
						in_frame_buf[byte_idx++] = c;
					} else {
						am_in_frame = FRAME_STATUS.OTHER_FRAME;
						other_frame_ID = c;
						tempval = get_channel(other_frame_ID, ref chan_ptr);
						if (tempval < 0){
							chan_ptr = unframed_ptr;
							am_in_frame = FRAME_STATUS.UNFRAMED;
						}
						add_byte(START_FRAME_BYTE,chan_ptr);
						if(needs_escape!=0) add_byte(ESCAPE_BYTE,chan_ptr);
						add_byte(c,chan_ptr);
					}
					continue;
				}
				
				if (am_in_frame == FRAME_STATUS.CORRECT_FRAME) {
					if ((c & 0xFF) == (ESCAPE_BYTE & 0xFF))
					{
						in_escape = 1;
						
						do
						{
							if (check_timeouts(systimer.ElapsedMilliseconds, timeout, max_timeout)) continue;
							tempval = (byte)read_byte(comport);
						} while (tempval < 0);
						c = (byte)tempval;
						switch (c){
							case ESCAPED_END_FRAME_BYTE:
								c = END_FRAME_BYTE;
								break;
							case ESCAPED_START_FRAME_BYTE:
								c = START_FRAME_BYTE;
								break;
							case ESCAPED_ESCAPE_BYTE:
								c = ESCAPE_BYTE;
								break;
							default:
								break;
						}
						in_frame_buf[byte_idx++] = c;
						in_escape = 0;
#if VERBOSE_PRINT
						Console.Write(" {0:X02}",c);
#endif
						continue;
					}

					if ((c & 0xFF) == (END_FRAME_BYTE & 0xFF))
					{
#if VERBOSE_PRINT || DEBUGPRINT
						Console.Write("\n     EndByte time {0}:\n", systimer.ElapsedMilliseconds / 1000.0);
#endif
						// frame_buf[byte_idx++] = c;

						/*
						 * Check CRC: if good end loop.  If bad, start looking for start byte.
						 * Check Frame Status:  If zero, end loop.  If non-zero, start looking for start byte.
						 */
						am_in_frame = FRAME_STATUS.UNFRAMED;
						calc_crc = calcFlirCRC16Bytes((UInt32) (byte_idx-2),(UInt16) CRC_START_IDX , in_frame_buf);

						if ( (((calc_crc >> 8) &0xFF) != in_frame_buf[byte_idx - 2]) || ((calc_crc &0xFF) != in_frame_buf[byte_idx - 1]) )
						{
							Console.Write("Failed packet Integrity check (calc){0:X02}{1:X02} != (pkt){2:X02}{3:X02}\n",((calc_crc >> 8) &0xFF),(calc_crc&0xFF),in_frame_buf[byte_idx - 2],in_frame_buf[byte_idx - 1]);
							Console.Write("byte count = {0}, crc len = {1}\n", byte_idx, byte_idx - NUM_FRAMING_BYTES);
							Console.Write("RAW Receive Packet: ");
							for (tempval=0;tempval<byte_idx;tempval++){
								Console.Write(" {0:X02}",in_frame_buf[tempval]);
							}
							Console.Write("\n");
							// Console.Write("     Failed time %f:\n",elapsed_sec);
							// failedcrc = 1;
							byte_idx = 0;
							systimer.Restart();
							timeout = start_byte_ms;
							continue;
						}
#if VERBOSE_PRINT
						else{
							Console.Write("\n");
						}
#endif
						found_complete_frame = 1;
						break;
					}

					in_frame_buf[byte_idx++] = c;
#if VERBOSE_PRINT
					Console.Write(" {0:X02}",c);
#endif
				}else if(am_in_frame == FRAME_STATUS.OTHER_FRAME) {
					if ((c & 0xFF) == (ESCAPE_BYTE & 0xFF)) {
						in_escape = 1;
						add_byte(c,chan_ptr);
						do {
							if (systimer.ElapsedMilliseconds>timeout) continue;
							tempval = read_byte(comport);
						} while (tempval < 0);
						c = (byte) tempval;
						add_byte(c,chan_ptr);
						in_escape = 0;
						
					} else if ((c & 0xFF) == (END_FRAME_BYTE & 0xFF)) {
						add_byte(c,chan_ptr);
						am_in_frame = FRAME_STATUS.UNFRAMED;
					}
#if VERBOSE_PRINT
					Console.Write("({0:G02}:{1:X02})",chan_ptr.channel,c);
#endif
					continue;
				} else { //Unframed data
#if VERBOSE_PRINT
					Console.Write("(U:{0:X02})",chan_ptr.channel,c);
#endif
					add_byte(c,unframed_ptr);
				}
			}
			
			if (found_complete_frame > 0) {
				extract_payload(in_frame_buf,FRAME_START_IDX, (UInt32)(byte_idx - NUM_FRAMING_BYTES), receiveBuffer, ref receiveBytes);
				return;
			} else {
				receiveBytes =  0;
				return;
			}
		}
Пример #6
0
		public void get_unframed(ref CHANNEL_T return_channel){
			return_channel = channel_list[0];
			return;
		}