如何在TwinCAT 2 NC I中读取当前执行NC程序行并同步至C#应用
TwinCAT NC当前程序行读取问题
需求
在C#应用中显示当前执行的NC程序行,支持两种实现路径:
- 在ST中将NC行作为变量存储,再由C#读取
- C#直接通过ADS读取NC当前程序行
问题现象
参考倍福TwinCAT 2文档中「TwinCAT NC -> TwinCAT NC I -> 附录 -> 零件程序显示:通过ADS读取当前NC行」的方法,使用索引组0x23000002、索引偏移0x20000001尝试读取,遇到以下错误:
- C#端:先后尝试TC2默认端口801和文档指定的500端口,调用
AdsClient.Read/ReadAny或注册设备通知时,报ADS错误0x701(Service is not supported by the server) - PLC ST端:使用
ADSREAD功能块读取时,报错误0x745(错误ID 1861:Timeout has occurred – the remote terminal is not responding in the specified ADS timeout. The route setting of the remote terminal may be configured incorrectly.)
补充细节
C#代码执行差异
- 以下读取代码可成功获取NC程序行内容:
TcAdsClient tcAdsClient = new TcAdsClient(); tcAdsClient.Connect("192.168.65.102.1.1", 500); try { string res = tcAdsClient.ReadAnyString(indexGroup, indexOffset, 80, Encoding.Default); ConsoleOutput += res; tcAdsClient.Dispose(); } catch (Exception e) { MessageBox.Show(e.Message); }
- 但注册设备通知的代码触发
0x701错误:
int sHandle = tcAdsClient.AddDeviceNotification(indexGroup, indexOffset, _AdsStream, 0, 80, AdsTransMode.OnChange, 250, 0, null);
ST功能块调用问题
ADSREAD功能块始终返回1861错误,代码示例:
fbADSRead: ADSREAD; sBuffer: DWORD; bExec: BOOL; ---- IF bExec THEN fbADSRead( NETID :='192.168.65.102.1.1', PORT :=500, IDXGRP :=16#2302, IDXOFFS :=16#20000001, LEN :=80, DESTADDR :=sBuffer, READ :=TRUE, TMOUT :=t#5s); END_IF
内容的提问来源于stack exchange,提问作者w1r2s
相关产品推荐
相关产品推荐

