Ejemplo n.º 1
0
 /* Версия функции, предназначенная в первую очередь для LabView, так как LabView медленно работает
  * со структурами C#, то сделан вариант, где все поля доп. информации передаются отдельным массивом */
 public _LTRNative.LTRERROR ProcessData(uint[] src, double[] dest, ref int size, ProcFlags flags,
                                        out FRAME_STATUS frame_status,
                                        byte[] digBits, byte[] ch, AdcRanges[] range)
 {
     uint[] info             = new uint[size];
     _LTRNative.LTRERROR err = LTR210_ProcessData(ref hnd, src, dest, ref size, flags, out frame_status, info);
     if (err == _LTRNative.LTRERROR.OK)
     {
         for (int i = 0; i < size; i++)
         {
             DATA_INFO cur_info = new DATA_INFO(info[i]);
             if (digBits.Length > i)
             {
                 digBits[i] = (byte)(cur_info.DigBitState & 1);
             }
             if (ch.Length > i)
             {
                 ch[i] = cur_info.Ch;
             }
             if (range.Length > i)
             {
                 range[i] = cur_info.Range;
             }
         }
     }
     return(err);
 }
Ejemplo n.º 2
0
 public _LTRNative.LTRERROR ProcessData(uint[] src, double[] dest, ref int size, ProcFlags flags,
                                        out FRAME_STATUS frame_status,
                                        DATA_INFO[] data_info)
 {
     /* напрямую нельзя передать массив структур. нужно либо через спец. методы маршалинга, либо через
      * эквивалентный массив 32-битных слов, как сделано тут */
     uint[] info             = new uint[size];
     _LTRNative.LTRERROR err = LTR210_ProcessData(ref hnd, src, dest, ref size, flags, out frame_status, info);
     if (err == _LTRNative.LTRERROR.OK)
     {
         for (int i = 0; i < size; i++)
         {
             if (data_info.Length > i)
             {
                 data_info[i] = new DATA_INFO(info[i]);
             }
         }
     }
     return(err);
 }
Ejemplo n.º 3
0
 public _LTRNative.LTRERROR ProcessData(uint[] src, double[] dest, ref int size, ProcFlags flags,
                                        out FRAME_STATUS frame_status)
 {
     return(LTR210_ProcessData(ref hnd, src, dest, ref size, flags, out frame_status, null));
 }
Ejemplo n.º 4
0
 static extern _LTRNative.LTRERROR LTR210_ProcessData(ref TLTR210 hnd, uint[] src,
                                                      double[] dest, ref int size, ProcFlags flags,
                                                      out FRAME_STATUS frame_status,
                                                      uint[] data_info);
Ejemplo n.º 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;
			}
		}