[제품종류] IEC1000
[개발환경] Visual Studio 2008 C#
SmartModbus를 사용하여 여러 device와 RS485를 통해 MODBUS 통신을 하고 있습니다.System.Threading.Timer 객체를 사용하여 1.5초 마다 MODBUS read를 수행합니다. (ReadHoldingRegister 함수 사용)
→ SmartThread를 사용하여 구현했더니 너무 버벅거리는 문제가 있었습니다.
SmartButton의 Click 이벤트를 통해 MODBUS write를 수행하는 함수를 구현하였습니다. (WriteSingleRegister 함수 사용)
문제는 Click 이벤트와 System.Threading.Timer의 Tick 이벤트가 충돌되는 경우가 발생하여 원하는 대로 동작이 되지 않는 경우가 가끔씩 발생합니다.
이벤트의 실행 우선순위를 설정할 수 있는 방법이 있는지요?
- 구현 예시 -
private void form_Load(object sender, EventArgs e) {
// Initialize timer object
timer = new System.Threading.Timer(new TimerCallback(callBack), null, 0, 1500);
}
public void callBackFunction(object state) {
// Call callback function in UI thread
this.BeginInvoke(new MethodInvoker(repeatFunc));
}
public void repeatFunc() {
for (int idx = 0; idx < TEMP_NUM; idx++) {
modbusProtocol.SlaveAddress = tempID[idx];
eCode = modbusProtocol.ReadHoldingRegister(ADDR_PV, 2, out response);
if (eCode == SmartX.SmartModbus.EXCEPTIONCODE.SUCCESS) {
Array.Reverse(response);
current[idx] = Convert.ToDecimal(BitConverter.ToInt32(response, 0)) / (decimal)10.0;
}
eCode = modbusProtocol.ReadHoldingRegister(ADDR_SV, 2, out response);
if (eCode == SmartX.SmartModbus.EXCEPTIONCODE.SUCCESS) {
Array.Reverse(response);
target[idx] = Convert.ToDecimal(BitConverter.ToInt32(response, 0)) / (decimal)10.0;
}
}
}
private void setMotorSpeed(object sender, EventArgs e) {
setSpeed(Int32.Parse(((SmartX.SmartButton)sender).ButtonText));
}
public void setSpeed(int index) {
modbusProtocol.SlaveAddress = ID_MOTOR;
currentRPM = speedTable[index];
modbusProtocol.WriteSingleRegister(ADDR_SET_MOTOR_SPEED, speedTable[index]);
}