"refactor:Switch-serial-comm-to-polling"
This commit is contained in:
@@ -207,7 +207,7 @@ namespace Project
|
|||||||
//연결은 되었으나 통신이 지난지 10초가 지났다면 자동종료한다
|
//연결은 되었으나 통신이 지난지 10초가 지났다면 자동종료한다
|
||||||
var tsRecv = VAR.TIME.RUN(recvtime);
|
var tsRecv = VAR.TIME.RUN(recvtime);
|
||||||
var tsConn = VAR.TIME.RUN(conntry);
|
var tsConn = VAR.TIME.RUN(conntry);
|
||||||
if (tsRecv.TotalSeconds > 10 && tsConn.TotalSeconds > 5)
|
if (tsRecv.TotalSeconds > 30 && tsConn.TotalSeconds > 5)
|
||||||
{
|
{
|
||||||
this.BeginInvoke(new Action(() =>
|
this.BeginInvoke(new Action(() =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -267,8 +267,12 @@ namespace arDev
|
|||||||
nDataTemp = Convert.ToByte(rcvdNow.Substring(idx, 2), 16);
|
nDataTemp = Convert.ToByte(rcvdNow.Substring(idx, 2), 16);
|
||||||
signal1.SetValue(nDataTemp); idx += 2;
|
signal1.SetValue(nDataTemp); idx += 2;
|
||||||
|
|
||||||
nDataTemp = Convert.ToByte(rcvdNow.Substring(idx, 2), 16);
|
if(idx < rcvdNow.Length-2)
|
||||||
signal2.SetValue(nDataTemp);
|
{
|
||||||
|
nDataTemp = Convert.ToByte(rcvdNow.Substring(idx, 2), 16);
|
||||||
|
signal2.SetValue(nDataTemp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DataReceive?.Invoke(this, new DataEventArgs(DataType.STS));
|
DataReceive?.Invoke(this, new DataEventArgs(DataType.STS));
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,12 @@ namespace arDev
|
|||||||
public string WriteErrorMessage = string.Empty;
|
public string WriteErrorMessage = string.Empty;
|
||||||
public int WaitTimeout { get; set; } = 1000;
|
public int WaitTimeout { get; set; } = 1000;
|
||||||
public int MinRecvLength { get; set; } = 1;
|
public int MinRecvLength { get; set; } = 1;
|
||||||
|
|
||||||
|
// Polling Thread related
|
||||||
|
protected Thread _recvThread;
|
||||||
|
protected volatile bool _isReading = false;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 포트이름
|
/// 포트이름
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -103,7 +109,7 @@ namespace arDev
|
|||||||
_device = new System.IO.Ports.SerialPort();
|
_device = new System.IO.Ports.SerialPort();
|
||||||
this.BaudRate = 9600;
|
this.BaudRate = 9600;
|
||||||
ScanInterval = 10;
|
ScanInterval = 10;
|
||||||
_device.DataReceived += barcode_DataReceived;
|
// _device.DataReceived += barcode_DataReceived; // Removed event handler
|
||||||
_device.ErrorReceived += this.barcode_ErrorReceived;
|
_device.ErrorReceived += this.barcode_ErrorReceived;
|
||||||
_device.WriteTimeout = 3000;
|
_device.WriteTimeout = 3000;
|
||||||
_device.ReadTimeout = 3000;
|
_device.ReadTimeout = 3000;
|
||||||
@@ -147,9 +153,23 @@ namespace arDev
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
_device.DataReceived -= barcode_DataReceived;
|
// Stop reading thread
|
||||||
|
_isReading = false;
|
||||||
|
|
||||||
|
// _device.DataReceived -= barcode_DataReceived; // Removed event handler
|
||||||
_device.ErrorReceived -= this.barcode_ErrorReceived;
|
_device.ErrorReceived -= this.barcode_ErrorReceived;
|
||||||
|
|
||||||
|
if (_recvThread != null && _recvThread.IsAlive)
|
||||||
|
{
|
||||||
|
_recvThread.Join(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_device != null)
|
||||||
|
{
|
||||||
|
if (_device.IsOpen) _device.Close();
|
||||||
|
_device.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
// Free any unmanaged objects here.
|
// Free any unmanaged objects here.
|
||||||
//
|
//
|
||||||
disposed = true;
|
disposed = true;
|
||||||
@@ -159,8 +179,24 @@ namespace arDev
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_device.Open();
|
if (_device.IsOpen == false)
|
||||||
return IsOpen;
|
{
|
||||||
|
_device.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_device.IsOpen)
|
||||||
|
{
|
||||||
|
// Start polling thread
|
||||||
|
if (_isReading == false)
|
||||||
|
{
|
||||||
|
_isReading = true;
|
||||||
|
_recvThread = new Thread(ReadPort);
|
||||||
|
_recvThread.IsBackground = true;
|
||||||
|
_recvThread.Start();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -193,14 +229,31 @@ namespace arDev
|
|||||||
|
|
||||||
public virtual bool Close()
|
public virtual bool Close()
|
||||||
{
|
{
|
||||||
if (_device != null && _device.IsOpen)
|
try
|
||||||
{
|
{
|
||||||
_device.DiscardInBuffer();
|
_isReading = false; // Stop thread loop
|
||||||
_device.DiscardOutBuffer();
|
|
||||||
_device.Close(); //dispose에서는 포트를 직접 클리어하지 않게 해뒀다.
|
if (_recvThread != null && _recvThread.IsAlive)
|
||||||
return true;
|
{
|
||||||
|
if (!_recvThread.Join(500)) // Wait for thread to finish
|
||||||
|
{
|
||||||
|
// _recvThread.Abort(); // Avoid Abort if possible
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_device != null && _device.IsOpen)
|
||||||
|
{
|
||||||
|
_device.DiscardInBuffer();
|
||||||
|
_device.DiscardOutBuffer();
|
||||||
|
_device.Close(); //dispose에서는 포트를 직접 클리어하지 않게 해뒀다.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
else return false;
|
|
||||||
}
|
}
|
||||||
protected Boolean RaiseRecvData()
|
protected Boolean RaiseRecvData()
|
||||||
{
|
{
|
||||||
@@ -226,6 +279,9 @@ namespace arDev
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// UI update might need Invoke if this event handler updates UI directly,
|
||||||
|
// but usually the subscriber handles Invoke.
|
||||||
|
// Since we are running on a background thread now, subscribers must be aware.
|
||||||
Message?.Invoke(this, new MessageEventArgs(Data, true)); //recvmessage
|
Message?.Invoke(this, new MessageEventArgs(Data, true)); //recvmessage
|
||||||
if (ProcessRecvData(Data) == false)
|
if (ProcessRecvData(Data) == false)
|
||||||
{
|
{
|
||||||
@@ -262,6 +318,9 @@ namespace arDev
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte[] buffer = new byte[] { };
|
byte[] buffer = new byte[] { };
|
||||||
|
|
||||||
|
// Replaced with ReadPort Loop
|
||||||
|
/*
|
||||||
void barcode_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
void barcode_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -308,6 +367,62 @@ namespace arDev
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void ReadPort()
|
||||||
|
{
|
||||||
|
while (_isReading)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_device == null || !_device.IsOpen)
|
||||||
|
{
|
||||||
|
Thread.Sleep(100);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int readCount = _device.BytesToRead;
|
||||||
|
if (readCount > 0)
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[readCount];
|
||||||
|
_device.Read(buffer, 0, buffer.Length);
|
||||||
|
|
||||||
|
byte[] remainBuffer;
|
||||||
|
Repeat:
|
||||||
|
if (CustomParser(buffer, out remainBuffer))
|
||||||
|
{
|
||||||
|
//분석완료이므로 받은 데이터를 버퍼에 기록한다
|
||||||
|
if (LastReceiveBuffer == null || (LastReceiveBuffer.Length != tempBuffer.Count))
|
||||||
|
Array.Resize(ref LastReceiveBuffer, tempBuffer.Count);
|
||||||
|
Array.Copy(tempBuffer.ToArray(), LastReceiveBuffer, tempBuffer.Count);
|
||||||
|
tempBuffer.Clear();
|
||||||
|
|
||||||
|
//수신메세지발생
|
||||||
|
RaiseRecvData();
|
||||||
|
if (remainBuffer != null && remainBuffer.Length > 0)
|
||||||
|
{
|
||||||
|
//버퍼를 변경해서 다시 전송을 해준다.
|
||||||
|
buffer = new byte[remainBuffer.Length]; // Reallocate buffer for remaining data
|
||||||
|
Array.Copy(remainBuffer, buffer, remainBuffer.Length);
|
||||||
|
goto Repeat; //남은 버퍼가 있다면 진행을 해준다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Thread.Sleep(20); // Data 없음, 대기
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Thread 상에서 Exception 발생 시 로그 남기고 계속 진행 여부 결정
|
||||||
|
// 여기서는 에러 메시지 발생시키고 Sleep
|
||||||
|
ErrorMessage = ex.Message;
|
||||||
|
this.Message?.Invoke(this, new MessageEventArgs(ex.Message, true));
|
||||||
|
Thread.Sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Submodule Cs_HMI/SubProject/CommUtil updated: b070b711f0...ed05439991
BIN
Document/이메일/RE_ _이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-pic다운로드메뉴얼.msg
Normal file
BIN
Document/이메일/RE_ _이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-pic다운로드메뉴얼.msg
Normal file
Binary file not shown.
BIN
Document/이메일/RE_ _이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-프토토콜+펌웨어파일.msg
Normal file
BIN
Document/이메일/RE_ _이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-프토토콜+펌웨어파일.msg
Normal file
Binary file not shown.
BIN
Document/이메일/_이노텍_ RFID 헥사파일 송부건.msg
Normal file
BIN
Document/이메일/_이노텍_ RFID 헥사파일 송부건.msg
Normal file
Binary file not shown.
BIN
Document/이메일/_이노텍_ 리프트형 AGV 펌웨어 송부건.msg
Normal file
BIN
Document/이메일/_이노텍_ 리프트형 AGV 펌웨어 송부건.msg
Normal file
Binary file not shown.
BIN
Document/이메일/_이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-프로토콜.msg
Normal file
BIN
Document/이메일/_이노텍_ 통신 프로토콜 송부건 (AGV_V350_LF)-프로토콜.msg
Normal file
Binary file not shown.
BIN
Document/통신프로토콜/리듐인산철 통신프로토콜정리 .xlsx
Normal file
BIN
Document/통신프로토콜/리듐인산철 통신프로토콜정리 .xlsx
Normal file
Binary file not shown.
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_24.11.20.xlsx
Normal file
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_24.11.20.xlsx
Normal file
Binary file not shown.
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_25.01.10.xlsx
Normal file
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_25.01.10.xlsx
Normal file
Binary file not shown.
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_25.01.10_r2.xlsx
Normal file
BIN
Document/통신프로토콜/통신 프로토콜_AGV_V350_LF_25.01.10_r2.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user