├── .gitignore
├── FanucInterfaceSample
├── FanucInterfaceSample.sln
├── FanucInterfaceSample
│ ├── App.config
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── FanucInterfaceSample.csproj
│ ├── MainWindow.xaml
│ ├── MainWindow.xaml.cs
│ └── Properties
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ ├── Resources.resx
│ │ ├── Settings.Designer.cs
│ │ └── Settings.settings
└── FanucRobIntelface
│ ├── FanucRobIntelface.cs
│ ├── FanucRobIntelface.csproj
│ ├── RobotData.cs
│ ├── SocketBase.cs
│ └── hexhelper.cs
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | [Bb]in/
27 | [Oo]bj/
28 | [Ll]og/
29 |
30 | # Visual Studio 2015/2017 cache/options directory
31 | .vs/
32 | # Uncomment if you have tasks that create the project's static files in wwwroot
33 | #wwwroot/
34 |
35 | # Visual Studio 2017 auto generated files
36 | Generated\ Files/
37 |
38 | # MSTest test Results
39 | [Tt]est[Rr]esult*/
40 | [Bb]uild[Ll]og.*
41 |
42 | # NUNIT
43 | *.VisualState.xml
44 | TestResult.xml
45 |
46 | # Build Results of an ATL Project
47 | [Dd]ebugPS/
48 | [Rr]eleasePS/
49 | dlldata.c
50 |
51 | # Benchmark Results
52 | BenchmarkDotNet.Artifacts/
53 |
54 | # .NET Core
55 | project.lock.json
56 | project.fragment.lock.json
57 | artifacts/
58 |
59 | # StyleCop
60 | StyleCopReport.xml
61 |
62 | # Files built by Visual Studio
63 | *_i.c
64 | *_p.c
65 | *_h.h
66 | *.ilk
67 | *.meta
68 | *.obj
69 | *.iobj
70 | *.pch
71 | *.pdb
72 | *.ipdb
73 | *.pgc
74 | *.pgd
75 | *.rsp
76 | *.sbr
77 | *.tlb
78 | *.tli
79 | *.tlh
80 | *.tmp
81 | *.tmp_proj
82 | *_wpftmp.csproj
83 | *.log
84 | *.vspscc
85 | *.vssscc
86 | .builds
87 | *.pidb
88 | *.svclog
89 | *.scc
90 |
91 | # Chutzpah Test files
92 | _Chutzpah*
93 |
94 | # Visual C++ cache files
95 | ipch/
96 | *.aps
97 | *.ncb
98 | *.opendb
99 | *.opensdf
100 | *.sdf
101 | *.cachefile
102 | *.VC.db
103 | *.VC.VC.opendb
104 |
105 | # Visual Studio profiler
106 | *.psess
107 | *.vsp
108 | *.vspx
109 | *.sap
110 |
111 | # Visual Studio Trace Files
112 | *.e2e
113 |
114 | # TFS 2012 Local Workspace
115 | $tf/
116 |
117 | # Guidance Automation Toolkit
118 | *.gpState
119 |
120 | # ReSharper is a .NET coding add-in
121 | _ReSharper*/
122 | *.[Rr]e[Ss]harper
123 | *.DotSettings.user
124 |
125 | # JustCode is a .NET coding add-in
126 | .JustCode
127 |
128 | # TeamCity is a build add-in
129 | _TeamCity*
130 |
131 | # DotCover is a Code Coverage Tool
132 | *.dotCover
133 |
134 | # AxoCover is a Code Coverage Tool
135 | .axoCover/*
136 | !.axoCover/settings.json
137 |
138 | # Visual Studio code coverage results
139 | *.coverage
140 | *.coveragexml
141 |
142 | # NCrunch
143 | _NCrunch_*
144 | .*crunch*.local.xml
145 | nCrunchTemp_*
146 |
147 | # MightyMoose
148 | *.mm.*
149 | AutoTest.Net/
150 |
151 | # Web workbench (sass)
152 | .sass-cache/
153 |
154 | # Installshield output folder
155 | [Ee]xpress/
156 |
157 | # DocProject is a documentation generator add-in
158 | DocProject/buildhelp/
159 | DocProject/Help/*.HxT
160 | DocProject/Help/*.HxC
161 | DocProject/Help/*.hhc
162 | DocProject/Help/*.hhk
163 | DocProject/Help/*.hhp
164 | DocProject/Help/Html2
165 | DocProject/Help/html
166 |
167 | # Click-Once directory
168 | publish/
169 |
170 | # Publish Web Output
171 | *.[Pp]ublish.xml
172 | *.azurePubxml
173 | # Note: Comment the next line if you want to checkin your web deploy settings,
174 | # but database connection strings (with potential passwords) will be unencrypted
175 | *.pubxml
176 | *.publishproj
177 |
178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
179 | # checkin your Azure Web App publish settings, but sensitive information contained
180 | # in these scripts will be unencrypted
181 | PublishScripts/
182 |
183 | # NuGet Packages
184 | *.nupkg
185 | # The packages folder can be ignored because of Package Restore
186 | **/[Pp]ackages/*
187 | # except build/, which is used as an MSBuild target.
188 | !**/[Pp]ackages/build/
189 | # Uncomment if necessary however generally it will be regenerated when needed
190 | #!**/[Pp]ackages/repositories.config
191 | # NuGet v3's project.json files produces more ignorable files
192 | *.nuget.props
193 | *.nuget.targets
194 |
195 | # Microsoft Azure Build Output
196 | csx/
197 | *.build.csdef
198 |
199 | # Microsoft Azure Emulator
200 | ecf/
201 | rcf/
202 |
203 | # Windows Store app package directories and files
204 | AppPackages/
205 | BundleArtifacts/
206 | Package.StoreAssociation.xml
207 | _pkginfo.txt
208 | *.appx
209 |
210 | # Visual Studio cache files
211 | # files ending in .cache can be ignored
212 | *.[Cc]ache
213 | # but keep track of directories ending in .cache
214 | !?*.[Cc]ache/
215 |
216 | # Others
217 | ClientBin/
218 | ~$*
219 | *~
220 | *.dbmdl
221 | *.dbproj.schemaview
222 | *.jfm
223 | *.pfx
224 | *.publishsettings
225 | orleans.codegen.cs
226 |
227 | # Including strong name files can present a security risk
228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
229 | #*.snk
230 |
231 | # Since there are multiple workflows, uncomment next line to ignore bower_components
232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
233 | #bower_components/
234 |
235 | # RIA/Silverlight projects
236 | Generated_Code/
237 |
238 | # Backup & report files from converting an old project file
239 | # to a newer Visual Studio version. Backup files are not needed,
240 | # because we have git ;-)
241 | _UpgradeReport_Files/
242 | Backup*/
243 | UpgradeLog*.XML
244 | UpgradeLog*.htm
245 | ServiceFabricBackup/
246 | *.rptproj.bak
247 |
248 | # SQL Server files
249 | *.mdf
250 | *.ldf
251 | *.ndf
252 |
253 | # Business Intelligence projects
254 | *.rdl.data
255 | *.bim.layout
256 | *.bim_*.settings
257 | *.rptproj.rsuser
258 | *- Backup*.rdl
259 |
260 | # Microsoft Fakes
261 | FakesAssemblies/
262 |
263 | # GhostDoc plugin setting file
264 | *.GhostDoc.xml
265 |
266 | # Node.js Tools for Visual Studio
267 | .ntvs_analysis.dat
268 | node_modules/
269 |
270 | # Visual Studio 6 build log
271 | *.plg
272 |
273 | # Visual Studio 6 workspace options file
274 | *.opt
275 |
276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
277 | *.vbw
278 |
279 | # Visual Studio LightSwitch build output
280 | **/*.HTMLClient/GeneratedArtifacts
281 | **/*.DesktopClient/GeneratedArtifacts
282 | **/*.DesktopClient/ModelManifest.xml
283 | **/*.Server/GeneratedArtifacts
284 | **/*.Server/ModelManifest.xml
285 | _Pvt_Extensions
286 |
287 | # Paket dependency manager
288 | .paket/paket.exe
289 | paket-files/
290 |
291 | # FAKE - F# Make
292 | .fake/
293 |
294 | # JetBrains Rider
295 | .idea/
296 | *.sln.iml
297 |
298 | # CodeRush personal settings
299 | .cr/personal
300 |
301 | # Python Tools for Visual Studio (PTVS)
302 | __pycache__/
303 | *.pyc
304 |
305 | # Cake - Uncomment if you are using it
306 | # tools/**
307 | # !tools/packages.config
308 |
309 | # Tabs Studio
310 | *.tss
311 |
312 | # Telerik's JustMock configuration file
313 | *.jmconfig
314 |
315 | # BizTalk build output
316 | *.btp.cs
317 | *.btm.cs
318 | *.odx.cs
319 | *.xsd.cs
320 |
321 | # OpenCover UI analysis results
322 | OpenCover/
323 |
324 | # Azure Stream Analytics local run output
325 | ASALocalRun/
326 |
327 | # MSBuild Binary and Structured Log
328 | *.binlog
329 |
330 | # NVidia Nsight GPU debugger configuration file
331 | *.nvuser
332 |
333 | # MFractors (Xamarin productivity tool) working folder
334 | .mfractor/
335 |
336 | # Local History for Visual Studio
337 | .localhistory/
338 |
339 | # BeatPulse healthcheck temp database
340 | healthchecksdb
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29613.14
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FanucInterfaceSample", "FanucInterfaceSample\FanucInterfaceSample.csproj", "{37320E33-A48E-4108-95BD-9AE8D0A0C27D}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FanucRobIntelface", "FanucRobIntelface\FanucRobIntelface.csproj", "{8D70246D-C514-4D7C-A23D-99CE09444BFD}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {37320E33-A48E-4108-95BD-9AE8D0A0C27D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {37320E33-A48E-4108-95BD-9AE8D0A0C27D}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {37320E33-A48E-4108-95BD-9AE8D0A0C27D}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {37320E33-A48E-4108-95BD-9AE8D0A0C27D}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {8D70246D-C514-4D7C-A23D-99CE09444BFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {8D70246D-C514-4D7C-A23D-99CE09444BFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {8D70246D-C514-4D7C-A23D-99CE09444BFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {8D70246D-C514-4D7C-A23D-99CE09444BFD}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {160B3D72-8A8B-4635-9DFF-4485ABD3CCF5}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | type your robot ip address here
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace FanucInterfaceSample
10 | {
11 | ///
12 | /// App.xaml 的交互逻辑
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/FanucInterfaceSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {37320E33-A48E-4108-95BD-9AE8D0A0C27D}
8 | WinExe
9 | FanucInterfaceSample
10 | FanucInterfaceSample
11 | v4.6.1
12 | 512
13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | 4
15 | true
16 | true
17 |
18 |
19 | AnyCPU
20 | true
21 | full
22 | false
23 | bin\Debug\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 |
28 |
29 | AnyCPU
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | 4.0
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | MSBuild:Compile
56 | Designer
57 |
58 |
59 | MSBuild:Compile
60 | Designer
61 |
62 |
63 | App.xaml
64 | Code
65 |
66 |
67 | MainWindow.xaml
68 | Code
69 |
70 |
71 |
72 |
73 | Code
74 |
75 |
76 | True
77 | True
78 | Resources.resx
79 |
80 |
81 | True
82 | Settings.settings
83 | True
84 |
85 |
86 | ResXFileCodeGenerator
87 | Resources.Designer.cs
88 |
89 |
90 | SettingsSingleFileGenerator
91 | Settings.Designer.cs
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | {8d70246d-c514-4d7c-a23d-99ce09444bfd}
100 | FanucRobIntelface
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Data;
9 | using System.Windows.Documents;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 | using System.Windows.Media.Imaging;
13 | using System.Windows.Navigation;
14 | using System.Windows.Shapes;
15 | using FanucRobot;
16 |
17 | namespace FanucInterfaceSample
18 | {
19 | ///
20 | /// MainWindow.xaml 的交互逻辑
21 | ///
22 | public partial class MainWindow : Window
23 | {
24 | public MainWindow()
25 | {
26 | InitializeComponent();
27 | }
28 | FanucRobIntelface fi;
29 | void PrintResult(string title, Array obj)
30 | {
31 | this.txtResult.Text += title + "=";
32 | for (int i = 0; i < obj.Length; i++)
33 | {
34 | this.txtResult.Text += obj.GetValue(i).ToString() + ",";
35 | }
36 | this.txtResult.Text += "\r\n";
37 | }
38 | private void ConnectButtonClicked(object sender, RoutedEventArgs e)
39 | {
40 | fi = new FanucRobIntelface(this.ipTxt.Text);
41 | var ret = fi.Connect();
42 | this.cmdgrid.IsEnabled = !ret.isError;
43 |
44 | txtResult.Text += $"connect -> {!ret.isError}--msg -> {ret.message}\r\n";
45 | }
46 |
47 | private void ReadRClicked(object sender, RoutedEventArgs e)
48 | {
49 | fi.Refresh();
50 | PrintResult($"int - R[{fi.intRegion[0]}-{fi.intRegion[1]}]", fi.intRegs);
51 | PrintResult($"float -R[{fi.floatRegion[0]}-{fi.floatRegion[1]}]", fi.floatRegs);
52 | }
53 |
54 | private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
55 | {
56 | Properties.Settings.Default.Save();
57 | }
58 |
59 | private void WriteRClicked(object sender, RoutedEventArgs e)
60 | {
61 | var val = new int[] { 1, 2, 3, 4, 5, 6 };
62 | var issuccess = fi.WriteR(val, 1);
63 | this.txtResult.Text += $"{issuccess} = fi.WriteR(val, 1);\r\n";
64 | var real = new float[] { 1.1f, 1.2f, 1.3f };
65 | fi.WriteR(real, 101);
66 | this.txtResult.Text += $"{issuccess} = fi.WriteR(real, 101);\r\n";
67 |
68 | }
69 |
70 | private void ReadPRClicked(object sender, RoutedEventArgs e)
71 | {
72 | fi.Refresh();
73 | for (int i = 0; i < fi.prRegs.Length; i++)
74 | {
75 | PrintResult($"PR[{fi.prRegion[0] + i}]", fi.prRegs[i].pc);
76 | }
77 | }
78 |
79 | private void DisconnectButtonClicked(object sender, RoutedEventArgs e)
80 | {
81 | fi.Disconnect();
82 | fi = null;
83 | this.txtResult.Clear();
84 | this.cmdgrid.IsEnabled = false;
85 | }
86 |
87 | private void ClearBtnClicked(object sender, RoutedEventArgs e)
88 | {
89 | this.txtResult.Clear();
90 | }
91 |
92 | private void WritePRClicked(object sender, RoutedEventArgs e)
93 | {
94 | var issuccess = fi.WritePR(new PR { pc = new float[] { 1, 2, 3, 4, 5, 6 } }, 1);
95 | this.txtResult.Text += $"{issuccess} = fi.WritePR();\r\n";
96 | }
97 |
98 | private void ReadCurPosClicked(object sender, RoutedEventArgs e)
99 | {
100 | fi.Refresh();
101 | PrintResult($"CurrentPos-XYZ", fi.curPos.pc);
102 | PrintResult($"CurrentPos-J", fi.curPos.pj);
103 | }
104 |
105 | private void ReadSdoClicked(object sender, RoutedEventArgs e)
106 | {
107 | var startIdx = 101;
108 | var count = 10;
109 | bool[] data = null;
110 | var ret = fi.ReadSdo(startIdx, count, ref data);
111 | PrintResult($"SDO[{startIdx}-{count + startIdx - 1}]", data);
112 | }
113 |
114 | private void ReadSdiClicked(object sender, RoutedEventArgs e)
115 | {
116 | var startIdx = 101;
117 | var count = 20;
118 | bool[] data = null;
119 | var ret = fi.ReadSdI(startIdx, count, ref data);
120 | PrintResult($"SDO[{startIdx}-{count + startIdx - 1}]", data);
121 | }
122 |
123 | private void ReadRdoClicked(object sender, RoutedEventArgs e)
124 | {
125 | var startIdx = 1;
126 | var count = 8;
127 | bool[] data = null;
128 | var ret = fi.ReadRdo(startIdx, count, ref data);
129 | PrintResult($"RDO[{startIdx}-{count + startIdx - 1}]", data);
130 | }
131 |
132 | private void ReadRdiClicked(object sender, RoutedEventArgs e)
133 | {
134 | var startIdx = 1;
135 | var count = 8;
136 | bool[] data = null;
137 | var ret = fi.ReadRdi(startIdx, count, ref data);
138 | PrintResult($"RDI[{startIdx}-{count + startIdx - 1}]", data);
139 | }
140 |
141 | private void WriteSdoClicked(object sender, RoutedEventArgs e)
142 | {
143 | var b = new bool[100];
144 | for (int i = 0; i < 100; i++)
145 | {
146 | b[i] = true;
147 | }
148 | var issuccess = fi.WriteSdo(b, 101);
149 | this.txtResult.Text += $"{issuccess} = fi.WriteSdo();\r\n";
150 |
151 | }
152 |
153 | private void WriteRdoClicked(object sender, RoutedEventArgs e)
154 | {
155 | var issuccess = fi.WriteRdo(new bool[] { true, false, true, true }, 1);
156 | this.txtResult.Text += $"{issuccess} = fi.WriteRdo();\r\n";
157 |
158 | }
159 |
160 | private void ReadSrClicked(object sender, RoutedEventArgs e)
161 | {
162 | fi.Refresh();
163 | PrintResult($"SR[{fi.strRegion[0]}-{fi.strRegion[1]}]", fi.strRegs);
164 | }
165 |
166 | private void WriteSrClicked(object sender, RoutedEventArgs e)
167 | {
168 | var issuccess = fi.WriteSR(new string[] { "123", "456", "789" }, 1);
169 | this.txtResult.Text += $"{issuccess} = fi.WriteSR();\r\n";
170 |
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // 有关程序集的一般信息由以下
8 | // 控制。更改这些特性值可修改
9 | // 与程序集关联的信息。
10 | [assembly: AssemblyTitle("FanucInterfaceSample")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("Microsoft")]
14 | [assembly: AssemblyProduct("FanucInterfaceSample")]
15 | [assembly: AssemblyCopyright("Copyright © Microsoft 2020")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // 将 ComVisible 设置为 false 会使此程序集中的类型
20 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
21 | //请将此类型的 ComVisible 特性设置为 true。
22 | [assembly: ComVisible(false)]
23 |
24 | //若要开始生成可本地化的应用程序,请设置
25 | //.csproj 文件中的 CultureYouAreCodingWith
26 | //例如,如果您在源文件中使用的是美国英语,
27 | //使用的是美国英语,请将 设置为 en-US。 然后取消
28 | //对以下 NeutralResourceLanguage 特性的注释。 更新
29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置
36 | //(未在页面中找到资源时使用,
37 | //或应用程序资源字典中找到时使用)
38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
39 | //(未在页面中找到资源时使用,
40 | //、应用程序或任何主题专用资源字典中找到时使用)
41 | )]
42 |
43 |
44 | // 程序集的版本信息由下列四个值组成:
45 | //
46 | // 主版本
47 | // 次版本
48 | // 生成号
49 | // 修订号
50 | //
51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
52 | //通过使用 "*",如下所示:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本: 4.0.30319.42000
5 | //
6 | // 对此文件的更改可能导致不正确的行为,如果
7 | // 重新生成代码,则所做更改将丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace FanucInterfaceSample.Properties
12 | {
13 |
14 |
15 | ///
16 | /// 强类型资源类,用于查找本地化字符串等。
17 | ///
18 | // 此类是由 StronglyTypedResourceBuilder
19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
21 | // (以 /str 作为命令选项),或重新生成 VS 项目。
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// 返回此类使用的缓存 ResourceManager 实例。
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FanucInterfaceSample.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// 覆盖当前线程的 CurrentUICulture 属性
56 | /// 使用此强类型的资源类的资源查找。
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本:4.0.30319.42000
5 | //
6 | // 对此文件的更改可能会导致不正确的行为,并且如果
7 | // 重新生成代码,这些更改将会丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace FanucInterfaceSample.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 |
26 | [global::System.Configuration.UserScopedSettingAttribute()]
27 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
28 | [global::System.Configuration.DefaultSettingValueAttribute("type your robot ip address here")]
29 | public string ipAddr {
30 | get {
31 | return ((string)(this["ipAddr"]));
32 | }
33 | set {
34 | this["ipAddr"] = value;
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucInterfaceSample/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | type your robot ip address here
7 |
8 |
9 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucRobIntelface/FanucRobIntelface.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Net.Sockets;
5 | using System.Text;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 |
9 | namespace FanucRobot
10 | {
11 | public class FanucRobIntelface:SocketBase
12 | {
13 | int regCount = 0;
14 | bool isConnected = false;
15 | Dictionary registerList = new Dictionary();
16 |
17 | //数值寄存器
18 | //int和float数组是同一块R寄存器的内容,只是编码方式不同,区间设置可以重叠。
19 | public int[] intRegion = new int[] { 1, 100 };//表示R[1]~R[100]为整数
20 | public int[] intRegs = null;//数据
21 | public int[] floatRegion = new int[] { 101, 200 };//表示R[101]~R[200]为实数
22 | public float[] floatRegs = null;
23 |
24 | //位置寄存器
25 | public int[] prRegion = new int[] { 1, 10 };
26 | public PR[] prRegs = null;
27 | public bool canReadCurrentPos = true;//设置是否读取当前位置
28 | public PR curPos = null;
29 |
30 | //字符串寄存器
31 | public int[] strRegion = new int[] { 1, 10 };
32 | public string[] strRegs = null;
33 |
34 | public FanucRobIntelface(string ipAdr) : base(ipAdr, 60008) { }
35 | public ResultMessage Connect()
36 | {
37 | if (this.isConnected) return CreatResult();
38 | var cmdSend = hexhelper.Hexstr2Byte("0000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
39 | byte[] recData = new byte[256];
40 | var result = SendRead(cmdSend, recData);
41 | if (result.isError) return result;
42 | var isSuccess = hexhelper.CheckEquel(hexhelper.Hexstr2Byte("0100000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), recData);
43 | if (!isSuccess) return CreatResult(true, "data error : in CheckEquel.");
44 | cmdSend = hexhelper.Hexstr2Byte("08000100000000000001000000000000000100000000000000000000000001c000000000100e000001014f01000000000000000000000000");
45 | result = SendRead(cmdSend, recData);
46 | if (result.isError) return result;
47 | result = CheckAnswer(recData, "01", "d4", 0, "02");
48 | if (result.isError) return result;
49 | cmdSend = hexhelper.Hexstr2Byte("02000800000000000001000000000000000100000000000000000000000008c000000000100e00000101073800000600434c524153470400");
50 | result = SendRead(cmdSend, recData);
51 | if (result.isError) return result;
52 | result = CheckAnswer(recData, "08", "d4", 0);
53 | if (result.isError) return result;
54 |
55 | int regLength = 0;
56 | string regStr = null;
57 | //注册int区
58 | if (intRegion[0] < intRegion[1])
59 | {
60 | regLength = (intRegion[1] - intRegion[0] + 1) * 2;
61 | regStr = $"SETASG {regCount + 1} {regLength} R[{intRegion[0]}] 1.0";
62 | result = Register(regStr);
63 | if (result.isError) return result;
64 | registerList.Add(RegCls.Rint, new int[] { regCount, regLength });
65 | regCount += regLength;
66 | }
67 |
68 | //注册float区
69 | if (floatRegion[0] < floatRegion[1])
70 | {
71 | regLength = (floatRegion[1] - floatRegion[0] + 1) * 2;
72 | regStr = $"SETASG {regCount + 1} {regLength} R[{floatRegion[0]}] 0";
73 | result = Register(regStr);
74 | if (result.isError) return result;
75 | registerList.Add(RegCls.Rfloat, new int[] { regCount, regLength });
76 | regCount += regLength;
77 | }
78 |
79 | //注册当前位置
80 | if (canReadCurrentPos)
81 | {
82 | regLength = 50;
83 | regStr = $"SETASG {regCount + 1} {regLength} POS[0] 0.0";
84 | result = Register(regStr);
85 | if (result.isError) return result;
86 | registerList.Add(RegCls.Rpos, new int[] { regCount, regLength });
87 | regCount += regLength;
88 | }
89 | //注册PR寄存器
90 | if (prRegion[0] < prRegion[1])
91 | {
92 | regLength = (prRegion[1] - prRegion[0] + 1) * 50;
93 | regStr = $"SETASG {regCount + 1} {regLength} PR[{prRegion[0]}] 0.0";
94 | result = Register(regStr);
95 | if (result.isError) return result;
96 | registerList.Add(RegCls.Rpr, new int[] { regCount, regLength });
97 | regCount += regLength;
98 | }
99 | //注册SR寄存器
100 | if (strRegion[0] < strRegion[1])
101 | {
102 | regLength = (strRegion[1] - strRegion[0] + 1) * 40;
103 | regStr = $"SETASG {regCount + 1} {regLength} SR[{strRegion[0]}] 1";
104 | result = Register(regStr);
105 | if (result.isError) return result;
106 | registerList.Add(RegCls.Rstring, new int[] { regCount, regLength });
107 | regCount += regLength;
108 | }
109 | this.isConnected = true;
110 | return result;
111 | }
112 | public void Disconnect()
113 | {
114 | Close();
115 | }
116 | public ResultMessage Refresh()
117 | {
118 | byte[] conRec = new byte[8000];
119 | var result = FetchData(conRec, 0, regCount, "0408");
120 | if (result.isError) return result;
121 | byte[] data = null;
122 | result = CheckAnswer2(conRec, regCount * 2,ref data);
123 | if (result.isError) return result;
124 | return GetData(data);
125 | }
126 | public ResultMessage ReadSdo(int startIdx, int count,ref bool[] dataOut)
127 | {
128 | startIdx--;
129 | return ReadIO(startIdx, count, "0446",ref dataOut);
130 | }
131 | public ResultMessage ReadSdI(int startIdx, int count, ref bool[] dataOut)
132 | {
133 | startIdx--;
134 | return ReadIO(startIdx, count, "0448", ref dataOut);
135 | }
136 | public ResultMessage ReadRdo(int startIdx, int count, ref bool[] dataOut)
137 | {
138 | startIdx--;
139 | startIdx += 5000;
140 | return ReadIO(startIdx, count, "0446",ref dataOut);
141 | }
142 | public ResultMessage ReadRdi(int startIdx, int count, ref bool[] dataOut)
143 | {
144 | startIdx--;
145 | startIdx += 5000;
146 | return ReadIO(startIdx, count, "0448",ref dataOut);
147 | }
148 | public ResultMessage WriteSdo(bool[] data, int startIdx)
149 | {
150 | startIdx--;
151 | return WriteIO(data, startIdx);
152 | }
153 | public ResultMessage WriteRdo(bool[] data, int startIdx)
154 | {
155 | startIdx--;
156 | startIdx += 5000;
157 | return WriteIO(data, startIdx);
158 | }
159 | public ResultMessage WriteR(int[] data, int startIdx)
160 | {
161 | startIdx -= intRegion[0];
162 | startIdx *= 2;
163 | startIdx += registerList[RegCls.Rint][0];
164 | var dataB = hexhelper.Int2Byte(data);
165 | return WriteData1(dataB, startIdx, dataB.Length / 2, "0708");
166 | }
167 | public ResultMessage WriteR(float[] data, int startIdx)
168 | {
169 | startIdx -= floatRegion[0];
170 | startIdx *= 2;
171 | startIdx += registerList[RegCls.Rfloat][0];
172 | var dataB = hexhelper.Float2Byte(data);
173 | return WriteData1(dataB, startIdx, dataB.Length / 2, "0708");
174 | }
175 | public ResultMessage WritePR(PR data, int startIdx)
176 | {
177 | startIdx -= prRegion[0];
178 | startIdx *= 50;
179 | startIdx += registerList[RegCls.Rpr][0];
180 | var dataB = Pos2Bytes(data);
181 | return WriteData1(dataB, startIdx, dataB.Length / 2, "0708");
182 | }
183 | public ResultMessage WriteSR(string[] data, int startIdx)
184 | {
185 | startIdx -= strRegion[0];
186 | startIdx *= 40;
187 | startIdx += registerList[RegCls.Rstring][0];
188 | var dataB = String2Byte(data);
189 | return WriteData1(dataB, startIdx, dataB.Length / 2, "0708");
190 | }
191 |
192 |
193 |
194 | ResultMessage Register(string rStr)
195 | {
196 | return WriteData1(Encoding.ASCII.GetBytes(rStr), 0, rStr.Length, "0738");
197 | }
198 | ResultMessage WriteData1(byte[] data, int startIdx, int count, string code)
199 | {
200 | byte[] recData = new byte[256];
201 | var len = hexhelper.Int2Hexstring(data.Length);
202 | var countH = hexhelper.Int2Hexstring(count);
203 | var startIdxH = hexhelper.Int2Hexstring(startIdx);
204 | var cmdData = hexhelper.Hexstr2Byte($"02000900{len}000000020000000000000002000000000000000000000000098000000000100e000001013200000000000101{code}{startIdxH}{countH}");
205 | var sendData = new byte[cmdData.Length + data.Length];
206 | Array.Copy(cmdData, 0, sendData, 0, cmdData.Length);
207 | Array.Copy(data, 0, sendData, cmdData.Length, data.Length);
208 | var result = SendRead(sendData, recData);
209 | if (result.isError) return result;
210 | return CheckAnswer(recData, "09", "d4", 0);
211 | }
212 | ResultMessage WriteData2(byte[] data, int startIdx, int count, string code)
213 | {
214 | byte[] recData = new byte[256];
215 | var countH = hexhelper.Int2Hexstring(count);
216 | var startIdxH = hexhelper.Int2Hexstring(startIdx);
217 | var cmdData = hexhelper.Hexstr2Byte($"02000800000000000001000000000000000100000000000000000000000008c000000000100e00000101{code}{startIdxH}{countH}0100020003000400");
218 | Array.Copy(data, 0, cmdData, cmdData.Length - 8, data.Length);
219 | var result = SendRead(cmdData, recData);
220 | if (result.isError) return result;
221 | return CheckAnswer(recData, "08", "d4", 0);
222 |
223 | }
224 | ResultMessage FetchData(byte[] data, int startIdx, int count, string code)
225 | {
226 |
227 | var countH = hexhelper.Int2Hexstring(count);
228 | var startIdxH = hexhelper.Int2Hexstring(startIdx);
229 | var cmdData = hexhelper.Hexstr2Byte($"02000600000000000001000000000000000100000000000000000000000006c000000000100e00000101{code}{startIdxH}{countH}0000000000000000");
230 | return SendRead(cmdData, data);
231 | }
232 | ResultMessage CheckAnswer(byte[] data, string code1, string code2, int count, string code3 = "04")
233 | {
234 | var len = hexhelper.Int2Hexstring(count);
235 | var str = $"0300{code1}00{len}000000010000000000000001000000000000000000000000{code1}{code2}100e0000303a000001010000000000000101ff{code3}00007c21";
236 | var corrB = hexhelper.Hexstr2Byte(str);
237 | return hexhelper.CheckEquel(corrB, data)? CreatResult():CreatResult(true,"data error : in CheckAnswer");
238 | }
239 | ResultMessage CheckAnswer1(byte[] answer,ref byte[] dataOut)
240 | {
241 | var correctAnswer = hexhelper.Hexstr2Byte($"03000600000000000001000000000000000100000000000000000000000006d4100e0000303a000001010000000000000000000000007c21");
242 | var dataB = new byte[6];
243 | Array.Copy(answer, 44, dataB, 0, dataB.Length);
244 | Array.Copy(new byte[] { 0, 0, 0, 0, 0, 0 }, 0, answer, 44, 6);
245 | if (hexhelper.CheckEquel(correctAnswer, answer))
246 | {
247 | dataOut = dataB;
248 | return CreatResult();
249 | }
250 | return CreatResult(true, "data error : in CheckAnswer1");
251 | }
252 | ResultMessage CheckAnswer2(byte[] answer, int count ,ref byte[] dataOut)
253 | {
254 | var result = CheckAnswer(answer, "06", "94", count);
255 | if (!result.isError)
256 | {
257 | var dataB = new byte[count];
258 | Array.Copy(answer, 56, dataB, 0, dataB.Length);
259 | dataOut = dataB;
260 | }
261 | return result;
262 | }
263 | ResultMessage ReadIO(int startIdx, int count, string code,ref bool[] dataOut)
264 | {
265 | var countP = (startIdx % 8) + count;
266 | countP = 8 - (countP % 8) + countP;
267 | var idx = startIdx / 8 * 8;
268 |
269 | var recData = new byte[800];
270 | var result = FetchData(recData, idx, countP, code);
271 | if (result.isError) return result;
272 | bool[] dataR;
273 | if (countP > 48)
274 | {
275 | byte[] data = null;
276 | result = CheckAnswer2(recData, countP / 8, ref data);
277 | if (result.isError) return result;
278 | dataR = hexhelper.Byte2Bit(data);
279 | }
280 | else
281 | {
282 | byte[] data = null;
283 | result = CheckAnswer1(recData,ref data);
284 | if (result.isError) return result;
285 | dataR = hexhelper.Byte2Bit(data);
286 | }
287 | var rets = new bool[count];
288 | Array.Copy(dataR, startIdx % 8, rets, 0, rets.Length);
289 | dataOut = rets;
290 | return result;
291 | }
292 | ResultMessage WriteIO(bool[] data, int startIdx)
293 | {
294 | var count = data.Length;
295 | var l = data.ToList();
296 | if (startIdx % 8 > 0)
297 | {
298 | l.InsertRange(0, new bool[startIdx % 8]);
299 | }
300 | if (l.Count % 8 > 0)
301 | {
302 | l.AddRange(new bool[8 - (l.Count % 8)]);
303 | }
304 | var dataR = l.ToArray();
305 | var dataB = hexhelper.Bit2Byte(dataR);
306 | if (dataB.Length > 6)
307 | {
308 | return WriteData1(dataB, startIdx, count, "0746");
309 | }
310 | else
311 | {
312 | return WriteData2(dataB, startIdx, count, "0746");
313 | }
314 | }
315 | ResultMessage GetData(byte[] data)
316 | {
317 | foreach (var reg in registerList)
318 | {
319 | var startIdx = reg.Value[0] * 2;
320 | var count = reg.Value[1] * 2;
321 | switch (reg.Key)
322 | {
323 | case RegCls.Rint:
324 | intRegs = hexhelper.Byte2Int(data, startIdx, count);
325 | break;
326 | case RegCls.Rfloat:
327 | floatRegs = hexhelper.Byte2Float(data, startIdx, count);
328 | break;
329 | case RegCls.Rpos:
330 | curPos = GetPosData(data, startIdx, count)[0];
331 | break;
332 | case RegCls.Rpr:
333 | prRegs = GetPosData(data, startIdx, count);
334 | break;
335 | case RegCls.Rstring:
336 | strRegs = GetString(data, startIdx, count);
337 | break;
338 | default:
339 | return CreatResult(true, "data error : in get data");
340 | }
341 | }
342 | return CreatResult();
343 | }
344 | PR[] GetPosData(byte[] data, int startIdx = 0, int count = 0)
345 | {
346 | var dataF = hexhelper.Byte2Float(data, startIdx, count);
347 | var countP = dataF.Length / 25;
348 | var prs = new PR[countP];
349 | for (int i = 0; i < countP; i++)
350 | {
351 | PR pr = new PR();
352 | Array.Copy(dataF, i * 25 + 0, pr.pc, 0, 6);
353 | Array.Copy(dataF, i * 25 + 13, pr.pj, 0, 6);
354 | prs[i] = pr;
355 | }
356 | return prs;
357 | }
358 | byte[] Pos2Bytes(PR pr)
359 | {
360 | var rets = new byte[52];
361 | var dataB = hexhelper.Float2Byte(pr.pc);
362 | Array.Copy(dataB, 0, rets, 0, dataB.Length);
363 | return rets;
364 | }
365 | byte[] String2Byte(string[] data)
366 | {
367 | var countS = data.Length;
368 | var rets = new byte[countS * 80];
369 | for (int i = 0; i < countS; i++)
370 | {
371 | var dataB = Encoding.ASCII.GetBytes(data[i]);
372 | Array.Copy(dataB, 0, rets, i * 80, dataB.Length);
373 | }
374 | return rets;
375 | }
376 | string[] GetString(byte[] data, int startIdx = 0, int count = 0)
377 | {
378 | if (count == 0)
379 | {
380 | count = data.Length;
381 | }
382 | count /= 80;
383 | var rets = new string[count];
384 | for (int i = 0; i < count; i++)
385 | {
386 | rets[i] = Encoding.ASCII.GetString(data, i * 80 + startIdx, 80);
387 | }
388 | return rets;
389 | }
390 | }
391 | }
392 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucRobIntelface/FanucRobIntelface.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | FanucRobot
6 | FanucRobot
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucRobIntelface/RobotData.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace FanucRobot
8 | {
9 | public class PR
10 | {
11 | public float[] pc = new float[6];
12 | public float[] pj = new float[6];
13 | }
14 | enum RegCls
15 | {
16 | Rint,
17 | Rfloat,
18 | Rpos,
19 | Rpr,
20 | Rstring,
21 | }
22 | public class ResultMessage
23 | {
24 | public bool isError = false;
25 | public string message = string.Empty;
26 | public ResultMessage(bool iserror,string msg)
27 | {
28 | this.isError = iserror;
29 | this.message = msg;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucRobIntelface/SocketBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Net.Sockets;
4 | using System.Text;
5 | using System.Threading;
6 |
7 | namespace FanucRobot
8 | {
9 | public class SocketBase
10 | {
11 | protected string ipAddr;
12 | protected int port;
13 | Socket _sc;
14 | bool connnected = false;
15 | public SocketBase(string ip, int port)
16 | {
17 | this.ipAddr = ip;
18 | this.port = port;
19 | }
20 | protected ResultMessage CreatResult(bool iserr = false, string msg ="")
21 | {
22 | return new ResultMessage(iserr, msg);
23 | }
24 | Socket GetAvailableConn()
25 | {
26 | if (!connnected)
27 | {
28 | _sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
29 | _sc.ReceiveTimeout = 2000;
30 | _sc.SendTimeout = 2000;
31 | var ar = _sc.BeginConnect(ipAddr, port, null, null);
32 | if (ar.AsyncWaitHandle.WaitOne(5000))
33 | {
34 | _sc.EndConnect(ar);
35 | this.connnected = true;
36 | }
37 | else
38 | {
39 | _sc = null;
40 | }
41 |
42 | }
43 | return _sc;
44 | }
45 | protected ResultMessage Send(byte[] send)
46 | {
47 | var socket = GetAvailableConn();
48 | if (socket != null)
49 | {
50 | try
51 | {
52 | socket.Send(send);
53 | }
54 | catch
55 | {
56 | this.connnected = false;
57 | CreatResult(true, "socket error : send time out.");
58 | }
59 | return CreatResult();
60 | }
61 | else
62 | {
63 | return CreatResult(true, "socket error : connect time out.");
64 | }
65 | }
66 | protected ResultMessage Read(byte[] read)
67 | {
68 | var socket = GetAvailableConn();
69 | if (socket != null)
70 | {
71 | try
72 | {
73 | int count = 0;
74 | do
75 | {
76 | Thread.Sleep(20);
77 | count += socket.Receive(read, count, socket.Available, SocketFlags.None);
78 | } while (socket.Available > 0);
79 | }
80 | catch
81 | {
82 | this.connnected = false;
83 | return CreatResult(true, "socket error : read time out.");
84 | }
85 | return CreatResult();
86 | }
87 | else
88 | {
89 | return CreatResult(true, "socket error : connect time out.");
90 | }
91 | }
92 | protected ResultMessage SendRead(byte[] send, byte[] read)
93 | {
94 | var res = this.Send(send);
95 | if (res.isError)
96 | {
97 | return res;
98 | }
99 | else
100 | {
101 | return this.Read(read);
102 | }
103 | }
104 | protected void Close()
105 | {
106 | this._sc?.Close();
107 | this._sc.Dispose();
108 | this.connnected = false;
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/FanucInterfaceSample/FanucRobIntelface/hexhelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace FanucRobot
8 | {
9 | class hexhelper
10 | {
11 | public static byte[] Hexstr2Byte(string hexString)
12 | {
13 | var l = hexString.Length;
14 | if (l % 2 == 1)
15 | {
16 | return null;
17 | }
18 | else
19 | {
20 | byte[] bs = new byte[l / 2];
21 | for (int i = 0; i < l / 2; i++)
22 | {
23 | var s = hexString.Substring(i * 2, 2);
24 | var b = Convert.ToByte(s, 16);
25 | bs[i] = b;
26 | }
27 | return bs;
28 | }
29 | }
30 | public static string Int2Hexstring(int data)
31 | {
32 | var s = string.Format("{0:X4}", data);
33 | return s.Substring(2) + s.Substring(0, 2);
34 | }
35 | public static byte[] Int2Byte(int[] data)
36 | {
37 | byte[] rets = new byte[data.Length * 4];
38 | for (int i = 0; i < data.Length; i++)
39 | {
40 | Array.Copy(BitConverter.GetBytes(data[i]), 0, rets, i * 4, 4);
41 | }
42 | return rets;
43 | }
44 | public static bool CheckEquel(byte[] b1, byte[] b2)
45 | {
46 | for (int i = 0; i < b1.Length; i++)
47 | {
48 | if (b1[i] != b2[i])
49 | {
50 | return false;
51 | }
52 | }
53 | return true;
54 | }
55 | public static int[] Byte2Int(byte[] data, int startIdx = 0, int count = 0)
56 | {
57 | if (count == 0)
58 | {
59 | count = data.Length;
60 | }
61 | var rets = new int[count / 4];
62 | for (int i = 0; i < rets.Length; i++)
63 | {
64 | rets[i] = BitConverter.ToInt32(data, i * 4 + startIdx);
65 | }
66 | return rets;
67 | }
68 | public static float[] Byte2Float(byte[] data, int startIdx = 0, int count = 0)
69 | {
70 | if (count == 0)
71 | {
72 | count = data.Length;
73 | }
74 | var rets = new float[count / 4];
75 | for (int i = 0; i < rets.Length; i++)
76 | {
77 | rets[i] = BitConverter.ToSingle(data, i * 4 + startIdx);
78 | }
79 | return rets;
80 | }
81 | public static byte[] Float2Byte(float[] data)
82 | {
83 | var retLen = data.Length;
84 | var rets = new byte[retLen * 4];
85 | for (int i = 0; i < retLen; i++)
86 | {
87 | Array.Copy(BitConverter.GetBytes(data[i]), 0, rets, i * 4, 4);
88 | }
89 | return rets;
90 | }
91 | public static bool[] Byte2Bit(byte[] data)
92 | {
93 | var rets = new bool[data.Length * 8];
94 | for (int i = 0; i < data.Length; i++)
95 | {
96 | for (int j = 0; j < 8; j++)
97 | {
98 | rets[i * 8 + j] = (data[i] & (1 << j)) == 0 ? false : true;
99 | }
100 | }
101 | return rets;
102 | }
103 | public static byte[] Bit2Byte(bool[] data)
104 | {
105 | var rets = new byte[data.Length / 8];
106 | for (int i = 0; i < rets.Length; i++)
107 | {
108 | for (int j = 0; j < 8; j++)
109 | {
110 | var bit = data[i * 8 + j] ? 1 : 0;
111 | rets[i] = (byte)(rets[i] | (bit << j));
112 | }
113 | }
114 | return rets;
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 wuyoukun
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RobotIntelface
2 | An unofficial implement of fanuc robot-intelface
3 | # test on
4 | R-30ib mate robot controller
5 | # how to use the lib
6 | 1.Instantiate : FanucRobIntelface fi = new FanucRobIntelface("192.168.1.100");
7 | 2.Read data : call fi.Refresh(),then read the data properties such as fi.intRegs. Whenever to get data, the refresh function must be called.
8 | 3.write data : call write functions such as fi.WriteR().
9 |
--------------------------------------------------------------------------------