214 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			214 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Threading;
 | |
| using System.ComponentModel;
 | |
| using System.Windows.Forms;
 | |
| using System.Drawing;
 | |
| using System.Drawing.Design;
 | |
| using System.Reflection;
 | |
| using System.Runtime;
 | |
| using System.Runtime.InteropServices;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| //for tcp,udp
 | |
| using System.Net.Sockets;
 | |
| using System.Net;
 | |
| 
 | |
| namespace arDev
 | |
| {
 | |
|     public partial class MasterK
 | |
|     {
 | |
| 
 | |
|         protected Boolean Send(byte[] packet)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 _device.DiscardInBuffer();
 | |
|                 System.Threading.Thread.Sleep(1);
 | |
|                 _device.DiscardOutBuffer();
 | |
|                 System.Threading.Thread.Sleep(1);
 | |
|                 _device.Write(packet, 0, packet.Length);
 | |
|                 char_tmo = 0;
 | |
|                 return true;
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 RaiseMessage(true, "Device Send Error : " + ex.Message);
 | |
|                 return false;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         protected Boolean Write(byte[] packet)
 | |
|         {
 | |
|             bSyncRecvOK = false;
 | |
|             errorMessage = string.Empty;
 | |
|             lastSendTime = DateTime.Now;
 | |
|             lastSendMessage = Parse_SendBuffer(packet);
 | |
|             Boolean retval = Send(packet);
 | |
|             if (!retval) RaiseMessage(true, "Tx Error : " + System.Text.Encoding.Default.GetString(packet));
 | |
|             else RaiseDataMessage(eMsgType.Tx, packet);
 | |
|             return retval;
 | |
|         }
 | |
|         public byte[] UserWrite(string command)
 | |
|         {
 | |
|             _mre.Reset();
 | |
|             var buffer = WriteSync(command);
 | |
|             _mre.Set();
 | |
|             return buffer;
 | |
|         }
 | |
| 
 | |
|         protected byte[] WriteSync(string command)
 | |
|         {
 | |
|             var bytes = System.Text.Encoding.Default.GetBytes(command);
 | |
|             if (command[0] == (char)wSTX)
 | |
|             {
 | |
|                 return WriteSync(bytes);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 List<byte> packet = new List<byte>();
 | |
|                 packet.Add(wSTX);
 | |
|                 packet.AddRange(bytes);
 | |
|                 packet.Add(wETX);
 | |
|                 return WriteSync(packet.ToArray());
 | |
|             }
 | |
|         }
 | |
|         protected int interframe_delay;
 | |
|         System.Diagnostics.Stopwatch sw_ch;
 | |
|         long char_tmo;
 | |
| 
 | |
|         string savefil = string.Empty;
 | |
| 
 | |
|         // Int64 writeCnt = 0;
 | |
|         protected byte[] WriteSync(byte[] command)
 | |
|         {
 | |
|             //writeCnt += 1;
 | |
|             //RaiseMessage(false, ">> WriteSync Enter " + writeCnt.ToString());
 | |
|             if (savefil == "")
 | |
|             {
 | |
|                 savefil = Environment.CurrentDirectory + "\rxBuffer.txt";
 | |
|             }
 | |
|             // if (!mre.WaitOne(500))
 | |
|             // {
 | |
|             //     errorMessage = "WaitOne error 500ms";
 | |
|             //     return null;
 | |
|             // }
 | |
| 
 | |
|             //// RaiseMessage(false, "write data sync");
 | |
| 
 | |
|             // mre.Reset();
 | |
| 
 | |
| 
 | |
|             //Boolean isComplete = false;
 | |
|             Boolean isTimeOut = false;
 | |
|             errorMessage = "Wait Data #2";
 | |
| 
 | |
|             LastRecvMessage.Clear();
 | |
|             List<byte> _buffer = new List<byte>();
 | |
|             wat.Restart();
 | |
| 
 | |
|             System.Threading.Thread.Sleep(interframe_delay);
 | |
| 
 | |
|             //send data            
 | |
|             if (!Write(command))
 | |
|             {
 | |
|                 errorMessage = "sync Buffer is null(1)";
 | |
|                 //mre.Set();
 | |
|                 //RaiseMessage(false, "<< WriteSync Out " + writeCnt.ToString());
 | |
|                 return null;
 | |
|             }
 | |
| 
 | |
|             bool done = false;
 | |
|             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
 | |
|             sw.Start();
 | |
|             int rcv ;
 | |
|             long tmo = 0;
 | |
|             do
 | |
|             {
 | |
|                 int buffercnt = 0;
 | |
|                 try
 | |
|                 {
 | |
|                     buffercnt = _device.BytesToRead;
 | |
|                 }
 | |
|                 catch (Exception ex)
 | |
|                 {
 | |
|                     RaiseMessage(true, ex.Message);
 | |
|                 }
 | |
| 
 | |
|                 if (buffercnt > 0)
 | |
|                 {
 | |
|                     try
 | |
|                     {
 | |
|                         rcv = _device.ReadByte();
 | |
|                         // System.IO.File.WriteAllBytes(savefil, new byte[] { (byte)rcv });
 | |
|                         RaiseDataMessage(eMsgType.RxRaw, new byte[(byte)rcv]);
 | |
| 
 | |
|                         if (rcv == 0x06 || rcv == 0x015)
 | |
|                         {
 | |
|                             _buffer.Clear();
 | |
|                             _buffer.Add((byte)rcv);
 | |
|                             //isComplete = false;
 | |
|                         }
 | |
|                         else if (rcv == 0x03) //endtext
 | |
|                         {
 | |
|                             //모두 수신한경우이다.
 | |
|                             _buffer.Add((byte)rcv);
 | |
|                             //isComplete = true;
 | |
|                             done = true;
 | |
|                             break;
 | |
|                         }
 | |
|                         else
 | |
|                         {
 | |
|                             _buffer.Add((byte)rcv);
 | |
|                         }
 | |
|                     }
 | |
|                     catch {
 | |
| 
 | |
|                     }
 | |
|                 }
 | |
|                 tmo = sw.ElapsedMilliseconds;
 | |
|             } while ((!done) && (_device.ReadTimeout > tmo));
 | |
|             sw_ch.Stop();
 | |
|             sw.Stop();
 | |
| 
 | |
|             if (tmo >= _device.ReadTimeout)
 | |
|             {
 | |
|                 errorMessage = string.Format("Writesync TimeOut({0})", _device.ReadTimeout);
 | |
|                 isTimeOut = true;
 | |
|             }
 | |
| 
 | |
|             byte[] Dummy = new byte[_buffer.Count];
 | |
|             _buffer.CopyTo(Dummy);
 | |
| 
 | |
|             // mre.Set();
 | |
|             if (isTimeOut)
 | |
|             {
 | |
|                 RaiseMessage(true, errorMessage);
 | |
|                 //RaiseMessage(false, "<< WriteSync Out " + writeCnt.ToString());
 | |
|                 return null;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 RaiseDataMessage(eMsgType.Rx, Dummy);
 | |
|                 var frame = Parse_RecvBuffer(Dummy);
 | |
|                 if (frame.Buffer == null)
 | |
|                 {
 | |
|                     errorMessage = "sync buffer is null(2)";
 | |
|                     RaiseMessage(true, errorMessage);
 | |
|                     // RaiseMessage(false, "<< WriteSync Out " + writeCnt.ToString());
 | |
|                     return null;
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     // RaiseMessage(false, "<< WriteSync Out " + writeCnt.ToString());
 | |
|                     return frame.Buffer;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| }
 | 
