├── README.md
├── PCAN_USB_UI
├── pecan.ico
├── Properties
│ ├── Settings.settings
│ ├── Settings.Designer.cs
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ └── Resources.resx
├── Program.cs
├── PCAN_USB_UI.csproj
├── FrmPCTest.cs
└── FrmPCTest.Designer.cs
├── LICENSE
├── PCAN_USB
├── Properties
│ └── AssemblyInfo.cs
├── PCAN_USB.csproj
├── PCAN-USB.cs
└── PCANBasic.cs
├── PCAN_For_USB.sln
└── .gitignore
/README.md:
--------------------------------------------------------------------------------
1 | # PCAN_For_USB
2 | CSharp code to interface to a PEAK-System PCAN-USB device
3 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/pecan.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GR8DAN/PCAN_For_USB/HEAD/PCAN_USB_UI/pecan.ico
--------------------------------------------------------------------------------
/PCAN_USB_UI/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Windows.Forms;
5 |
6 | namespace CAN.PC
7 | {
8 | static class Program
9 | {
10 | ///
11 | /// The main entry point for the application.
12 | ///
13 | [STAThread]
14 | static void Main()
15 | {
16 | Application.EnableVisualStyles();
17 | Application.SetCompatibleTextRenderingDefault(false);
18 | Application.Run(new FrmPCTest());
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Daniel S. Fowler
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 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace CAN.PC.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.5.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 | }
27 |
--------------------------------------------------------------------------------
/PCAN_USB/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("PCAN_USB C# Interface DLL")]
8 | [assembly: AssemblyDescription("C# DLL to simplify interfacing to PEAK-System Technik GmbH PCAN USB Devices")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("PCAN_USB")]
12 | [assembly: AssemblyCopyright("Copyright © 2017")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("a353007b-3af7-414b-beed-e13906f51ad1")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.InteropServices;
3 |
4 | // General Information about an assembly is controlled through the following
5 | // set of attributes. Change these attribute values to modify the information
6 | // associated with an assembly.
7 | [assembly: AssemblyTitle("PCAN_For_USB to Test PCAN_USB.dll")]
8 | [assembly: AssemblyDescription("A test program to test the simple PCAN_USB.dll that interfaces to PEAK USB devices")]
9 | [assembly: AssemblyConfiguration("")]
10 | [assembly: AssemblyCompany("")]
11 | [assembly: AssemblyProduct("PCAN_For_USB")]
12 | [assembly: AssemblyCopyright("Copyright © 2017")]
13 | [assembly: AssemblyTrademark("")]
14 | [assembly: AssemblyCulture("")]
15 |
16 | // Setting ComVisible to false makes the types in this assembly not visible
17 | // to COM components. If you need to access a type in this assembly from
18 | // COM, set the ComVisible attribute to true on that type.
19 | [assembly: ComVisible(false)]
20 |
21 | // The following GUID is for the ID of the typelib if this project is exposed to COM
22 | [assembly: Guid("3b1d6bf9-6d1c-4926-a882-70eeaa60249e")]
23 |
24 | // Version information for an assembly consists of the following four values:
25 | //
26 | // Major Version
27 | // Minor Version
28 | // Build Number
29 | // Revision
30 | //
31 | // You can specify all the values or you can default the Build and Revision Numbers
32 | // by using the '*' as shown below:
33 | // [assembly: AssemblyVersion("1.0.*")]
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.0.0.0")]
36 |
--------------------------------------------------------------------------------
/PCAN_For_USB.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2003
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PCAN_USB_UI", "PCAN_USB_UI\PCAN_USB_UI.csproj", "{3B1D6BF9-6D1C-4926-A882-70EEAA60249E}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PCAN_USB", "PCAN_USB\PCAN_USB.csproj", "{A353007B-3AF7-414B-BEED-E13906F51AD1}"
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 | {3B1D6BF9-6D1C-4926-A882-70EEAA60249E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {3B1D6BF9-6D1C-4926-A882-70EEAA60249E}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {3B1D6BF9-6D1C-4926-A882-70EEAA60249E}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {3B1D6BF9-6D1C-4926-A882-70EEAA60249E}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {A353007B-3AF7-414B-BEED-E13906F51AD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {A353007B-3AF7-414B-BEED-E13906F51AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {A353007B-3AF7-414B-BEED-E13906F51AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {A353007B-3AF7-414B-BEED-E13906F51AD1}.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 = {58065838-AF93-4DB0-829B-43C471039534}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/PCAN_USB/PCAN_USB.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {A353007B-3AF7-414B-BEED-E13906F51AD1}
8 | Library
9 | Properties
10 | CAN.PC
11 | PCAN_USB
12 | v4.0
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace CAN.PC.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CAN.PC.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/PCAN_USB_UI.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {3B1D6BF9-6D1C-4926-A882-70EEAA60249E}
8 | WinExe
9 | CAN.PC
10 | PCAN_For_USB
11 | v4.0
12 | 512
13 | publish\
14 | true
15 | Disk
16 | false
17 | Foreground
18 | 7
19 | Days
20 | false
21 | false
22 | true
23 | 0
24 | 1.0.0.%2a
25 | false
26 | false
27 | true
28 |
29 |
30 | AnyCPU
31 | true
32 | full
33 | false
34 | bin\Debug\
35 | DEBUG;TRACE
36 | prompt
37 | 4
38 |
39 |
40 | AnyCPU
41 | pdbonly
42 | true
43 | bin\Release\
44 | TRACE
45 | prompt
46 | 4
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | Form
56 |
57 |
58 | FrmPCTest.cs
59 |
60 |
61 |
62 |
63 | FrmPCTest.cs
64 |
65 |
66 | ResXFileCodeGenerator
67 | Resources.Designer.cs
68 | Designer
69 |
70 |
71 | True
72 | Resources.resx
73 | True
74 |
75 |
76 | SettingsSingleFileGenerator
77 | Settings.Designer.cs
78 |
79 |
80 | True
81 | Settings.settings
82 | True
83 |
84 |
85 |
86 |
87 | {a353007b-3af7-414b-beed-e13906f51ad1}
88 | PCAN_USB
89 |
90 |
91 |
92 |
93 | False
94 | .NET Framework 3.5 SP1
95 | false
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/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 |
--------------------------------------------------------------------------------
/.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 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/FrmPCTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Globalization;
4 | using System.Windows.Forms;
5 |
6 | namespace CAN.PC
7 | {
8 | public partial class FrmPCTest : Form
9 | {
10 | PCAN_USB pCAN = new PCAN_USB(); //Layer between form and PEAK interface DLL
11 |
12 | #region Form functions
13 | public FrmPCTest()
14 | {
15 | InitializeComponent();
16 | pCAN.ControlForm = this;
17 | pCAN.Feedback = LstStatus;
18 | pCAN.ReceivedMessages = LstReceivedMessages;
19 | }
20 | //Configure UI on load
21 | private void FrmPCTest_Load(object sender, EventArgs e)
22 | {
23 | LstBaudRate.Items.AddRange(PCAN_USB.CANBaudRates);
24 | LstBaudRate.SelectedIndex=LstBaudRate.Items.IndexOf("500 kbit/s");
25 | //Start check for PCAN device
26 | TimerCheckForDevice.Enabled = true;
27 | }
28 | //Uninitialise connected PEAK PCAN USB device
29 | private void FrmPCTest_FormClosing(object sender, FormClosingEventArgs e)
30 | {
31 | // Releases a current connected Peak CAN USB channel
32 | pCAN.Uninitialize();
33 | }
34 | #endregion
35 |
36 | #region Check for plugged in PCAN USB device
37 | static int ErrorCount = 10; //used to display and error prompt
38 | private void TimerCheckForDevice_Tick(object sender, EventArgs e)
39 | {
40 | TimerCheckForDevice.Enabled = false;
41 | List PeakUSBDevices = PCAN_USB.GetUSBDevices();
42 | if (PeakUSBDevices != null)
43 | LstDevices.Items.AddRange(PeakUSBDevices.ToArray());
44 | else
45 | {
46 | //display an error message every 10 counts
47 | //int remainder = ErrorCount % 10;
48 | if ((ErrorCount++ % 10) == 0)
49 | {
50 | AddMessage(LstStatus,"Plug in a PEAK PCAN USB Adapter");
51 | }
52 | TimerCheckForDevice.Enabled = true;
53 | }
54 | }
55 | #endregion
56 |
57 | #region CAN Id display - converting between decimal and hex
58 | bool bChanging; //Set true when a field is changing (interfield conversion)
59 | //If decimal id changes update hex version
60 | private void TxtId_TextChanged(object sender, EventArgs e)
61 | {
62 | uint number;
63 |
64 | if (!bChanging && (TxtId.Text.Trim().Length > 0))
65 | {
66 | bChanging = true;
67 | try
68 | {
69 | number = uint.Parse(TxtId.Text);
70 | TxtIdHex.Text = number.ToString("X");
71 | }
72 | catch (Exception)
73 | {
74 | TxtIdHex.Text = String.Empty;
75 | }
76 | bChanging = false;
77 | }
78 | else if (TxtId.Text.Trim().Length == 0)
79 | {
80 | bChanging = true;
81 | TxtIdHex.Text = String.Empty;
82 | bChanging = false;
83 | }
84 | }
85 | //If hex id changes update decimal version
86 | private void TxtIdHex_TextChanged(object sender, EventArgs e)
87 | {
88 | if (!bChanging && TxtIdHex.Text.Trim().Length > 0)
89 | {
90 | bChanging = true;
91 | try
92 | {
93 | uint fromHex = uint.Parse(TxtIdHex.Text, System.Globalization.NumberStyles.HexNumber);
94 | TxtId.Text = fromHex.ToString();
95 | }
96 | catch (Exception)
97 | {
98 | TxtId.Text = String.Empty;
99 | }
100 | bChanging = false;
101 | }
102 | else if (TxtIdHex.Text.Trim().Length == 0)
103 | {
104 | bChanging = true;
105 | TxtId.Text = String.Empty;
106 | bChanging = false;
107 | }
108 | }
109 | #endregion
110 |
111 | #region UI routines
112 | //Selecting PEAK USB device
113 | private void LstDevices_SelectedIndexChanged(object sender, EventArgs e)
114 | {
115 | //Close any open channels
116 | pCAN.Uninitialize();
117 | //If a peak device is selected allow baud rate selection and start button
118 | UInt16 handle = 0;
119 | if (LstDevices.SelectedIndex > -1)
120 | handle = PCAN_USB.DecodePEAKHandle(LstDevices.Items[LstDevices.SelectedIndex].ToString());
121 | if (handle > 0)
122 | {
123 | GrpInitialize.Enabled = true;
124 | }
125 | else
126 | {
127 | GrpInitialize.Enabled = false;
128 | }
129 | }
130 | //Start or Stop the selected device
131 | private void ButStartStop_Click(object sender, EventArgs e)
132 | {
133 | UInt16 handle = 0; //PCAN handles are ushorts
134 | if (ButStartStop.Text == "Start")
135 | {
136 | if (LstDevices.SelectedIndex > -1)
137 | handle = PCAN_USB.DecodePEAKHandle(LstDevices.Items[LstDevices.SelectedIndex].ToString());
138 | if(handle > 0)
139 | {
140 | pCAN.PeakCANHandle = handle;
141 | pCAN.InitializeCAN(pCAN.PeakCANHandle, LstBaudRate.Items[LstBaudRate.SelectedIndex].ToString(), true);
142 | GrpMessage.Enabled = true;
143 | GrpReading.Enabled = true;
144 | ChkTrace.Enabled = true;
145 | ButStartStop.Text = "Stop";
146 | }
147 | else
148 | {
149 | AddMessage(LstStatus,"No device selected");
150 | }
151 | }
152 | else
153 | {
154 | pCAN.Uninitialize();
155 | GrpMessage.Enabled = false;
156 | ButClear.Enabled = true;
157 | ChkTrace.Enabled = false;
158 | ButStartStop.Text = "Start";
159 | }
160 | }
161 | //Send the defined CAN data
162 | private void ButSend_Click(object sender, EventArgs e)
163 | {
164 | byte[] data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
165 |
166 | for (int i=0; i<(int)NudLength.Value; i++)
167 | {
168 | if(!Byte.TryParse(((TextBox)GrpMessage.Controls.Find("TxtData" + i.ToString(), true)[0]).Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out data[i]))
169 | AddMessage(LstStatus,"Error converting data to send:"+ ((TextBox)GrpMessage.Controls.Find("TxtData" + i.ToString(), true)[0]).Text);
170 | }
171 | UInt32 id = UInt32.MaxValue;
172 | if(UInt32.TryParse(TxtId.Text, out id))
173 | {
174 | pCAN.WriteFrame(id, (int)NudLength.Value, data);
175 | }
176 | else
177 | {
178 | AddMessage(LstStatus,"Error converting message id:" + TxtId.Text);
179 | }
180 | }
181 | //Clear the displayed packets
182 | private void ButClear_Click(object sender, EventArgs e)
183 | {
184 | pCAN.Packets = new List();
185 | LstReceivedMessages.Items.Clear();
186 | }
187 |
188 | private void NudLength_ValueChanged(object sender, EventArgs e)
189 | {
190 | if (NudLength.Value > 7)
191 | TxtData7.Enabled = true;
192 | else
193 | TxtData7.Enabled = false;
194 | if (NudLength.Value > 6)
195 | TxtData6.Enabled = true;
196 | else
197 | TxtData6.Enabled = false;
198 | if (NudLength.Value > 5)
199 | TxtData5.Enabled = true;
200 | else
201 | TxtData5.Enabled = false;
202 | if (NudLength.Value > 4)
203 | TxtData4.Enabled = true;
204 | else
205 | TxtData4.Enabled = false;
206 | if (NudLength.Value > 3)
207 | TxtData3.Enabled = true;
208 | else
209 | TxtData3.Enabled = false;
210 | if (NudLength.Value > 2)
211 | TxtData2.Enabled = true;
212 | else
213 | TxtData2.Enabled = false;
214 | if (NudLength.Value > 1)
215 | TxtData1.Enabled = true;
216 | else
217 | TxtData1.Enabled = false;
218 | if (NudLength.Value > 0)
219 | TxtData0.Enabled = true;
220 | else
221 | TxtData0.Enabled = false;
222 | }
223 |
224 | private void ChkOverwrite_CheckedChanged(object sender, EventArgs e)
225 | {
226 | pCAN.OverwriteLastPacket = ChkOverwrite.Checked;
227 | LstReceivedMessages.Items.Clear();
228 | }
229 |
230 | private void ButChooseFile_Click(object sender, EventArgs e)
231 | {
232 | using (FolderBrowserDialog dlg = new FolderBrowserDialog())
233 | {
234 | dlg.Description = "Select a folder for the trace file";
235 | if (dlg.ShowDialog() == DialogResult.OK)
236 | {
237 | TxtTraceDir.Text = dlg.SelectedPath;
238 | }
239 | }
240 | }
241 |
242 | private void ChkIdentifyPCANUSB_CheckStateChanged(object sender, EventArgs e)
243 | {
244 | if (LstDevices.SelectedIndex >= 0)
245 | {
246 | UInt16 handle = 0; //used to check identifying state of PCAN USB device
247 | handle = PCAN_USB.DecodePEAKHandle(LstDevices.Items[LstDevices.SelectedIndex].ToString());
248 | //set identify
249 | if(handle>0)
250 | PCAN_USB.SetIdentify(handle, ChkIdentifyPCANUSB.Checked);
251 | }
252 | }
253 | #endregion
254 |
255 | #region Logging
256 |
257 | private void ChkTrace_CheckedChanged(object sender, EventArgs e)
258 | {
259 | if (ChkTrace.Checked)
260 | {
261 | GrpTrace.Enabled = true;
262 | }
263 | else
264 | {
265 | pCAN.StopLogging();
266 | GrpTrace.Enabled = false;
267 | }
268 | }
269 |
270 | private void ButLogging_Click(object sender, EventArgs e)
271 | {
272 | if (ChkTrace.Checked && (ButLogging.Text == "Start"))
273 | {
274 | if (pCAN.StartLogging(TxtTraceDir.Text, ChkMultiFile.Checked, ChkDateFiles.Checked, (UInt32)NudTraceSize.Value))
275 | {
276 | AddMessage(LstStatus, "Logging started.");
277 | ButLogging.Text = "Stop";
278 | }
279 | else
280 | {
281 | AddMessage(LstStatus, "Failed to start logging.");
282 | }
283 | }
284 | else
285 | {
286 | if (!pCAN.StopLogging())
287 | {
288 | AddMessage(LstStatus, "Logging stopped.");
289 | }
290 | ButLogging.Text = "Start";
291 | }
292 | }
293 | #endregion
294 |
295 | #region Utils
296 | private int AddMessage(ListBox AddTo, string MessageToAdd)
297 | {
298 | int ret = -1;
299 |
300 | if (AddTo != null)
301 | {
302 | //Limit number of items
303 | if (AddTo.Items.Count >= 60000)
304 | AddTo.Items.RemoveAt(0);
305 | ret = AddTo.Items.Add(MessageToAdd);
306 | //ensure new item is visible
307 | AddTo.TopIndex = AddTo.Items.Count - 1;
308 | return ret;
309 | }
310 |
311 | return ret;
312 | }
313 | #endregion
314 |
315 |
316 | }
317 | }
318 |
--------------------------------------------------------------------------------
/PCAN_USB_UI/FrmPCTest.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace CAN.PC
2 | {
3 | partial class FrmPCTest
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Windows Form Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | this.components = new System.ComponentModel.Container();
32 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmPCTest));
33 | this.LstDevices = new System.Windows.Forms.ListBox();
34 | this.GrpInitialize = new System.Windows.Forms.GroupBox();
35 | this.ChkTrace = new System.Windows.Forms.CheckBox();
36 | this.LstBaudRate = new System.Windows.Forms.ListBox();
37 | this.ButStartStop = new System.Windows.Forms.Button();
38 | this.LblBaudRate = new System.Windows.Forms.Label();
39 | this.LstStatus = new System.Windows.Forms.ListBox();
40 | this.TxtData7 = new System.Windows.Forms.TextBox();
41 | this.TxtData6 = new System.Windows.Forms.TextBox();
42 | this.TxtData5 = new System.Windows.Forms.TextBox();
43 | this.TxtData4 = new System.Windows.Forms.TextBox();
44 | this.TxtData3 = new System.Windows.Forms.TextBox();
45 | this.TxtData2 = new System.Windows.Forms.TextBox();
46 | this.TxtData1 = new System.Windows.Forms.TextBox();
47 | this.TxtData0 = new System.Windows.Forms.TextBox();
48 | this.NudLength = new System.Windows.Forms.NumericUpDown();
49 | this.LblNumBytes = new System.Windows.Forms.Label();
50 | this.TxtIdHex = new System.Windows.Forms.TextBox();
51 | this.TxtId = new System.Windows.Forms.TextBox();
52 | this.LblCANId = new System.Windows.Forms.Label();
53 | this.ButSend = new System.Windows.Forms.Button();
54 | this.GrpMessage = new System.Windows.Forms.GroupBox();
55 | this.LblStatus = new System.Windows.Forms.Label();
56 | this.GrpReading = new System.Windows.Forms.GroupBox();
57 | this.ChkOverwrite = new System.Windows.Forms.CheckBox();
58 | this.LstReceivedMessages = new System.Windows.Forms.ListBox();
59 | this.ButClear = new System.Windows.Forms.Button();
60 | this.TimerCheckForDevice = new System.Windows.Forms.Timer(this.components);
61 | this.LblListOfDevices = new System.Windows.Forms.Label();
62 | this.GrpTrace = new System.Windows.Forms.GroupBox();
63 | this.LblMaxSize = new System.Windows.Forms.Label();
64 | this.NudTraceSize = new System.Windows.Forms.NumericUpDown();
65 | this.ChkDateFiles = new System.Windows.Forms.CheckBox();
66 | this.ChkMultiFile = new System.Windows.Forms.CheckBox();
67 | this.TxtTraceDir = new System.Windows.Forms.TextBox();
68 | this.LblTraceDir = new System.Windows.Forms.Label();
69 | this.ButChooseFile = new System.Windows.Forms.Button();
70 | this.ButLogging = new System.Windows.Forms.Button();
71 | this.ChkIdentifyPCANUSB = new System.Windows.Forms.CheckBox();
72 | this.GrpInitialize.SuspendLayout();
73 | ((System.ComponentModel.ISupportInitialize)(this.NudLength)).BeginInit();
74 | this.GrpMessage.SuspendLayout();
75 | this.GrpReading.SuspendLayout();
76 | this.GrpTrace.SuspendLayout();
77 | ((System.ComponentModel.ISupportInitialize)(this.NudTraceSize)).BeginInit();
78 | this.SuspendLayout();
79 | //
80 | // LstDevices
81 | //
82 | this.LstDevices.FormattingEnabled = true;
83 | this.LstDevices.Location = new System.Drawing.Point(8, 23);
84 | this.LstDevices.Margin = new System.Windows.Forms.Padding(2);
85 | this.LstDevices.Name = "LstDevices";
86 | this.LstDevices.Size = new System.Drawing.Size(153, 95);
87 | this.LstDevices.TabIndex = 2;
88 | this.LstDevices.SelectedIndexChanged += new System.EventHandler(this.LstDevices_SelectedIndexChanged);
89 | //
90 | // GrpInitialize
91 | //
92 | this.GrpInitialize.Controls.Add(this.ChkIdentifyPCANUSB);
93 | this.GrpInitialize.Controls.Add(this.ChkTrace);
94 | this.GrpInitialize.Controls.Add(this.LstBaudRate);
95 | this.GrpInitialize.Controls.Add(this.ButStartStop);
96 | this.GrpInitialize.Controls.Add(this.LblBaudRate);
97 | this.GrpInitialize.Enabled = false;
98 | this.GrpInitialize.Location = new System.Drawing.Point(165, 9);
99 | this.GrpInitialize.Name = "GrpInitialize";
100 | this.GrpInitialize.Size = new System.Drawing.Size(176, 112);
101 | this.GrpInitialize.TabIndex = 3;
102 | this.GrpInitialize.TabStop = false;
103 | this.GrpInitialize.Text = "Initialize";
104 | //
105 | // ChkTrace
106 | //
107 | this.ChkTrace.AutoSize = true;
108 | this.ChkTrace.Enabled = false;
109 | this.ChkTrace.Location = new System.Drawing.Point(107, 65);
110 | this.ChkTrace.Name = "ChkTrace";
111 | this.ChkTrace.Size = new System.Drawing.Size(54, 17);
112 | this.ChkTrace.TabIndex = 7;
113 | this.ChkTrace.Text = "Trace";
114 | this.ChkTrace.UseVisualStyleBackColor = true;
115 | this.ChkTrace.CheckedChanged += new System.EventHandler(this.ChkTrace_CheckedChanged);
116 | //
117 | // LstBaudRate
118 | //
119 | this.LstBaudRate.FormattingEnabled = true;
120 | this.LstBaudRate.Location = new System.Drawing.Point(5, 39);
121 | this.LstBaudRate.Name = "LstBaudRate";
122 | this.LstBaudRate.Size = new System.Drawing.Size(97, 69);
123 | this.LstBaudRate.TabIndex = 5;
124 | //
125 | // ButStartStop
126 | //
127 | this.ButStartStop.Location = new System.Drawing.Point(107, 39);
128 | this.ButStartStop.Name = "ButStartStop";
129 | this.ButStartStop.Size = new System.Drawing.Size(50, 20);
130 | this.ButStartStop.TabIndex = 6;
131 | this.ButStartStop.Text = "Start";
132 | this.ButStartStop.UseVisualStyleBackColor = true;
133 | this.ButStartStop.Click += new System.EventHandler(this.ButStartStop_Click);
134 | //
135 | // LblBaudRate
136 | //
137 | this.LblBaudRate.Location = new System.Drawing.Point(5, 16);
138 | this.LblBaudRate.Name = "LblBaudRate";
139 | this.LblBaudRate.Size = new System.Drawing.Size(96, 20);
140 | this.LblBaudRate.TabIndex = 4;
141 | this.LblBaudRate.Text = "Select Baud Rate:";
142 | //
143 | // LstStatus
144 | //
145 | this.LstStatus.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
146 | | System.Windows.Forms.AnchorStyles.Right)));
147 | this.LstStatus.FormattingEnabled = true;
148 | this.LstStatus.Location = new System.Drawing.Point(8, 275);
149 | this.LstStatus.Name = "LstStatus";
150 | this.LstStatus.Size = new System.Drawing.Size(584, 69);
151 | this.LstStatus.TabIndex = 38;
152 | //
153 | // TxtData7
154 | //
155 | this.TxtData7.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
156 | this.TxtData7.Location = new System.Drawing.Point(215, 66);
157 | this.TxtData7.MaxLength = 2;
158 | this.TxtData7.Name = "TxtData7";
159 | this.TxtData7.Size = new System.Drawing.Size(24, 20);
160 | this.TxtData7.TabIndex = 22;
161 | this.TxtData7.Text = "00";
162 | this.TxtData7.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
163 | //
164 | // TxtData6
165 | //
166 | this.TxtData6.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
167 | this.TxtData6.Location = new System.Drawing.Point(185, 66);
168 | this.TxtData6.MaxLength = 2;
169 | this.TxtData6.Name = "TxtData6";
170 | this.TxtData6.Size = new System.Drawing.Size(24, 20);
171 | this.TxtData6.TabIndex = 21;
172 | this.TxtData6.Text = "00";
173 | this.TxtData6.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
174 | //
175 | // TxtData5
176 | //
177 | this.TxtData5.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
178 | this.TxtData5.Location = new System.Drawing.Point(155, 66);
179 | this.TxtData5.MaxLength = 2;
180 | this.TxtData5.Name = "TxtData5";
181 | this.TxtData5.Size = new System.Drawing.Size(24, 20);
182 | this.TxtData5.TabIndex = 20;
183 | this.TxtData5.Text = "00";
184 | this.TxtData5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
185 | //
186 | // TxtData4
187 | //
188 | this.TxtData4.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
189 | this.TxtData4.Location = new System.Drawing.Point(125, 66);
190 | this.TxtData4.MaxLength = 2;
191 | this.TxtData4.Name = "TxtData4";
192 | this.TxtData4.Size = new System.Drawing.Size(24, 20);
193 | this.TxtData4.TabIndex = 19;
194 | this.TxtData4.Text = "00";
195 | this.TxtData4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
196 | //
197 | // TxtData3
198 | //
199 | this.TxtData3.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
200 | this.TxtData3.Location = new System.Drawing.Point(95, 66);
201 | this.TxtData3.MaxLength = 2;
202 | this.TxtData3.Name = "TxtData3";
203 | this.TxtData3.Size = new System.Drawing.Size(24, 20);
204 | this.TxtData3.TabIndex = 18;
205 | this.TxtData3.Text = "00";
206 | this.TxtData3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
207 | //
208 | // TxtData2
209 | //
210 | this.TxtData2.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
211 | this.TxtData2.Location = new System.Drawing.Point(66, 66);
212 | this.TxtData2.MaxLength = 2;
213 | this.TxtData2.Name = "TxtData2";
214 | this.TxtData2.Size = new System.Drawing.Size(24, 20);
215 | this.TxtData2.TabIndex = 17;
216 | this.TxtData2.Text = "00";
217 | this.TxtData2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
218 | //
219 | // TxtData1
220 | //
221 | this.TxtData1.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
222 | this.TxtData1.Location = new System.Drawing.Point(36, 66);
223 | this.TxtData1.MaxLength = 2;
224 | this.TxtData1.Name = "TxtData1";
225 | this.TxtData1.Size = new System.Drawing.Size(24, 20);
226 | this.TxtData1.TabIndex = 16;
227 | this.TxtData1.Text = "00";
228 | this.TxtData1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
229 | //
230 | // TxtData0
231 | //
232 | this.TxtData0.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
233 | this.TxtData0.Location = new System.Drawing.Point(5, 66);
234 | this.TxtData0.MaxLength = 2;
235 | this.TxtData0.Name = "TxtData0";
236 | this.TxtData0.Size = new System.Drawing.Size(24, 20);
237 | this.TxtData0.TabIndex = 15;
238 | this.TxtData0.Text = "00";
239 | this.TxtData0.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
240 | //
241 | // NudLength
242 | //
243 | this.NudLength.BackColor = System.Drawing.Color.White;
244 | this.NudLength.Location = new System.Drawing.Point(125, 42);
245 | this.NudLength.Maximum = new decimal(new int[] {
246 | 8,
247 | 0,
248 | 0,
249 | 0});
250 | this.NudLength.Name = "NudLength";
251 | this.NudLength.ReadOnly = true;
252 | this.NudLength.Size = new System.Drawing.Size(41, 20);
253 | this.NudLength.TabIndex = 14;
254 | this.NudLength.Value = new decimal(new int[] {
255 | 8,
256 | 0,
257 | 0,
258 | 0});
259 | this.NudLength.ValueChanged += new System.EventHandler(this.NudLength_ValueChanged);
260 | //
261 | // LblNumBytes
262 | //
263 | this.LblNumBytes.AutoSize = true;
264 | this.LblNumBytes.Location = new System.Drawing.Point(2, 45);
265 | this.LblNumBytes.Name = "LblNumBytes";
266 | this.LblNumBytes.Size = new System.Drawing.Size(111, 13);
267 | this.LblNumBytes.TabIndex = 13;
268 | this.LblNumBytes.Text = "Number of data bytes:";
269 | //
270 | // TxtIdHex
271 | //
272 | this.TxtIdHex.Location = new System.Drawing.Point(123, 16);
273 | this.TxtIdHex.MaxLength = 3;
274 | this.TxtIdHex.Name = "TxtIdHex";
275 | this.TxtIdHex.Size = new System.Drawing.Size(73, 20);
276 | this.TxtIdHex.TabIndex = 12;
277 | this.TxtIdHex.Text = "F";
278 | this.TxtIdHex.TextChanged += new System.EventHandler(this.TxtIdHex_TextChanged);
279 | //
280 | // TxtId
281 | //
282 | this.TxtId.Location = new System.Drawing.Point(44, 16);
283 | this.TxtId.MaxLength = 4;
284 | this.TxtId.Name = "TxtId";
285 | this.TxtId.Size = new System.Drawing.Size(73, 20);
286 | this.TxtId.TabIndex = 11;
287 | this.TxtId.Text = "15";
288 | this.TxtId.TextChanged += new System.EventHandler(this.TxtId_TextChanged);
289 | //
290 | // LblCANId
291 | //
292 | this.LblCANId.AutoSize = true;
293 | this.LblCANId.Location = new System.Drawing.Point(2, 19);
294 | this.LblCANId.Name = "LblCANId";
295 | this.LblCANId.Size = new System.Drawing.Size(41, 13);
296 | this.LblCANId.TabIndex = 10;
297 | this.LblCANId.Text = "CAN Id";
298 | //
299 | // ButSend
300 | //
301 | this.ButSend.Location = new System.Drawing.Point(5, 88);
302 | this.ButSend.Margin = new System.Windows.Forms.Padding(2);
303 | this.ButSend.Name = "ButSend";
304 | this.ButSend.Size = new System.Drawing.Size(50, 19);
305 | this.ButSend.TabIndex = 23;
306 | this.ButSend.Text = "Send";
307 | this.ButSend.UseVisualStyleBackColor = true;
308 | this.ButSend.Click += new System.EventHandler(this.ButSend_Click);
309 | //
310 | // GrpMessage
311 | //
312 | this.GrpMessage.Controls.Add(this.LblCANId);
313 | this.GrpMessage.Controls.Add(this.ButSend);
314 | this.GrpMessage.Controls.Add(this.TxtData0);
315 | this.GrpMessage.Controls.Add(this.TxtIdHex);
316 | this.GrpMessage.Controls.Add(this.TxtData1);
317 | this.GrpMessage.Controls.Add(this.TxtId);
318 | this.GrpMessage.Controls.Add(this.TxtData2);
319 | this.GrpMessage.Controls.Add(this.TxtData3);
320 | this.GrpMessage.Controls.Add(this.LblNumBytes);
321 | this.GrpMessage.Controls.Add(this.TxtData4);
322 | this.GrpMessage.Controls.Add(this.NudLength);
323 | this.GrpMessage.Controls.Add(this.TxtData5);
324 | this.GrpMessage.Controls.Add(this.TxtData7);
325 | this.GrpMessage.Controls.Add(this.TxtData6);
326 | this.GrpMessage.Enabled = false;
327 | this.GrpMessage.Location = new System.Drawing.Point(347, 9);
328 | this.GrpMessage.Name = "GrpMessage";
329 | this.GrpMessage.Size = new System.Drawing.Size(245, 112);
330 | this.GrpMessage.TabIndex = 9;
331 | this.GrpMessage.TabStop = false;
332 | this.GrpMessage.Text = "Transmit Message";
333 | //
334 | // LblStatus
335 | //
336 | this.LblStatus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
337 | this.LblStatus.AutoSize = true;
338 | this.LblStatus.Location = new System.Drawing.Point(5, 259);
339 | this.LblStatus.Name = "LblStatus";
340 | this.LblStatus.Size = new System.Drawing.Size(95, 13);
341 | this.LblStatus.TabIndex = 37;
342 | this.LblStatus.Text = "System Messages:";
343 | //
344 | // GrpReading
345 | //
346 | this.GrpReading.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
347 | | System.Windows.Forms.AnchorStyles.Left)
348 | | System.Windows.Forms.AnchorStyles.Right)));
349 | this.GrpReading.Controls.Add(this.ChkOverwrite);
350 | this.GrpReading.Controls.Add(this.LstReceivedMessages);
351 | this.GrpReading.Controls.Add(this.ButClear);
352 | this.GrpReading.Enabled = false;
353 | this.GrpReading.Location = new System.Drawing.Point(8, 177);
354 | this.GrpReading.Name = "GrpReading";
355 | this.GrpReading.Size = new System.Drawing.Size(584, 77);
356 | this.GrpReading.TabIndex = 33;
357 | this.GrpReading.TabStop = false;
358 | this.GrpReading.Text = "Received Messages";
359 | //
360 | // ChkOverwrite
361 | //
362 | this.ChkOverwrite.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
363 | this.ChkOverwrite.AutoSize = true;
364 | this.ChkOverwrite.Checked = true;
365 | this.ChkOverwrite.CheckState = System.Windows.Forms.CheckState.Checked;
366 | this.ChkOverwrite.Location = new System.Drawing.Point(59, 53);
367 | this.ChkOverwrite.Margin = new System.Windows.Forms.Padding(2);
368 | this.ChkOverwrite.Name = "ChkOverwrite";
369 | this.ChkOverwrite.Size = new System.Drawing.Size(71, 17);
370 | this.ChkOverwrite.TabIndex = 36;
371 | this.ChkOverwrite.Text = "Overwrite";
372 | this.ChkOverwrite.UseVisualStyleBackColor = true;
373 | this.ChkOverwrite.CheckedChanged += new System.EventHandler(this.ChkOverwrite_CheckedChanged);
374 | //
375 | // LstReceivedMessages
376 | //
377 | this.LstReceivedMessages.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
378 | | System.Windows.Forms.AnchorStyles.Left)
379 | | System.Windows.Forms.AnchorStyles.Right)));
380 | this.LstReceivedMessages.FormattingEnabled = true;
381 | this.LstReceivedMessages.Location = new System.Drawing.Point(7, 20);
382 | this.LstReceivedMessages.Name = "LstReceivedMessages";
383 | this.LstReceivedMessages.Size = new System.Drawing.Size(572, 30);
384 | this.LstReceivedMessages.TabIndex = 34;
385 | //
386 | // ButClear
387 | //
388 | this.ButClear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
389 | this.ButClear.Enabled = false;
390 | this.ButClear.Location = new System.Drawing.Point(5, 53);
391 | this.ButClear.Margin = new System.Windows.Forms.Padding(2);
392 | this.ButClear.Name = "ButClear";
393 | this.ButClear.Size = new System.Drawing.Size(50, 19);
394 | this.ButClear.TabIndex = 35;
395 | this.ButClear.Text = "Clear";
396 | this.ButClear.UseVisualStyleBackColor = true;
397 | this.ButClear.Click += new System.EventHandler(this.ButClear_Click);
398 | //
399 | // TimerCheckForDevice
400 | //
401 | this.TimerCheckForDevice.Interval = 500;
402 | this.TimerCheckForDevice.Tick += new System.EventHandler(this.TimerCheckForDevice_Tick);
403 | //
404 | // LblListOfDevices
405 | //
406 | this.LblListOfDevices.AutoSize = true;
407 | this.LblListOfDevices.Location = new System.Drawing.Point(8, 8);
408 | this.LblListOfDevices.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
409 | this.LblListOfDevices.Name = "LblListOfDevices";
410 | this.LblListOfDevices.Size = new System.Drawing.Size(106, 13);
411 | this.LblListOfDevices.TabIndex = 1;
412 | this.LblListOfDevices.Text = "PCAN USB Devices:";
413 | //
414 | // GrpTrace
415 | //
416 | this.GrpTrace.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
417 | | System.Windows.Forms.AnchorStyles.Right)));
418 | this.GrpTrace.Controls.Add(this.LblMaxSize);
419 | this.GrpTrace.Controls.Add(this.NudTraceSize);
420 | this.GrpTrace.Controls.Add(this.ChkDateFiles);
421 | this.GrpTrace.Controls.Add(this.ChkMultiFile);
422 | this.GrpTrace.Controls.Add(this.TxtTraceDir);
423 | this.GrpTrace.Controls.Add(this.LblTraceDir);
424 | this.GrpTrace.Controls.Add(this.ButChooseFile);
425 | this.GrpTrace.Controls.Add(this.ButLogging);
426 | this.GrpTrace.Enabled = false;
427 | this.GrpTrace.Location = new System.Drawing.Point(8, 123);
428 | this.GrpTrace.Name = "GrpTrace";
429 | this.GrpTrace.Size = new System.Drawing.Size(584, 48);
430 | this.GrpTrace.TabIndex = 24;
431 | this.GrpTrace.TabStop = false;
432 | this.GrpTrace.Text = "Trace Settings";
433 | //
434 | // LblMaxSize
435 | //
436 | this.LblMaxSize.AutoSize = true;
437 | this.LblMaxSize.Location = new System.Drawing.Point(192, 20);
438 | this.LblMaxSize.Name = "LblMaxSize";
439 | this.LblMaxSize.Size = new System.Drawing.Size(54, 13);
440 | this.LblMaxSize.TabIndex = 27;
441 | this.LblMaxSize.Text = "Max. MBs";
442 | //
443 | // NudTraceSize
444 | //
445 | this.NudTraceSize.Location = new System.Drawing.Point(252, 18);
446 | this.NudTraceSize.Name = "NudTraceSize";
447 | this.NudTraceSize.Size = new System.Drawing.Size(45, 20);
448 | this.NudTraceSize.TabIndex = 28;
449 | this.NudTraceSize.Value = new decimal(new int[] {
450 | 100,
451 | 0,
452 | 0,
453 | 0});
454 | //
455 | // ChkDateFiles
456 | //
457 | this.ChkDateFiles.AutoSize = true;
458 | this.ChkDateFiles.Checked = true;
459 | this.ChkDateFiles.CheckState = System.Windows.Forms.CheckState.Checked;
460 | this.ChkDateFiles.Location = new System.Drawing.Point(96, 19);
461 | this.ChkDateFiles.Name = "ChkDateFiles";
462 | this.ChkDateFiles.Size = new System.Drawing.Size(89, 17);
463 | this.ChkDateFiles.TabIndex = 26;
464 | this.ChkDateFiles.Text = "Date in name";
465 | this.ChkDateFiles.UseVisualStyleBackColor = true;
466 | //
467 | // ChkMultiFile
468 | //
469 | this.ChkMultiFile.AutoSize = true;
470 | this.ChkMultiFile.Checked = true;
471 | this.ChkMultiFile.CheckState = System.Windows.Forms.CheckState.Checked;
472 | this.ChkMultiFile.Location = new System.Drawing.Point(6, 19);
473 | this.ChkMultiFile.Name = "ChkMultiFile";
474 | this.ChkMultiFile.Size = new System.Drawing.Size(83, 17);
475 | this.ChkMultiFile.TabIndex = 25;
476 | this.ChkMultiFile.Text = "Multiple files";
477 | this.ChkMultiFile.UseVisualStyleBackColor = true;
478 | //
479 | // TxtTraceDir
480 | //
481 | this.TxtTraceDir.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
482 | | System.Windows.Forms.AnchorStyles.Right)));
483 | this.TxtTraceDir.Location = new System.Drawing.Point(324, 17);
484 | this.TxtTraceDir.Name = "TxtTraceDir";
485 | this.TxtTraceDir.Size = new System.Drawing.Size(162, 20);
486 | this.TxtTraceDir.TabIndex = 30;
487 | this.TxtTraceDir.Text = "\\Temp";
488 | //
489 | // LblTraceDir
490 | //
491 | this.LblTraceDir.AutoSize = true;
492 | this.LblTraceDir.Location = new System.Drawing.Point(303, 20);
493 | this.LblTraceDir.Name = "LblTraceDir";
494 | this.LblTraceDir.Size = new System.Drawing.Size(23, 13);
495 | this.LblTraceDir.TabIndex = 29;
496 | this.LblTraceDir.Text = "Dir:";
497 | //
498 | // ButChooseFile
499 | //
500 | this.ButChooseFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
501 | this.ButChooseFile.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
502 | this.ButChooseFile.Location = new System.Drawing.Point(492, 17);
503 | this.ButChooseFile.Name = "ButChooseFile";
504 | this.ButChooseFile.Size = new System.Drawing.Size(30, 20);
505 | this.ButChooseFile.TabIndex = 31;
506 | this.ButChooseFile.Text = "...";
507 | this.ButChooseFile.UseVisualStyleBackColor = true;
508 | this.ButChooseFile.Click += new System.EventHandler(this.ButChooseFile_Click);
509 | //
510 | // ButLogging
511 | //
512 | this.ButLogging.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
513 | this.ButLogging.Location = new System.Drawing.Point(528, 17);
514 | this.ButLogging.Name = "ButLogging";
515 | this.ButLogging.Size = new System.Drawing.Size(50, 20);
516 | this.ButLogging.TabIndex = 32;
517 | this.ButLogging.Text = "Start";
518 | this.ButLogging.UseVisualStyleBackColor = true;
519 | this.ButLogging.Click += new System.EventHandler(this.ButLogging_Click);
520 | //
521 | // ChkIdentifyPCANUSB
522 | //
523 | this.ChkIdentifyPCANUSB.AutoSize = true;
524 | this.ChkIdentifyPCANUSB.Location = new System.Drawing.Point(107, 88);
525 | this.ChkIdentifyPCANUSB.Name = "ChkIdentifyPCANUSB";
526 | this.ChkIdentifyPCANUSB.Size = new System.Drawing.Size(60, 17);
527 | this.ChkIdentifyPCANUSB.TabIndex = 8;
528 | this.ChkIdentifyPCANUSB.Text = "I&dentify";
529 | this.ChkIdentifyPCANUSB.UseVisualStyleBackColor = true;
530 | this.ChkIdentifyPCANUSB.CheckStateChanged += new System.EventHandler(this.ChkIdentifyPCANUSB_CheckStateChanged);
531 | //
532 | // FrmPCTest
533 | //
534 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
535 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
536 | this.ClientSize = new System.Drawing.Size(599, 346);
537 | this.Controls.Add(this.GrpTrace);
538 | this.Controls.Add(this.LblListOfDevices);
539 | this.Controls.Add(this.GrpReading);
540 | this.Controls.Add(this.LblStatus);
541 | this.Controls.Add(this.GrpMessage);
542 | this.Controls.Add(this.LstStatus);
543 | this.Controls.Add(this.GrpInitialize);
544 | this.Controls.Add(this.LstDevices);
545 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
546 | this.Margin = new System.Windows.Forms.Padding(2);
547 | this.MinimumSize = new System.Drawing.Size(584, 385);
548 | this.Name = "FrmPCTest";
549 | this.Text = "PCAN USB Tester";
550 | this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmPCTest_FormClosing);
551 | this.Load += new System.EventHandler(this.FrmPCTest_Load);
552 | this.GrpInitialize.ResumeLayout(false);
553 | this.GrpInitialize.PerformLayout();
554 | ((System.ComponentModel.ISupportInitialize)(this.NudLength)).EndInit();
555 | this.GrpMessage.ResumeLayout(false);
556 | this.GrpMessage.PerformLayout();
557 | this.GrpReading.ResumeLayout(false);
558 | this.GrpReading.PerformLayout();
559 | this.GrpTrace.ResumeLayout(false);
560 | this.GrpTrace.PerformLayout();
561 | ((System.ComponentModel.ISupportInitialize)(this.NudTraceSize)).EndInit();
562 | this.ResumeLayout(false);
563 | this.PerformLayout();
564 |
565 | }
566 |
567 | #endregion
568 | private System.Windows.Forms.ListBox LstDevices;
569 | private System.Windows.Forms.GroupBox GrpInitialize;
570 | private System.Windows.Forms.Button ButStartStop;
571 | private System.Windows.Forms.Label LblBaudRate;
572 | private System.Windows.Forms.ListBox LstBaudRate;
573 | private System.Windows.Forms.ListBox LstStatus;
574 | private System.Windows.Forms.TextBox TxtData7;
575 | private System.Windows.Forms.TextBox TxtData6;
576 | private System.Windows.Forms.TextBox TxtData5;
577 | private System.Windows.Forms.TextBox TxtData4;
578 | private System.Windows.Forms.TextBox TxtData3;
579 | private System.Windows.Forms.TextBox TxtData2;
580 | private System.Windows.Forms.TextBox TxtData1;
581 | private System.Windows.Forms.TextBox TxtData0;
582 | private System.Windows.Forms.NumericUpDown NudLength;
583 | private System.Windows.Forms.Label LblNumBytes;
584 | private System.Windows.Forms.TextBox TxtIdHex;
585 | private System.Windows.Forms.TextBox TxtId;
586 | private System.Windows.Forms.Label LblCANId;
587 | private System.Windows.Forms.Button ButSend;
588 | private System.Windows.Forms.GroupBox GrpMessage;
589 | private System.Windows.Forms.Label LblStatus;
590 | private System.Windows.Forms.GroupBox GrpReading;
591 | private System.Windows.Forms.Button ButClear;
592 | private System.Windows.Forms.ListBox LstReceivedMessages;
593 | private System.Windows.Forms.Timer TimerCheckForDevice;
594 | private System.Windows.Forms.Label LblListOfDevices;
595 | private System.Windows.Forms.CheckBox ChkOverwrite;
596 | private System.Windows.Forms.CheckBox ChkTrace;
597 | private System.Windows.Forms.GroupBox GrpTrace;
598 | private System.Windows.Forms.Label LblMaxSize;
599 | private System.Windows.Forms.NumericUpDown NudTraceSize;
600 | private System.Windows.Forms.CheckBox ChkDateFiles;
601 | private System.Windows.Forms.CheckBox ChkMultiFile;
602 | private System.Windows.Forms.TextBox TxtTraceDir;
603 | private System.Windows.Forms.Label LblTraceDir;
604 | private System.Windows.Forms.Button ButChooseFile;
605 | private System.Windows.Forms.Button ButLogging;
606 | private System.Windows.Forms.CheckBox ChkIdentifyPCANUSB;
607 | }
608 | }
609 |
610 |
--------------------------------------------------------------------------------
/PCAN_USB/PCAN-USB.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Threading;
5 | using System.Windows.Forms;
6 | using System.Globalization;
7 | using System.IO;
8 |
9 | namespace CAN.PC
10 | {
11 | //PEAK handle data type
12 | using PCANHandle = UInt16;
13 |
14 | ///
15 | /// Used to provide a DLL to link from a C# program to the PEAK PCAN USB Devices.
16 | /// Requires the PCANBasic.cs file from the PEAK PCAN samples.
17 | /// Some code is derived from the PCAN C# sample.
18 | /// The PEAK PCAN drivers musts be installed on the target machine.
19 | ///
20 | public class PCAN_USB
21 | {
22 | #region PEAK USB devices, and handle definition and storage
23 | ///
24 | /// Define handles for PEAK USB devices
25 | ///
26 | static PCANHandle[] handlesArray = new PCANHandle[]
27 | {
28 | PCANBasic.PCAN_USBBUS1,
29 | PCANBasic.PCAN_USBBUS2,
30 | PCANBasic.PCAN_USBBUS3,
31 | PCANBasic.PCAN_USBBUS4,
32 | PCANBasic.PCAN_USBBUS5,
33 | PCANBasic.PCAN_USBBUS6,
34 | PCANBasic.PCAN_USBBUS7,
35 | PCANBasic.PCAN_USBBUS8,
36 | PCANBasic.PCAN_USBBUS9,
37 | PCANBasic.PCAN_USBBUS10,
38 | PCANBasic.PCAN_USBBUS11,
39 | PCANBasic.PCAN_USBBUS12,
40 | PCANBasic.PCAN_USBBUS13,
41 | PCANBasic.PCAN_USBBUS14,
42 | PCANBasic.PCAN_USBBUS15,
43 | PCANBasic.PCAN_USBBUS16
44 | };
45 | ///
46 | /// The handled of the selected PEAK USB device
47 | ///
48 | public PCANHandle PeakCANHandle { get; set; } = 0;
49 | ///
50 | /// Get a list of connected devices
51 | ///
52 | /// List of devices
53 | public static List GetUSBDevices()
54 | {
55 | //Uses the PEAK System USB1 to USB16 handles for the PCAN-USB devices
56 | TPCANStatus dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
57 | UInt32 iBuffer; //Passed to PCAN dll in GetValue call
58 | List PCANDevices = null; //The list of devices to return
59 |
60 | bool isFD; //CAN Flexible Data Rate (not currently supported by PCAN_USB)
61 |
62 | for (int i = 0; i < handlesArray.Length; i++)
63 | {
64 | // Checks for a Plug&Play Handle and, depending upon the return value, include it
65 | // into the list of available hardware channels.
66 | dllRet = PCANBasic.GetValue(handlesArray[i], TPCANParameter.PCAN_CHANNEL_CONDITION, out iBuffer, sizeof(UInt32));
67 | if ((dllRet == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.PCAN_CHANNEL_AVAILABLE) == PCANBasic.PCAN_CHANNEL_AVAILABLE || (iBuffer & PCANBasic.PCAN_CHANNEL_PCANVIEW) == PCANBasic.PCAN_CHANNEL_PCANVIEW))
68 | {
69 | dllRet = PCANBasic.GetValue(handlesArray[i], TPCANParameter.PCAN_CHANNEL_FEATURES, out iBuffer, sizeof(UInt32));
70 | isFD = (dllRet == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.FEATURE_FD_CAPABLE) == PCANBasic.FEATURE_FD_CAPABLE);
71 | if (PCANDevices == null)
72 | PCANDevices = new List();
73 | PCANDevices.Add(FormatChannelName(handlesArray[i], isFD));
74 | }
75 | }
76 | return PCANDevices;
77 | }
78 | ///
79 | /// PEAK format text for a PCAN-Basic channel handle
80 | ///
81 | /// PCAN-Basic Handle to format
82 | /// If the channel is FD capable
83 | /// The formatted text for a channel
84 | private static string FormatChannelName(PCANHandle handle, bool isFD)
85 | {
86 | TPCANDevice devDevice;
87 | byte byChannel;
88 |
89 | // Gets the owner device and channel for a
90 | // PCAN-Basic handle
91 | if (handle < 0x100)
92 | {
93 | devDevice = (TPCANDevice)(handle >> 4);
94 | byChannel = (byte)(handle & 0xF);
95 | }
96 | else
97 | {
98 | devDevice = (TPCANDevice)(handle >> 8);
99 | byChannel = (byte)(handle & 0xFF);
100 | }
101 | // Constructs the PCAN-Basic Channel name and return it
102 | if (isFD)
103 | return string.Format("{0}:FD {1} ({2:X2}h)", devDevice, byChannel, handle);
104 | else
105 | return string.Format("{0} {1} ({2:X2}h)", devDevice, byChannel, handle);
106 | }
107 | ///
108 | /// Returns the PCAN USB handle for a given PEAK displayed handle
109 | ///
110 | /// Displayed handle string
111 | /// PCAN USB handle, 0 if no string provided or if string incorrect
112 | public static PCANHandle DecodePEAKHandle(string PEAKListHandle)
113 | {
114 | if(!(string.IsNullOrEmpty(PEAKListHandle) || PEAKListHandle.Trim()==string.Empty))
115 | {
116 | // Get the handle from the text being shown
117 | PEAKListHandle = PEAKListHandle.Substring(PEAKListHandle.IndexOf('(') + 1, 3);
118 | PEAKListHandle = PEAKListHandle.Replace('h', ' ').Trim(' ');
119 | PCANHandle handle = 0;
120 | if (!PCANHandle.TryParse(PEAKListHandle, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out handle))
121 | handle = 0;
122 | return handle;
123 | }
124 | else
125 | {
126 | return 0;
127 | }
128 | }
129 | #endregion
130 |
131 | #region Identify device (flashes LED if supported)
132 | ///
133 | /// Get identify setting from PCAN USB device
134 | ///
135 | /// PCAN USB device handle
136 | /// true if identifying is on for the device, else false
137 | public static bool IsIdentifyOn(PCANHandle PCANUSBHandle)
138 | {
139 | TPCANStatus dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
140 | UInt32 iBuffer; //Passed to PCAN dll in GetValue call
141 |
142 | dllRet = PCANBasic.GetValue(PCANUSBHandle, TPCANParameter.PCAN_CHANNEL_IDENTIFYING, out iBuffer, sizeof(UInt32));
143 | if (dllRet == TPCANStatus.PCAN_ERROR_OK && ((iBuffer & PCANBasic.PCAN_PARAMETER_ON) == PCANBasic.PCAN_PARAMETER_ON))
144 | return true;
145 | else
146 | return false;
147 |
148 | }
149 |
150 | ///
151 | /// Set the identify setting for a PCAN USB device
152 | ///
153 | /// PCAN USB device handle
154 | /// identify on or off
155 | /// true if setting was made correctly, else false if it failed
156 | public static bool SetIdentify(PCANHandle PCANUSBHandle, bool OnOff)
157 | {
158 | TPCANStatus dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
159 | UInt32 iBuffer; //Passed to PCAN dll in GetValue call
160 |
161 | if (OnOff)
162 | iBuffer = PCANBasic.PCAN_PARAMETER_ON;
163 | else
164 | iBuffer = PCANBasic.PCAN_PARAMETER_OFF;
165 |
166 | dllRet = PCANBasic.SetValue(PCANUSBHandle, TPCANParameter.PCAN_CHANNEL_IDENTIFYING, ref iBuffer, sizeof(UInt32));
167 | if (dllRet == TPCANStatus.PCAN_ERROR_OK)
168 | return true;
169 | else
170 | return false;
171 |
172 | }
173 | #endregion
174 |
175 | #region Set the user defined device number (not the PEAK id)
176 | //PCAN_DEVICE_NUMBER
177 | #endregion
178 |
179 | #region The UI list boxes for UI message and CAN message rx updates
180 | ///
181 | /// Feeback any status messages to a UI
182 | ///
183 | public ListBox Feedback { get; set; } = null;
184 | ///
185 | /// For showing the received messages
186 | ///
187 | public ListBox ReceivedMessages { get; set; } = null;
188 |
189 | //First packet read
190 | public Packet DiffPacket { get; set; } = null;
191 |
192 | ///
193 | /// Add a message to a UI listbox
194 | ///
195 | /// Use this list box
196 | /// Add this message
197 | /// Index of the added item or -1
198 | private static int AddMessage(ListBox AddTo, string MessageToAdd)
199 | {
200 | int ret = -1;
201 |
202 | if (AddTo != null)
203 | {
204 | //Limit number of items
205 | if (AddTo.Items.Count >= 60000)
206 | AddTo.Items.RemoveAt(0);
207 | ret=AddTo.Items.Add(MessageToAdd);
208 | //ensure new item is visible
209 | AddTo.TopIndex = AddTo.Items.Count - 1;
210 | return ret;
211 | }
212 |
213 | return ret;
214 | }
215 | ///
216 | ///
217 | ///
218 | /// Use this list box
219 | /// Add this message
220 | /// Index of item to replace
221 | /// Index of the replaced item or -1
222 | private int AddMessage(ListBox AddTo, string MessageToAdd, int IndexToReplace)
223 | {
224 | if (AddTo != null)
225 | {
226 | if (IndexToReplace > -1 && IndexToReplace < AddTo.Items.Count)
227 | {
228 | //Replace item
229 | AddTo.Items[IndexToReplace] = MessageToAdd;
230 | return IndexToReplace;
231 | }
232 | else
233 | {
234 | //Limit number of items
235 | if (AddTo.Items.Count >= 60000)
236 | AddTo.Items.RemoveAt(0);
237 | //Replacing so no need to move current view.
238 | return AddTo.Items.Add(MessageToAdd);
239 | }
240 | }
241 | else
242 | return -1;
243 | }
244 | #endregion
245 |
246 | #region Hardware status
247 | ///
248 | /// Store the last PEAK PCAN USB operation status result
249 | ///
250 | public TPCANStatus LastOperationStatus = TPCANStatus.PCAN_ERROR_UNKNOWN;
251 | ///
252 | /// Help Function used to get an error as text
253 | ///
254 | /// Error code to be translated
255 | /// A text with the translated error
256 | public string PeakCANStatusErrorString(TPCANStatus error)
257 | {
258 | // Creates a buffer big enough for a error-text
259 | StringBuilder strTemp = new StringBuilder(256);
260 |
261 | // Gets the text using the GetErrorText API function
262 | // If the function success, the translated error is returned. If it fails,
263 | // a text describing the current error is returned.
264 | //
265 | if (PCANBasic.GetErrorText(error, 0, strTemp) != TPCANStatus.PCAN_ERROR_OK)
266 | return string.Format("An error occurred. Error-code's text ({0:X}) couldn't be retrieved", error);
267 | else
268 | {
269 | return strTemp.ToString();
270 | }
271 | }
272 | ///
273 | /// If possible display the last operation result message if required
274 | ///
275 | private void LastOperationErrorMessage()
276 | {
277 | if (Feedback != null)
278 | {
279 | AddMessage(Feedback, PeakCANStatusErrorString(LastOperationStatus));
280 | }
281 | }
282 | #endregion
283 |
284 | #region Valid Baud Rates
285 | ///
286 | /// PCAN USB supported baud rates
287 | ///
288 | public static string[] CANBaudRates = {"5 kbit/s", "10 kbit/s", "20 kbit/s", "33.333 kbit/s", "47.619 kbit/s", "50 kbit/s", "83.333 kbit/s",
289 | "95.238 kbit/s", "100 kbit/s", "125 kbit/s", "250 kbit/s", "500 kbit/s", "800 kbit/s", "1 Mbit/s" };
290 | ///
291 | /// Convert as readable baud rate to a PCAN USB baud rate.
292 | /// See CANBaudRates for values.
293 | ///
294 | /// Baud rate x kbit/s or Mbit/s
295 | /// PEAK baud rate value, 5 kbit/s setting if string not recognised
296 | TPCANBaudrate CANBaudRateToPeakCANBaudRate(string CANBaudRate)
297 | {
298 | if (CANBaudRate == "1 Mbit/s")
299 | return TPCANBaudrate.PCAN_BAUD_1M;
300 | else if (CANBaudRate == "800 kbit/s")
301 | return TPCANBaudrate.PCAN_BAUD_800K;
302 | else if (CANBaudRate == "500 kbit/s")
303 | return TPCANBaudrate.PCAN_BAUD_500K;
304 | else if (CANBaudRate == "250 kbit/s")
305 | return TPCANBaudrate.PCAN_BAUD_250K;
306 | else if (CANBaudRate == "125 kbit/s")
307 | return TPCANBaudrate.PCAN_BAUD_125K;
308 | else if (CANBaudRate == "100 kbit/s")
309 | return TPCANBaudrate.PCAN_BAUD_100K;
310 | else if (CANBaudRate == "95.238 kbit/s")
311 | return TPCANBaudrate.PCAN_BAUD_95K;
312 | else if (CANBaudRate == "83.333 kbit/s")
313 | return TPCANBaudrate.PCAN_BAUD_83K;
314 | else if (CANBaudRate == "50 kbit/s")
315 | return TPCANBaudrate.PCAN_BAUD_50K;
316 | else if (CANBaudRate == "47.619 kbit/s")
317 | return TPCANBaudrate.PCAN_BAUD_47K;
318 | else if (CANBaudRate == "33.333 kbit/s")
319 | return TPCANBaudrate.PCAN_BAUD_33K;
320 | else if (CANBaudRate == "20 kbit/s")
321 | return TPCANBaudrate.PCAN_BAUD_20K;
322 | else if (CANBaudRate == "10 kbit/s")
323 | return TPCANBaudrate.PCAN_BAUD_10K;
324 | else return TPCANBaudrate.PCAN_BAUD_5K;
325 | }
326 | #endregion
327 |
328 | #region Storage for captured packets
329 | ///
330 | /// A CAN data packet class
331 | ///
332 | public class Packet
333 | {
334 | //PEAK uses a struct for passing to the PEAK dll
335 | public ulong Microseconds { set; get; }
336 | public uint Id { set; get; }
337 | public byte Length { set; get; }
338 | public byte[] Data { set; get; }
339 | //Index for displaying in list boxes
340 | public int DisplayIndex { get; set; } = -1;
341 |
342 | public static Packet Clone(Packet PacketToClone)
343 | {
344 | Packet pkt = new Packet();
345 | pkt.Microseconds = PacketToClone.Microseconds;
346 | pkt.Id = PacketToClone.Id;
347 | pkt.Length = PacketToClone.Length;
348 | pkt.Data = PacketToClone.Data.Clone() as byte[];
349 | pkt.DisplayIndex = PacketToClone.DisplayIndex;
350 |
351 | return pkt;
352 | }
353 |
354 | }
355 | ///
356 | /// List of all packets
357 | ///
358 | public List Packets { get; set; } = new List();
359 | ///
360 | /// Current packet being operated on
361 | ///
362 | Packet CurrentPacket { get; set; }
363 | //local StringBuilder for the packet to string function
364 | static StringBuilder sb;
365 | ///
366 | /// Convert a CAN packet to a displayable string
367 | ///
368 | ///
369 | ///
370 | public static string PacketToString(Packet ThisPacket)
371 | {
372 | sb = new StringBuilder();
373 |
374 | sb.Append((ThisPacket.Microseconds / 1000000.0d).ToString("F6", CultureInfo.InvariantCulture));
375 | sb.Append(' ');
376 | sb.Append(ThisPacket.Id);
377 | sb.Append(' ');
378 | sb.Append(ThisPacket.Length);
379 | sb.Append(' ');
380 | for (int i = 0; i < ThisPacket.Length; i++)
381 | {
382 | sb.Append(" " + ThisPacket.Data[i].ToString("X2"));
383 | }
384 |
385 | return sb.ToString();
386 | }
387 | //Local for the capture mode
388 | bool overwriteLastPacket;
389 | ///
390 | /// Either overwrite packets of same id or add to end of storage
391 | ///
392 | public bool OverwriteLastPacket
393 | {
394 | get { return overwriteLastPacket; }
395 | set
396 | {
397 | if (value != overwriteLastPacket)
398 | {
399 | overwriteLastPacket = value;
400 | Packets = new List();
401 | }
402 | }
403 | }
404 | ///
405 | /// Store a captured CAN packet
406 | ///
407 | /// Time registered by PEAK device
408 | /// Id of the packet
409 | /// Length of data
410 | /// Captured data
411 | /// Reference to the packet
412 | Packet UpdatePackets(ulong TimeInMicroseconds, uint CANId, byte DataLength, byte[] Data)
413 | {
414 | Packet packet;
415 |
416 | if (overwriteLastPacket)
417 | {
418 | //Update packet list by overwriting last value
419 | packet = Packets.Find(x => x.Id == CANId);
420 | if (packet == null)
421 | {
422 | packet = new Packet();
423 | Packets.Add(packet);
424 | }
425 | }
426 | else
427 | {
428 | //store as new values
429 | packet = new Packet();
430 | Packets.Add(packet);
431 | }
432 | //add/update values
433 | packet.Microseconds = TimeInMicroseconds;
434 | packet.Id = CANId;
435 | packet.Length = DataLength;
436 | packet.Data = Data;
437 |
438 | //remember first packet for diff calc
439 | if (DiffPacket==null)
440 | {
441 | AddMessage(Feedback, "First packet received " + PacketToString(packet));
442 | DiffPacket = Packet.Clone(packet);
443 | }
444 | return packet;
445 | }
446 | #endregion
447 |
448 | #region Specific Packet Monitoring
449 | public bool WatchForPackets = false;
450 | ///
451 | /// List of packets to watch out for
452 | ///
453 | public List WatchPackets { get; set; } = new List();
454 |
455 | public void SetWatchPackets()
456 | {
457 | Packet aPacket = new Packet();
458 | byte[] data = { 1 };
459 | aPacket.Id = 534;
460 | aPacket.Length = 1;
461 | aPacket.Data = data;
462 | WatchPackets.Add(aPacket);
463 | /*
464 | aPacket = new Packet();
465 | data = new byte[]{ 1 };
466 | aPacket.Id = 534;
467 | aPacket.Length = 1;
468 | aPacket.Data = data;
469 | WatchPackets.Add(aPacket);
470 | */ }
471 |
472 | public bool FoundPacket()
473 | {
474 | bool ret = false;
475 |
476 | foreach (Packet packet in WatchPackets)
477 | {
478 | if (packet.Id == CurrentPacket.Id)
479 | {
480 | if (packet.Length == CurrentPacket.Length)
481 | {
482 | for (int i = 0; i < packet.Length && i < CurrentPacket.Length; i++)
483 | {
484 | if (packet.Data[i] == CurrentPacket.Data[i])
485 | ret = true;
486 | else
487 | ret = false;
488 | }
489 | if (ret)
490 | //found exact match, exit
491 | break;
492 | }
493 | }
494 | }
495 |
496 | return ret;
497 | }
498 | #endregion
499 |
500 | #region The background CAN message reading thread
501 | ///
502 | /// Thread for message reading (using events)
503 | ///
504 | private Thread ReadThread;
505 | ///
506 | /// Read-Delegate Handler
507 | ///
508 | private delegate void ReadDelegateHandler();
509 | ///
510 | /// Local control of read thread.
511 | /// Set on call to SetCANMessageRead.
512 | ///
513 | bool RxMessages { get; set; } = false; //Set true to read messages
514 | ///
515 | /// Read Delegate for calling the function "ReadMessage"
516 | ///
517 | private ReadDelegateHandler ReadDelegate;
518 | ///
519 | /// Receive-Event
520 | ///
521 | private AutoResetEvent ReceiveEvent;
522 | ///
523 | /// The form for CAN read delegate invoke
524 | ///
525 | public Form ControlForm { get; set; } = null;
526 | ///
527 | /// Function for reading CAN messages on normal CAN devices
528 | /// Set to the ReadDelegate
529 | ///
530 | /// A TPCANStatus error code
531 | private void ReadMessage()
532 | {
533 | //TODO move to moduel level for performance
534 | TPCANMsg CANMsg;
535 | TPCANTimestamp CANTimeStamp;
536 | TPCANStatus stsResult;
537 | ulong timeInMicroseconds;
538 |
539 | //Read till buffer empty
540 | do
541 | {
542 | // The "Read" function of PCANBasic
543 | stsResult = PCANBasic.Read(PeakCANHandle, out CANMsg, out CANTimeStamp);
544 | if (stsResult != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
545 | {
546 | //Get the PEAK packet time in
547 | timeInMicroseconds = Convert.ToUInt64(CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow);
548 | //Store the read in packet and get a reference to it.
549 | CurrentPacket = UpdatePackets(timeInMicroseconds, CANMsg.ID, CANMsg.LEN, CANMsg.DATA);
550 | //Display it if possible, either over existing line to new line
551 | if (CurrentPacket.DisplayIndex > -1)
552 | AddMessage(ReceivedMessages, PacketToString(CurrentPacket), CurrentPacket.DisplayIndex);
553 | else
554 | CurrentPacket.DisplayIndex = AddMessage(ReceivedMessages, PacketToString(CurrentPacket));
555 | if (WatchForPackets)
556 | {
557 | if (FoundPacket())
558 | {
559 | AddMessage(Feedback, "Found " + PacketToString(CurrentPacket) + " " + DateTime.Now.ToLocalTime());
560 | if(DiffPacket!=null)
561 | {
562 | ulong diff = CurrentPacket.Microseconds - DiffPacket.Microseconds;
563 | AddMessage(Feedback, "Diff: " + (Convert.ToDouble(diff) / 1000000.0d).ToString("F6", CultureInfo.InvariantCulture) + " " + DateTime.Now.ToLocalTime());
564 | }
565 | else
566 | {
567 | AddMessage(Feedback, "First Packet");
568 | }
569 | Console.Beep();
570 | DiffPacket = Packet.Clone(CurrentPacket);
571 | }
572 |
573 |
574 | }
575 | }
576 | if (stsResult == TPCANStatus.PCAN_ERROR_ILLOPERATION)
577 | break;
578 | } while (PeakCANHandle > 0 && (!Convert.ToBoolean(stsResult & TPCANStatus.PCAN_ERROR_QRCVEMPTY)));
579 | }
580 | ///
581 | /// Thread-Function used for reading PCAN-Basic messages
582 | ///
583 | private void CANReadThreadFunc()
584 | {
585 | UInt32 iBuffer;
586 |
587 | iBuffer = Convert.ToUInt32(ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
588 | // Sets the handle of the Receive-Event.
589 | LastOperationStatus = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));
590 |
591 | if (LastOperationStatus != TPCANStatus.PCAN_ERROR_OK)
592 | {
593 | //throw new Exception(PeakCANStatusErrorString(LastOperationStatus));
594 | }
595 |
596 | if (ControlForm != null)
597 | {
598 | while (RxMessages)
599 | {
600 | // Waiting for Receive-Event
601 | if (ReceiveEvent.WaitOne(50))
602 | // Process Receive-Event using .NET Invoke function
603 | // in order to interact with Winforms UI (calling the
604 | // function ReadMessage)
605 | if (RxMessages) //Double check reading is still required
606 | ControlForm.Invoke(ReadDelegate);
607 | }
608 | }
609 | else
610 | throw new Exception("No control form set.");
611 | }
612 | ///
613 | /// Set up the CAN Rx thread
614 | ///
615 | public void SetCANMessageRead()
616 | {
617 | // Create and start the tread to read CAN Message using SetRcvEvent()
618 | ThreadStart threadDelegate = new ThreadStart(CANReadThreadFunc);
619 | ReadThread = new Thread(threadDelegate);
620 | ReadThread.IsBackground = true;
621 | RxMessages = true; //allow rx
622 | ReadThread.Start();
623 | }
624 | #endregion
625 |
626 | #region Logging
627 |
628 | ///
629 | /// Configures the PCAN-Trace file for a PCAN-Basic Channel
630 | ///
631 | private void ConfigureTraceFile(string TraceDirectory, bool MultipleFiles, bool TimeStampName, UInt32 MaxMegabytes)
632 | {
633 | UInt32 iBuffer;
634 | TPCANStatus stsResult;
635 |
636 | // Configure the size of a trace file (max 100 MBs)
637 | stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_SIZE, ref MaxMegabytes, sizeof(UInt32));
638 | if (stsResult != TPCANStatus.PCAN_ERROR_OK)
639 | AddMessage(Feedback, GetFormatedError(stsResult));
640 |
641 | //Configure trace location
642 | stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_LOCATION, TraceDirectory, (uint) TraceDirectory.Length);
643 | if (stsResult != TPCANStatus.PCAN_ERROR_OK)
644 | AddMessage(Feedback, GetFormatedError(stsResult));
645 |
646 | //Single or multiple files
647 | if(MultipleFiles)
648 | iBuffer = PCANBasic.TRACE_FILE_SEGMENTED | PCANBasic.TRACE_FILE_OVERWRITE;
649 | else
650 | iBuffer = PCANBasic.TRACE_FILE_SINGLE | PCANBasic.TRACE_FILE_OVERWRITE;
651 |
652 | if(TimeStampName)
653 | iBuffer = iBuffer | PCANBasic.TRACE_FILE_DATE | PCANBasic.TRACE_FILE_TIME;
654 |
655 | stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_CONFIGURE, ref iBuffer, sizeof(UInt32));
656 | if (stsResult != TPCANStatus.PCAN_ERROR_OK)
657 | AddMessage(Feedback, GetFormatedError(stsResult));
658 | }
659 |
660 | ///
661 | /// Help Function used to get an error as text
662 | ///
663 | /// Error code to be translated
664 | /// A text with the translated error
665 | private string GetFormatedError(TPCANStatus error)
666 | {
667 | StringBuilder strTemp;
668 |
669 | // Creates a buffer big enough for a error-text
670 | //
671 | strTemp = new StringBuilder(256);
672 | // Gets the text using the GetErrorText API function
673 | // If the function success, the translated error is returned. If it fails,
674 | // a text describing the current error is returned.
675 | //
676 | if (PCANBasic.GetErrorText(error, 0, strTemp) != TPCANStatus.PCAN_ERROR_OK)
677 | return string.Format("An error occurred. Error-code's text ({0:X}) couldn't be retrieved", error);
678 | else
679 | return strTemp.ToString();
680 | }
681 |
682 |
683 | bool logging;
684 |
685 | public bool StartLogging(string DirectoryForLogFile, bool MultipleFiles, bool TimestampName, UInt32 MaxMegabytes)
686 | {
687 | TPCANStatus stsResult; //result of call to PEAK
688 | UInt32 iBuffer; //Int buffer to send to peak
689 | logging = false; //true is logging turned on 9return value)
690 |
691 | if(Directory.Exists(DirectoryForLogFile))
692 | {
693 | ConfigureTraceFile(DirectoryForLogFile, MultipleFiles, TimestampName, MaxMegabytes);
694 | }
695 | else
696 | {
697 | ConfigureTraceFile("", MultipleFiles, TimestampName, MaxMegabytes);
698 | AddMessage(Feedback, "Directory for trace file: " + DirectoryForLogFile + ", not set (doesn't exist), default is exe location.");
699 | }
700 |
701 | iBuffer = (uint)PCANBasic.PCAN_PARAMETER_ON;
702 | stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_STATUS, ref iBuffer, sizeof(UInt32));
703 | if (stsResult == TPCANStatus.PCAN_ERROR_OK)
704 | {
705 | AddMessage(Feedback, "Trace log on");
706 | logging = true;
707 | }
708 | return logging;
709 | }
710 |
711 | public bool StopLogging()
712 | {
713 | UInt32 iBuffer = (uint)PCANBasic.PCAN_PARAMETER_OFF;
714 | TPCANStatus stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_STATUS, ref iBuffer, sizeof(UInt32));
715 | if (stsResult == TPCANStatus.PCAN_ERROR_OK)
716 | {
717 | AddMessage(Feedback, "Trace log off");
718 | logging = false;
719 | }
720 | return logging;
721 | }
722 |
723 | #endregion
724 |
725 | #region Construction
726 | public PCAN_USB()
727 | {
728 | Initialise();
729 | }
730 | public PCAN_USB(Form ReadInvoker)
731 | {
732 | //set the form invoking the read delegate
733 | ControlForm = ReadInvoker;
734 | Initialise();
735 | }
736 | private void Initialise()
737 | {
738 | //Delegate for reading messages on a Thread
739 | ReadDelegate = new ReadDelegateHandler(ReadMessage);
740 | // Creates the event used for signalize incomming messages
741 | ReceiveEvent = new AutoResetEvent(false);
742 | //default capture mode to overwrite messages in the packet list (messages of same id)
743 | overwriteLastPacket = true;
744 | }
745 | #endregion
746 |
747 | #region PEAK device initialization and release
748 | ///
749 | /// Initialise the given PEAK USB device
750 | ///
751 | /// PEAK USB handle
752 | /// Baud rate as displayed
753 | /// Device operation status
754 | public TPCANStatus InitializeCAN(PCANHandle CANHandle, string CANBaudRate, bool EnableRead)
755 | {
756 | LastOperationStatus = PCANBasic.Initialize(CANHandle, CANBaudRateToPeakCANBaudRate(CANBaudRate), (TPCANType)0, 0, 0);
757 | InitializeMessage();
758 | //Setup receive
759 | if(EnableRead) SetCANMessageRead();
760 | return LastOperationStatus;
761 | }
762 | ///
763 | /// On initialise update the UI list box (if set)
764 | ///
765 | private void InitializeMessage()
766 | {
767 | if (LastOperationStatus != TPCANStatus.PCAN_ERROR_OK)
768 | {
769 | if (LastOperationStatus != TPCANStatus.PCAN_ERROR_CAUTION)
770 | {
771 | LastOperationErrorMessage();
772 | }
773 | else
774 | {
775 | AddMessage(Feedback, "The bitrate being used is different than the given one.");
776 | }
777 | }
778 | else
779 | {
780 | AddMessage(Feedback, "Initialised.");
781 | }
782 | }
783 | ///
784 | /// Release the PEAK CAN USB device
785 | ///
786 | ///
787 | public TPCANStatus Uninitialize()
788 | {
789 | TPCANStatus ret = TPCANStatus.PCAN_ERROR_UNKNOWN;
790 | //Stop reading
791 | RxMessages = false;
792 | if (PeakCANHandle != 0)
793 | {
794 | ret = PCANBasic.Uninitialize(PeakCANHandle);
795 | PeakCANHandle = 0;
796 | }
797 | if (ReadThread != null)
798 | {
799 | ReadThread.Abort();
800 | ReadThread.Join();
801 | ReadThread = null;
802 | }
803 | AddMessage(Feedback, "Uninitialised.");
804 | LastOperationStatus = ret;
805 | return ret;
806 | }
807 | #endregion
808 |
809 | #region Write a CAN packet
810 | public TPCANStatus WriteFrame(UInt32 Id, int DataLength, byte[] Data)
811 | {
812 | TPCANMsg CANMsg;
813 |
814 | // Create a TPCANMsg message structure
815 | CANMsg = new TPCANMsg();
816 |
817 | // Configure the Message Type.
818 | CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
819 | // Message contents (ID, Length of the Data, Data)
820 | CANMsg.ID = Id;
821 | CANMsg.LEN = (byte)DataLength;
822 | CANMsg.DATA = Data;
823 |
824 | // The message is sent to the configured hardware
825 | LastOperationStatus = PCANBasic.Write(PeakCANHandle, ref CANMsg);
826 | if (LastOperationStatus != TPCANStatus.PCAN_ERROR_OK)
827 | {
828 | LastOperationErrorMessage();
829 | }
830 | return LastOperationStatus;
831 | }
832 | #endregion
833 | }
834 | }
835 |
--------------------------------------------------------------------------------
/PCAN_USB/PCANBasic.cs:
--------------------------------------------------------------------------------
1 | // PCANBasic.cs
2 | //
3 | // ~~~~~~~~~~~~
4 | //
5 | // PCAN-Basic API
6 | //
7 | // ~~~~~~~~~~~~
8 | //
9 | // ------------------------------------------------------------------
10 | // Author : Keneth Wagner
11 | // Last change: 13.11.2017 Wagner
12 | //
13 | // Language: C# 1.0
14 | // ------------------------------------------------------------------
15 | //
16 | // Copyright (C) 1999-2017 PEAK-System Technik GmbH, Darmstadt
17 | // more Info at http://www.peak-system.com
18 | //
19 | using System;
20 | using System.Text;
21 | using System.Runtime.InteropServices;
22 |
23 | // Namespace changed from PEAK's Peak.Can.Basic to CAN.PC
24 | namespace CAN.PC
25 | {
26 | using TPCANHandle = System.UInt16;
27 | using TPCANBitrateFD = System.String;
28 | using TPCANTimestampFD = System.UInt64;
29 |
30 | #region Enumerations
31 | ///
32 | /// Represents a PCAN status/error code
33 | ///
34 | [Flags]
35 | public enum TPCANStatus : uint
36 | {
37 | ///
38 | /// No error
39 | ///
40 | PCAN_ERROR_OK = 0x00000,
41 | ///
42 | /// Transmit buffer in CAN controller is full
43 | ///
44 | PCAN_ERROR_XMTFULL = 0x00001,
45 | ///
46 | /// CAN controller was read too late
47 | ///
48 | PCAN_ERROR_OVERRUN = 0x00002,
49 | ///
50 | /// Bus error: an error counter reached the 'light' limit
51 | ///
52 | PCAN_ERROR_BUSLIGHT = 0x00004,
53 | ///
54 | /// Bus error: an error counter reached the 'heavy' limit
55 | ///
56 | PCAN_ERROR_BUSHEAVY = 0x00008,
57 | ///
58 | /// Bus error: an error counter reached the 'warning' limit
59 | ///
60 | PCAN_ERROR_BUSWARNING = PCAN_ERROR_BUSHEAVY,
61 | ///
62 | /// Bus error: the CAN controller is error passive
63 | ///
64 | PCAN_ERROR_BUSPASSIVE = 0x40000,
65 | ///
66 | /// Bus error: the CAN controller is in bus-off state
67 | ///
68 | PCAN_ERROR_BUSOFF = 0x00010,
69 | ///
70 | /// Mask for all bus errors
71 | ///
72 | PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSWARNING | PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF | PCAN_ERROR_BUSPASSIVE),
73 | ///
74 | /// Receive queue is empty
75 | ///
76 | PCAN_ERROR_QRCVEMPTY = 0x00020,
77 | ///
78 | /// Receive queue was read too late
79 | ///
80 | PCAN_ERROR_QOVERRUN = 0x00040,
81 | ///
82 | /// Transmit queue is full
83 | ///
84 | PCAN_ERROR_QXMTFULL = 0x00080,
85 | ///
86 | /// Test of the CAN controller hardware registers failed (no hardware found)
87 | ///
88 | PCAN_ERROR_REGTEST = 0x00100,
89 | ///
90 | /// Driver not loaded
91 | ///
92 | PCAN_ERROR_NODRIVER = 0x00200,
93 | ///
94 | /// Hardware already in use by a Net
95 | ///
96 | PCAN_ERROR_HWINUSE = 0x00400,
97 | ///
98 | /// A Client is already connected to the Net
99 | ///
100 | PCAN_ERROR_NETINUSE = 0x00800,
101 | ///
102 | /// Hardware handle is invalid
103 | ///
104 | PCAN_ERROR_ILLHW = 0x01400,
105 | ///
106 | /// Net handle is invalid
107 | ///
108 | PCAN_ERROR_ILLNET = 0x01800,
109 | ///
110 | /// Client handle is invalid
111 | ///
112 | PCAN_ERROR_ILLCLIENT = 0x01C00,
113 | ///
114 | /// Mask for all handle errors
115 | ///
116 | PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT),
117 | ///
118 | /// Resource (FIFO, Client, timeout) cannot be created
119 | ///
120 | PCAN_ERROR_RESOURCE = 0x02000,
121 | ///
122 | /// Invalid parameter
123 | ///
124 | PCAN_ERROR_ILLPARAMTYPE = 0x04000,
125 | ///
126 | /// Invalid parameter value
127 | ///
128 | PCAN_ERROR_ILLPARAMVAL = 0x08000,
129 | ///
130 | /// Unknown error
131 | ///
132 | PCAN_ERROR_UNKNOWN = 0x10000,
133 | ///
134 | /// Invalid data, function, or action.
135 | ///
136 | PCAN_ERROR_ILLDATA = 0x20000,
137 | ///
138 | /// An operation was successfully carried out, however, irregularities were registered
139 | ///
140 | PCAN_ERROR_CAUTION = 0x2000000,
141 | ///
142 | /// Channel is not initialized
143 | /// Value was changed from 0x40000 to 0x4000000
144 | ///
145 | PCAN_ERROR_INITIALIZE = 0x4000000,
146 | ///
147 | /// Invalid operation
148 | /// Value was changed from 0x80000 to 0x8000000
149 | ///
150 | PCAN_ERROR_ILLOPERATION = 0x8000000,
151 | }
152 |
153 | ///
154 | /// Represents a PCAN device
155 | ///
156 | public enum TPCANDevice : byte
157 | {
158 | ///
159 | /// Undefined, unknown or not selected PCAN device value
160 | ///
161 | PCAN_NONE = 0,
162 | ///
163 | /// PCAN Non-Plug and Play devices. NOT USED WITHIN PCAN-Basic API
164 | ///
165 | PCAN_PEAKCAN = 1,
166 | ///
167 | /// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
168 | ///
169 | PCAN_ISA = 2,
170 | ///
171 | /// PCAN-Dongle
172 | ///
173 | PCAN_DNG = 3,
174 | ///
175 | /// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
176 | ///
177 | PCAN_PCI = 4,
178 | ///
179 | /// PCAN-USB and PCAN-USB Pro
180 | ///
181 | PCAN_USB = 5,
182 | ///
183 | /// PCAN-PC Card
184 | ///
185 | PCAN_PCC = 6,
186 | ///
187 | /// PCAN Virtual hardware. NOT USED WITHIN PCAN-Basic API
188 | ///
189 | PCAN_VIRTUAL = 7,
190 | ///
191 | /// PCAN Gateway devices
192 | ///
193 | PCAN_LAN = 8
194 | }
195 |
196 | ///
197 | /// Represents a PCAN parameter to be read or set
198 | ///
199 | public enum TPCANParameter : byte
200 | {
201 | ///
202 | /// PCAN-USB device number parameter
203 | ///
204 | PCAN_DEVICE_NUMBER = 1,
205 | ///
206 | /// PCAN-PC Card 5-Volt power parameter
207 | ///
208 | PCAN_5VOLTS_POWER = 2,
209 | ///
210 | /// PCAN receive event handler parameter
211 | ///
212 | PCAN_RECEIVE_EVENT = 3,
213 | ///
214 | /// PCAN message filter parameter
215 | ///
216 | PCAN_MESSAGE_FILTER = 4,
217 | ///
218 | /// PCAN-Basic API version parameter
219 | ///
220 | PCAN_API_VERSION = 5,
221 | ///
222 | /// PCAN device channel version parameter
223 | ///
224 | PCAN_CHANNEL_VERSION = 6,
225 | ///
226 | /// PCAN Reset-On-Busoff parameter
227 | ///
228 | PCAN_BUSOFF_AUTORESET = 7,
229 | ///
230 | /// PCAN Listen-Only parameter
231 | ///
232 | PCAN_LISTEN_ONLY = 8,
233 | ///
234 | /// Directory path for log files
235 | ///
236 | PCAN_LOG_LOCATION = 9,
237 | ///
238 | /// Debug-Log activation status
239 | ///
240 | PCAN_LOG_STATUS = 10,
241 | ///
242 | /// Configuration of the debugged information (LOG_FUNCTION_***)
243 | ///
244 | PCAN_LOG_CONFIGURE = 11,
245 | ///
246 | /// Custom insertion of text into the log file
247 | ///
248 | PCAN_LOG_TEXT = 12,
249 | ///
250 | /// Availability status of a PCAN-Channel
251 | ///
252 | PCAN_CHANNEL_CONDITION = 13,
253 | ///
254 | /// PCAN hardware name parameter
255 | ///
256 | PCAN_HARDWARE_NAME = 14,
257 | ///
258 | /// Message reception status of a PCAN-Channel
259 | ///
260 | PCAN_RECEIVE_STATUS = 15,
261 | ///
262 | /// CAN-Controller number of a PCAN-Channel
263 | ///
264 | PCAN_CONTROLLER_NUMBER = 16,
265 | ///
266 | /// Directory path for PCAN trace files
267 | ///
268 | PCAN_TRACE_LOCATION = 17,
269 | ///
270 | /// CAN tracing activation status
271 | ///
272 | PCAN_TRACE_STATUS = 18,
273 | ///
274 | /// Configuration of the maximum file size of a CAN trace
275 | ///
276 | PCAN_TRACE_SIZE = 19,
277 | ///
278 | /// Configuration of the trace file storing mode (TRACE_FILE_***)
279 | ///
280 | PCAN_TRACE_CONFIGURE = 20,
281 | ///
282 | /// Physical identification of a USB based PCAN-Channel by blinking its associated LED
283 | ///
284 | PCAN_CHANNEL_IDENTIFYING = 21,
285 | ///
286 | /// Capabilities of a PCAN device (FEATURE_***)
287 | ///
288 | PCAN_CHANNEL_FEATURES = 22,
289 | ///
290 | /// Using of an existing bit rate (PCAN-View connected to a channel)
291 | ///
292 | PCAN_BITRATE_ADAPTING = 23,
293 | ///
294 | /// Configured bit rate as Btr0Btr1 value
295 | ///
296 | PCAN_BITRATE_INFO = 24,
297 | ///
298 | /// Configured bit rate as TPCANBitrateFD string
299 | ///
300 | PCAN_BITRATE_INFO_FD = 25,
301 | ///
302 | /// Configured nominal CAN Bus speed as Bits per seconds
303 | ///
304 | PCAN_BUSSPEED_NOMINAL = 26,
305 | ///
306 | /// Configured CAN data speed as Bits per seconds
307 | ///
308 | PCAN_BUSSPEED_DATA = 27,
309 | ///
310 | /// Remote address of a LAN channel as string in IPv4 format
311 | ///
312 | PCAN_IP_ADDRESS = 28,
313 | ///
314 | /// Status of the Virtual PCAN-Gateway Service
315 | ///
316 | PCAN_LAN_SERVICE_STATUS = 29,
317 | ///
318 | /// Status messages reception status within a PCAN-Channel
319 | ///
320 | PCAN_ALLOW_STATUS_FRAMES = 30,
321 | ///
322 | /// RTR messages reception status within a PCAN-Channel
323 | ///
324 | PCAN_ALLOW_RTR_FRAMES = 31,
325 | ///
326 | /// Error messages reception status within a PCAN-Channel
327 | ///
328 | PCAN_ALLOW_ERROR_FRAMES = 32,
329 | ///
330 | /// Delay, in microseconds, between sending frames
331 | ///
332 | PCAN_INTERFRAME_DELAY = 33,
333 | ///
334 | /// Filter over code and mask patterns for 11-Bit messages
335 | ///
336 | PCAN_ACCEPTANCE_FILTER_11BIT = 34,
337 | ///
338 | /// Filter over code and mask patterns for 29-Bit messages
339 | ///
340 | PCAN_ACCEPTANCE_FILTER_29BIT = 35,
341 | ///
342 | /// Output mode of 32 digital I/O pin of a PCAN-USB Chip. 1: Output-Active 0 : Output Inactive
343 | ///
344 | PCAN_IO_DIGITAL_CONFIGURATION = 36,
345 | ///
346 | /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip
347 | ///
348 | PCAN_IO_DIGITAL_VALUE = 37,
349 | ///
350 | /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip - Multiple digital I/O pins to 1 = High
351 | ///
352 | PCAN_IO_DIGITAL_SET = 38,
353 | ///
354 | /// Clear multiple digital I/O pins to 0
355 | ///
356 | PCAN_IO_DIGITAL_CLEAR = 39,
357 | ///
358 | /// Get value of a single analog input pin
359 | ///
360 | PCAN_IO_ANALOG_VALUE = 40,
361 | }
362 |
363 | ///
364 | /// Represents the type of a PCAN message
365 | ///
366 | [Flags]
367 | public enum TPCANMessageType : byte
368 | {
369 | ///
370 | /// The PCAN message is a CAN Standard Frame (11-bit identifier)
371 | ///
372 | PCAN_MESSAGE_STANDARD = 0x00,
373 | ///
374 | /// The PCAN message is a CAN Remote-Transfer-Request Frame
375 | ///
376 | PCAN_MESSAGE_RTR = 0x01,
377 | ///
378 | /// The PCAN message is a CAN Extended Frame (29-bit identifier)
379 | ///
380 | PCAN_MESSAGE_EXTENDED = 0x02,
381 | ///
382 | /// The PCAN message represents a FD frame in terms of CiA Specs
383 | ///
384 | PCAN_MESSAGE_FD = 0x04,
385 | ///
386 | /// The PCAN message represents a FD bit rate switch (CAN data at a higher bit rate)
387 | ///
388 | PCAN_MESSAGE_BRS = 0x08,
389 | ///
390 | /// The PCAN message represents a FD error state indicator(CAN FD transmitter was error active)
391 | ///
392 | PCAN_MESSAGE_ESI = 0x10,
393 | ///
394 | /// The PCAN message represents an error frame
395 | ///
396 | PCAN_MESSAGE_ERRFRAME = 0x40,
397 | ///
398 | /// The PCAN message represents a PCAN status message
399 | ///
400 | PCAN_MESSAGE_STATUS = 0x80,
401 | }
402 |
403 | ///
404 | /// Represents a PCAN filter mode
405 | ///
406 | public enum TPCANMode : byte
407 | {
408 | ///
409 | /// Mode is Standard (11-bit identifier)
410 | ///
411 | PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD,
412 | ///
413 | /// Mode is Extended (29-bit identifier)
414 | ///
415 | PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED,
416 | }
417 |
418 | ///
419 | /// Represents a PCAN Baud rate register value
420 | ///
421 | public enum TPCANBaudrate : ushort
422 | {
423 | ///
424 | /// 1 MBit/s
425 | ///
426 | PCAN_BAUD_1M = 0x0014,
427 | ///
428 | /// 800 KBit/s
429 | ///
430 | PCAN_BAUD_800K = 0x0016,
431 | ///
432 | /// 500 kBit/s
433 | ///
434 | PCAN_BAUD_500K = 0x001C,
435 | ///
436 | /// 250 kBit/s
437 | ///
438 | PCAN_BAUD_250K = 0x011C,
439 | ///
440 | /// 125 kBit/s
441 | ///
442 | PCAN_BAUD_125K = 0x031C,
443 | ///
444 | /// 100 kBit/s
445 | ///
446 | PCAN_BAUD_100K = 0x432F,
447 | ///
448 | /// 95,238 KBit/s
449 | ///
450 | PCAN_BAUD_95K = 0xC34E,
451 | ///
452 | /// 83,333 KBit/s
453 | ///
454 | PCAN_BAUD_83K = 0x852B,
455 | ///
456 | /// 50 kBit/s
457 | ///
458 | PCAN_BAUD_50K = 0x472F,
459 | ///
460 | /// 47,619 KBit/s
461 | ///
462 | PCAN_BAUD_47K = 0x1414,
463 | ///
464 | /// 33,333 KBit/s
465 | ///
466 | PCAN_BAUD_33K = 0x8B2F,
467 | ///
468 | /// 20 kBit/s
469 | ///
470 | PCAN_BAUD_20K = 0x532F,
471 | ///
472 | /// 10 kBit/s
473 | ///
474 | PCAN_BAUD_10K = 0x672F,
475 | ///
476 | /// 5 kBit/s
477 | ///
478 | PCAN_BAUD_5K = 0x7F7F,
479 | }
480 |
481 | ///
482 | /// Represents the type of PCAN (non plug and play) hardware to be initialized
483 | ///
484 | public enum TPCANType : byte
485 | {
486 | ///
487 | /// PCAN-ISA 82C200
488 | ///
489 | PCAN_TYPE_ISA = 0x01,
490 | ///
491 | /// PCAN-ISA SJA1000
492 | ///
493 | PCAN_TYPE_ISA_SJA = 0x09,
494 | ///
495 | /// PHYTEC ISA
496 | ///
497 | PCAN_TYPE_ISA_PHYTEC = 0x04,
498 | ///
499 | /// PCAN-Dongle 82C200
500 | ///
501 | PCAN_TYPE_DNG = 0x02,
502 | ///
503 | /// PCAN-Dongle EPP 82C200
504 | ///
505 | PCAN_TYPE_DNG_EPP = 0x03,
506 | ///
507 | /// PCAN-Dongle SJA1000
508 | ///
509 | PCAN_TYPE_DNG_SJA = 0x05,
510 | ///
511 | /// PCAN-Dongle EPP SJA1000
512 | ///
513 | PCAN_TYPE_DNG_SJA_EPP = 0x06,
514 | }
515 | #endregion
516 |
517 | #region Structures
518 | ///
519 | /// Represents a PCAN message
520 | ///
521 | public struct TPCANMsg
522 | {
523 | ///
524 | /// 11/29-bit message identifier
525 | ///
526 | public uint ID;
527 | ///
528 | /// Type of the message
529 | ///
530 | [MarshalAs(UnmanagedType.U1)]
531 | public TPCANMessageType MSGTYPE;
532 | ///
533 | /// Data Length Code of the message (0..8)
534 | ///
535 | public byte LEN;
536 | ///
537 | /// Data of the message (DATA[0]..DATA[7])
538 | ///
539 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
540 | public byte[] DATA;
541 | }
542 |
543 | ///
544 | /// Represents a timestamp of a received PCAN message.
545 | /// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
546 | ///
547 | public struct TPCANTimestamp
548 | {
549 | ///
550 | /// Base-value: milliseconds: 0.. 2^32-1
551 | ///
552 | public uint millis;
553 | ///
554 | /// Roll-arounds of millis
555 | ///
556 | public ushort millis_overflow;
557 | ///
558 | /// Microseconds: 0..999
559 | ///
560 | public ushort micros;
561 | }
562 |
563 | ///
564 | /// Represents a PCAN message from a FD capable hardware
565 | ///
566 | public struct TPCANMsgFD
567 | {
568 | ///
569 | /// 11/29-bit message identifier
570 | ///
571 | public uint ID;
572 | ///
573 | /// Type of the message
574 | ///
575 | [MarshalAs(UnmanagedType.U1)]
576 | public TPCANMessageType MSGTYPE;
577 | ///
578 | /// Data Length Code of the message (0..15)
579 | ///
580 | public byte DLC;
581 | ///
582 | /// Data of the message (DATA[0]..DATA[63])
583 | ///
584 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
585 | public byte[] DATA;
586 | }
587 | #endregion
588 |
589 | #region PCANBasic class
590 | ///
591 | /// PCAN-Basic API class implementation
592 | ///
593 | public static class PCANBasic
594 | {
595 | #region PCAN-BUS Handles Definition
596 | ///
597 | /// Undefined/default value for a PCAN bus
598 | ///
599 | public const TPCANHandle PCAN_NONEBUS = 0x00;
600 |
601 | ///
602 | /// PCAN-ISA interface, channel 1
603 | ///
604 | public const TPCANHandle PCAN_ISABUS1 = 0x21;
605 | ///
606 | /// PCAN-ISA interface, channel 2
607 | ///
608 | public const TPCANHandle PCAN_ISABUS2 = 0x22;
609 | ///
610 | /// PCAN-ISA interface, channel 3
611 | ///
612 | public const TPCANHandle PCAN_ISABUS3 = 0x23;
613 | ///
614 | /// PCAN-ISA interface, channel 4
615 | ///
616 | public const TPCANHandle PCAN_ISABUS4 = 0x24;
617 | ///
618 | /// PCAN-ISA interface, channel 5
619 | ///
620 | public const TPCANHandle PCAN_ISABUS5 = 0x25;
621 | ///
622 | /// PCAN-ISA interface, channel 6
623 | ///
624 | public const TPCANHandle PCAN_ISABUS6 = 0x26;
625 | ///
626 | /// PCAN-ISA interface, channel 7
627 | ///
628 | public const TPCANHandle PCAN_ISABUS7 = 0x27;
629 | ///
630 | /// PCAN-ISA interface, channel 8
631 | ///
632 | public const TPCANHandle PCAN_ISABUS8 = 0x28;
633 |
634 | ///
635 | /// PPCAN-Dongle/LPT interface, channel 1
636 | ///
637 | public const TPCANHandle PCAN_DNGBUS1 = 0x31;
638 |
639 | ///
640 | /// PCAN-PCI interface, channel 1
641 | ///
642 | public const TPCANHandle PCAN_PCIBUS1 = 0x41;
643 | ///
644 | /// PCAN-PCI interface, channel 2
645 | ///
646 | public const TPCANHandle PCAN_PCIBUS2 = 0x42;
647 | ///
648 | /// PCAN-PCI interface, channel 3
649 | ///
650 | public const TPCANHandle PCAN_PCIBUS3 = 0x43;
651 | ///
652 | /// PCAN-PCI interface, channel 4
653 | ///
654 | public const TPCANHandle PCAN_PCIBUS4 = 0x44;
655 | ///
656 | /// PCAN-PCI interface, channel 5
657 | ///
658 | public const TPCANHandle PCAN_PCIBUS5 = 0x45;
659 | ///
660 | /// PCAN-PCI interface, channel 6
661 | ///
662 | public const TPCANHandle PCAN_PCIBUS6 = 0x46;
663 | ///
664 | /// PCAN-PCI interface, channel 7
665 | ///
666 | public const TPCANHandle PCAN_PCIBUS7 = 0x47;
667 | ///
668 | /// PCAN-PCI interface, channel 8
669 | ///
670 | public const TPCANHandle PCAN_PCIBUS8 = 0x48;
671 | ///
672 | /// PCAN-PCI interface, channel 9
673 | ///
674 | public const TPCANHandle PCAN_PCIBUS9 = 0x409;
675 | ///
676 | /// PCAN-PCI interface, channel 10
677 | ///
678 | public const TPCANHandle PCAN_PCIBUS10 = 0x40A;
679 | ///
680 | /// PCAN-PCI interface, channel 11
681 | ///
682 | public const TPCANHandle PCAN_PCIBUS11 = 0x40B;
683 | ///
684 | /// PCAN-PCI interface, channel 12
685 | ///
686 | public const TPCANHandle PCAN_PCIBUS12 = 0x40C;
687 | ///
688 | /// PCAN-PCI interface, channel 13
689 | ///
690 | public const TPCANHandle PCAN_PCIBUS13 = 0x40D;
691 | ///
692 | /// PCAN-PCI interface, channel 14
693 | ///
694 | public const TPCANHandle PCAN_PCIBUS14 = 0x40E;
695 | ///
696 | /// PCAN-PCI interface, channel 15
697 | ///
698 | public const TPCANHandle PCAN_PCIBUS15 = 0x40F;
699 | ///
700 | /// PCAN-PCI interface, channel 16
701 | ///
702 | public const TPCANHandle PCAN_PCIBUS16 = 0x410;
703 |
704 | ///
705 | /// PCAN-USB interface, channel 1
706 | ///
707 | public const TPCANHandle PCAN_USBBUS1 = 0x51;
708 | ///
709 | /// PCAN-USB interface, channel 2
710 | ///
711 | public const TPCANHandle PCAN_USBBUS2 = 0x52;
712 | ///
713 | /// PCAN-USB interface, channel 3
714 | ///
715 | public const TPCANHandle PCAN_USBBUS3 = 0x53;
716 | ///
717 | /// PCAN-USB interface, channel 4
718 | ///
719 | public const TPCANHandle PCAN_USBBUS4 = 0x54;
720 | ///
721 | /// PCAN-USB interface, channel 5
722 | ///
723 | public const TPCANHandle PCAN_USBBUS5 = 0x55;
724 | ///
725 | /// PCAN-USB interface, channel 6
726 | ///
727 | public const TPCANHandle PCAN_USBBUS6 = 0x56;
728 | ///
729 | /// PCAN-USB interface, channel 7
730 | ///
731 | public const TPCANHandle PCAN_USBBUS7 = 0x57;
732 | ///
733 | /// PCAN-USB interface, channel 8
734 | ///
735 | public const TPCANHandle PCAN_USBBUS8 = 0x58;
736 | ///
737 | /// PCAN-USB interface, channel 9
738 | ///
739 | public const TPCANHandle PCAN_USBBUS9 = 0x509;
740 | ///
741 | /// PCAN-USB interface, channel 10
742 | ///
743 | public const TPCANHandle PCAN_USBBUS10 = 0x50A;
744 | ///
745 | /// PCAN-USB interface, channel 11
746 | ///
747 | public const TPCANHandle PCAN_USBBUS11 = 0x50B;
748 | ///
749 | /// PCAN-USB interface, channel 12
750 | ///
751 | public const TPCANHandle PCAN_USBBUS12 = 0x50C;
752 | ///
753 | /// PCAN-USB interface, channel 13
754 | ///
755 | public const TPCANHandle PCAN_USBBUS13 = 0x50D;
756 | ///
757 | /// PCAN-USB interface, channel 14
758 | ///
759 | public const TPCANHandle PCAN_USBBUS14 = 0x50E;
760 | ///
761 | /// PCAN-USB interface, channel 15
762 | ///
763 | public const TPCANHandle PCAN_USBBUS15 = 0x50F;
764 | ///
765 | /// PCAN-USB interface, channel 16
766 | ///
767 | public const TPCANHandle PCAN_USBBUS16 = 0x510;
768 |
769 | ///
770 | /// PCAN-PC Card interface, channel 1
771 | ///
772 | public const TPCANHandle PCAN_PCCBUS1 = 0x61;
773 | ///
774 | /// PCAN-PC Card interface, channel 2
775 | ///
776 | public const TPCANHandle PCAN_PCCBUS2 = 0x62;
777 |
778 | ///
779 | /// PCAN-LAN interface, channel 1
780 | ///
781 | public const TPCANHandle PCAN_LANBUS1 = 0x801;
782 | ///
783 | /// PCAN-LAN interface, channel 2
784 | ///
785 | public const TPCANHandle PCAN_LANBUS2 = 0x802;
786 | ///
787 | /// PCAN-LAN interface, channel 3
788 | ///
789 | public const TPCANHandle PCAN_LANBUS3 = 0x803;
790 | ///
791 | /// PCAN-LAN interface, channel 4
792 | ///
793 | public const TPCANHandle PCAN_LANBUS4 = 0x804;
794 | ///
795 | /// PCAN-LAN interface, channel 5
796 | ///
797 | public const TPCANHandle PCAN_LANBUS5 = 0x805;
798 | ///
799 | /// PCAN-LAN interface, channel 6
800 | ///
801 | public const TPCANHandle PCAN_LANBUS6 = 0x806;
802 | ///
803 | /// PCAN-LAN interface, channel 7
804 | ///
805 | public const TPCANHandle PCAN_LANBUS7 = 0x807;
806 | ///
807 | /// PCAN-LAN interface, channel 8
808 | ///
809 | public const TPCANHandle PCAN_LANBUS8 = 0x808;
810 | ///
811 | /// PCAN-LAN interface, channel 9
812 | ///
813 | public const TPCANHandle PCAN_LANBUS9 = 0x809;
814 | ///
815 | /// PCAN-LAN interface, channel 10
816 | ///
817 | public const TPCANHandle PCAN_LANBUS10 = 0x80A;
818 | ///
819 | /// PCAN-LAN interface, channel 11
820 | ///
821 | public const TPCANHandle PCAN_LANBUS11 = 0x80B;
822 | ///
823 | /// PCAN-LAN interface, channel 12
824 | ///
825 | public const TPCANHandle PCAN_LANBUS12 = 0x80C;
826 | ///
827 | /// PCAN-LAN interface, channel 13
828 | ///
829 | public const TPCANHandle PCAN_LANBUS13 = 0x80D;
830 | ///
831 | /// PCAN-LAN interface, channel 14
832 | ///
833 | public const TPCANHandle PCAN_LANBUS14 = 0x80E;
834 | ///
835 | /// PCAN-LAN interface, channel 15
836 | ///
837 | public const TPCANHandle PCAN_LANBUS15 = 0x80F;
838 | ///
839 | /// PCAN-LAN interface, channel 16
840 | ///
841 | public const TPCANHandle PCAN_LANBUS16 = 0x810;
842 | #endregion
843 |
844 | #region FD Bit rate parameters
845 | ///
846 | /// Clock frequency in Herz (80000000, 60000000, 40000000, 30000000, 24000000, 20000000)
847 | ///
848 | public const string PCAN_BR_CLOCK = "f_clock";
849 | ///
850 | /// Clock frequency in Megaherz (80, 60, 40, 30, 24, 20)
851 | ///
852 | public const string PCAN_BR_CLOCK_MHZ = "f_clock_mhz";
853 | ///
854 | /// Clock prescaler for nominal time quantum
855 | ///
856 | public const string PCAN_BR_NOM_BRP = "nom_brp";
857 | ///
858 | /// TSEG1 segment for nominal bit rate in time quanta
859 | ///
860 | public const string PCAN_BR_NOM_TSEG1 = "nom_tseg1";
861 | ///
862 | /// TSEG2 segment for nominal bit rate in time quanta
863 | ///
864 | public const string PCAN_BR_NOM_TSEG2 = "nom_tseg2";
865 | ///
866 | /// Synchronization Jump Width for nominal bit rate in time quanta
867 | ///
868 | public const string PCAN_BR_NOM_SJW = "nom_sjw";
869 | ///
870 | /// Sample point for nominal bit rate
871 | ///
872 | public const string PCAN_BR_NOM_SAMPLE = "nom_sam";
873 | ///
874 | /// Clock prescaler for highspeed data time quantum
875 | ///
876 | public const string PCAN_BR_DATA_BRP = "data_brp";
877 | ///
878 | /// TSEG1 segment for fast data bit rate in time quanta
879 | ///
880 | public const string PCAN_BR_DATA_TSEG1 = "data_tseg1";
881 | ///
882 | /// TSEG2 segment for fast data bit rate in time quanta
883 | ///
884 | public const string PCAN_BR_DATA_TSEG2 = "data_tseg2";
885 | ///
886 | /// Synchronization Jump Width for highspeed data bit rate in time quanta
887 | ///
888 | public const string PCAN_BR_DATA_SJW = "data_sjw";
889 | ///
890 | /// Secondary sample point delay for highspeed data bit rate in cyles
891 | ///
892 | public const string PCAN_BR_DATA_SAMPLE = "data_ssp_offset";
893 | #endregion
894 |
895 | #region Parameter values definition
896 | ///
897 | /// The PCAN parameter is not set (inactive)
898 | ///
899 | public const int PCAN_PARAMETER_OFF = 0;
900 | ///
901 | /// The PCAN parameter is set (active)
902 | ///
903 | public const int PCAN_PARAMETER_ON = 1;
904 | ///
905 | /// The PCAN filter is closed. No messages will be received
906 | ///
907 | public const int PCAN_FILTER_CLOSE = 0;
908 | ///
909 | /// The PCAN filter is fully opened. All messages will be received
910 | ///
911 | public const int PCAN_FILTER_OPEN = 1;
912 | ///
913 | /// The PCAN filter is custom configured. Only registered
914 | /// messages will be received
915 | ///
916 | public const int PCAN_FILTER_CUSTOM = 2;
917 | ///
918 | /// The PCAN-Channel handle is illegal, or its associated hardware is not available
919 | ///
920 | public const int PCAN_CHANNEL_UNAVAILABLE = 0;
921 | ///
922 | /// The PCAN-Channel handle is available to be connected (Plug and Play Hardware: it means furthermore that the hardware is plugged-in)
923 | ///
924 | public const int PCAN_CHANNEL_AVAILABLE = 1;
925 | ///
926 | /// The PCAN-Channel handle is valid, and is already being used
927 | ///
928 | public const int PCAN_CHANNEL_OCCUPIED = 2;
929 | ///
930 | /// The PCAN-Channel handle is already being used by a PCAN-View application, but is available to connect
931 | ///
932 | public const int PCAN_CHANNEL_PCANVIEW = (PCAN_CHANNEL_AVAILABLE | PCAN_CHANNEL_OCCUPIED);
933 |
934 | ///
935 | /// Logs system exceptions / errors
936 | ///
937 | public const int LOG_FUNCTION_DEFAULT = 0x00;
938 | ///
939 | /// Logs the entries to the PCAN-Basic API functions
940 | ///
941 | public const int LOG_FUNCTION_ENTRY = 0x01;
942 | ///
943 | /// Logs the parameters passed to the PCAN-Basic API functions
944 | ///
945 | public const int LOG_FUNCTION_PARAMETERS = 0x02;
946 | ///
947 | /// Logs the exits from the PCAN-Basic API functions
948 | ///
949 | public const int LOG_FUNCTION_LEAVE = 0x04;
950 | ///
951 | /// Logs the CAN messages passed to the CAN_Write function
952 | ///
953 | public const int LOG_FUNCTION_WRITE = 0x08;
954 | ///
955 | /// Logs the CAN messages received within the CAN_Read function
956 | ///
957 | public const int LOG_FUNCTION_READ = 0x10;
958 | ///
959 | /// Logs all possible information within the PCAN-Basic API functions
960 | ///
961 | public const int LOG_FUNCTION_ALL = 0xFFFF;
962 |
963 | ///
964 | /// A single file is written until it size reaches PAN_TRACE_SIZE
965 | ///
966 | public const int TRACE_FILE_SINGLE = 0x00;
967 | ///
968 | /// Traced data is distributed in several files with size PAN_TRACE_SIZE
969 | ///
970 | public const int TRACE_FILE_SEGMENTED = 0x01;
971 | ///
972 | /// Includes the date into the name of the trace file
973 | ///
974 | public const int TRACE_FILE_DATE = 0x02;
975 | ///
976 | /// Includes the start time into the name of the trace file
977 | ///
978 | public const int TRACE_FILE_TIME = 0x04;
979 | ///
980 | /// Causes the overwriting of available traces (same name)
981 | ///
982 | public const int TRACE_FILE_OVERWRITE = 0x80;
983 |
984 | ///
985 | /// Device supports flexible data-rate (CAN-FD)
986 | ///
987 | public const int FEATURE_FD_CAPABLE = 0x01;
988 | ///
989 | /// Device supports a delay between sending frames (FPGA based USB devices)
990 | ///
991 | public const int FEATURE_DELAY_CAPABLE = 0x02;
992 | ///
993 | /// Device supports I/O functionality for electronic circuits (USB-Chip devices)
994 | ///
995 | public const int FEATURE_IO_CAPABLE = 0x04;
996 |
997 | ///
998 | /// The service is not running
999 | ///
1000 | public const int SERVICE_STATUS_STOPPED = 0x01;
1001 | ///
1002 | /// The service is running
1003 | ///
1004 | public const int SERVICE_STATUS_RUNNING = 0x04;
1005 | #endregion
1006 |
1007 | #region PCANBasic API Implementation
1008 | ///
1009 | /// Initializes a PCAN Channel
1010 | ///
1011 | /// The handle of a PCAN Channel
1012 | /// The speed for the communication (BTR0BTR1 code)
1013 | /// NON PLUG and PLAY: The type of hardware and operation mode
1014 | /// NON PLUG and PLAY: The I/O address for the parallel port
1015 | /// NON PLUG and PLAY: Interrupt number of the parallel por
1016 | /// A TPCANStatus error code
1017 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")]
1018 | public static extern TPCANStatus Initialize(
1019 | [MarshalAs(UnmanagedType.U2)]
1020 | TPCANHandle Channel,
1021 | [MarshalAs(UnmanagedType.U2)]
1022 | TPCANBaudrate Btr0Btr1,
1023 | [MarshalAs(UnmanagedType.U1)]
1024 | TPCANType HwType,
1025 | UInt32 IOPort,
1026 | UInt16 Interrupt);
1027 |
1028 | ///
1029 | /// Initializes a PCAN Channel
1030 | ///
1031 | /// The handle of a PCAN Channel
1032 | /// The speed for the communication (BTR0BTR1 code)
1033 | /// A TPCANStatus error code
1034 | public static TPCANStatus Initialize(
1035 | TPCANHandle Channel,
1036 | TPCANBaudrate Btr0Btr1)
1037 | {
1038 | return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0);
1039 | }
1040 |
1041 | ///
1042 | /// Initializes a FD capable PCAN Channel
1043 | ///
1044 | /// The handle of a FD capable PCAN Channel
1045 | /// The speed for the communication (FD bit rate string)
1046 | /// See PCAN_BR_* values
1047 | /// Bit rate string must follow the following construction rules:
1048 | /// * parameters and values must be separated by '='
1049 | /// * Couples of Parameter/value must be separated by ','
1050 | /// * Following Parameter must be filled out: f_clock, data_brp, data_sjw, data_tseg1, data_tseg2,
1051 | /// nom_brp, nom_sjw, nom_tseg1, nom_tseg2.
1052 | /// * Following Parameters are optional (not used yet): data_ssp_offset, nom_sam
1053 | /// f_clock=80000000,nom_brp=10,nom_tseg1=5,nom_tseg2=2,nom_sjw=1,data_brp=4,data_tseg1=7,data_tseg2=2,data_sjw=1
1054 | /// A TPCANStatus error code
1055 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_InitializeFD")]
1056 | public static extern TPCANStatus InitializeFD(
1057 | [MarshalAs(UnmanagedType.U2)]
1058 | TPCANHandle Channel,
1059 | TPCANBitrateFD BitrateFD);
1060 |
1061 | ///
1062 | /// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
1063 | ///
1064 | /// Giving the TPCANHandle value "PCAN_NONEBUS",
1065 | /// uninitialize all initialized channels
1066 | /// The handle of a PCAN Channel
1067 | /// A TPCANStatus error code
1068 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")]
1069 | public static extern TPCANStatus Uninitialize(
1070 | [MarshalAs(UnmanagedType.U2)]
1071 | TPCANHandle Channel);
1072 |
1073 | ///
1074 | /// Resets the receive and transmit queues of the PCAN Channel
1075 | ///
1076 | /// A reset of the CAN controller is not performed
1077 | /// The handle of a PCAN Channel
1078 | /// A TPCANStatus error code
1079 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")]
1080 | public static extern TPCANStatus Reset(
1081 | [MarshalAs(UnmanagedType.U2)]
1082 | TPCANHandle Channel);
1083 |
1084 | ///
1085 | /// Gets the current status of a PCAN Channel
1086 | ///
1087 | /// The handle of a PCAN Channel
1088 | /// A TPCANStatus error code
1089 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")]
1090 | public static extern TPCANStatus GetStatus(
1091 | [MarshalAs(UnmanagedType.U2)]
1092 | TPCANHandle Channel);
1093 |
1094 | ///
1095 | /// Reads a CAN message from the receive queue of a PCAN Channel
1096 | ///
1097 | /// The handle of a PCAN Channel
1098 | /// A TPCANMsg structure buffer to store the CAN message
1099 | /// A TPCANTimestamp structure buffer to get
1100 | /// the reception time of the message
1101 | /// A TPCANStatus error code
1102 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
1103 | public static extern TPCANStatus Read(
1104 | [MarshalAs(UnmanagedType.U2)]
1105 | TPCANHandle Channel,
1106 | out TPCANMsg MessageBuffer,
1107 | out TPCANTimestamp TimestampBuffer);
1108 |
1109 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
1110 | private static extern TPCANStatus Read(
1111 | [MarshalAs(UnmanagedType.U2)]
1112 | TPCANHandle Channel,
1113 | out TPCANMsg MessageBuffer,
1114 | IntPtr bufferPointer);
1115 |
1116 | ///
1117 | /// Reads a CAN message from the receive queue of a PCAN Channel
1118 | ///
1119 | /// The handle of a PCAN Channel
1120 | /// A TPCANMsg structure buffer to store the CAN message
1121 | /// A TPCANStatus error code
1122 | public static TPCANStatus Read(
1123 | TPCANHandle Channel,
1124 | out TPCANMsg MessageBuffer)
1125 | {
1126 | return Read(Channel, out MessageBuffer, IntPtr.Zero);
1127 | }
1128 |
1129 | ///
1130 | /// Reads a CAN message from the receive queue of a FD capable PCAN Channel
1131 | ///
1132 | /// The handle of a FD capable PCAN Channel
1133 | /// A TPCANMsgFD structure buffer to store the CAN message
1134 | /// A TPCANTimestampFD buffer to get the
1135 | /// reception time of the message
1136 | /// A TPCANStatus error code
1137 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")]
1138 | public static extern TPCANStatus ReadFD(
1139 | [MarshalAs(UnmanagedType.U2)]
1140 | TPCANHandle Channel,
1141 | out TPCANMsgFD MessageBuffer,
1142 | out TPCANTimestampFD TimestampBuffer);
1143 |
1144 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")]
1145 | private static extern TPCANStatus ReadFD(
1146 | [MarshalAs(UnmanagedType.U2)]
1147 | TPCANHandle Channel,
1148 | out TPCANMsgFD MessageBuffer,
1149 | IntPtr TimestampBuffer);
1150 |
1151 | ///
1152 | /// Reads a CAN message from the receive queue of a FD capable PCAN Channel
1153 | ///
1154 | /// The handle of a FD capable PCAN Channel
1155 | /// A TPCANMsgFD structure buffer to store the CAN message
1156 | /// A TPCANStatus error code
1157 | public static TPCANStatus ReadFD(
1158 | TPCANHandle Channel,
1159 | out TPCANMsgFD MessageBuffer)
1160 | {
1161 | return ReadFD(Channel, out MessageBuffer, IntPtr.Zero);
1162 | }
1163 |
1164 | ///
1165 | /// Transmits a CAN message
1166 | ///
1167 | /// The handle of a PCAN Channel
1168 | /// A TPCANMsg buffer with the message to be sent
1169 | /// A TPCANStatus error code
1170 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
1171 | public static extern TPCANStatus Write(
1172 | [MarshalAs(UnmanagedType.U2)]
1173 | TPCANHandle Channel,
1174 | ref TPCANMsg MessageBuffer);
1175 |
1176 | ///
1177 | /// Transmits a CAN message over a FD capable PCAN Channel
1178 | ///
1179 | /// The handle of a FD capable PCAN Channel
1180 | /// A TPCANMsgFD buffer with the message to be sent
1181 | /// A TPCANStatus error code
1182 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_WriteFD")]
1183 | public static extern TPCANStatus WriteFD(
1184 | [MarshalAs(UnmanagedType.U2)]
1185 | TPCANHandle Channel,
1186 | ref TPCANMsgFD MessageBuffer);
1187 |
1188 | ///
1189 | /// Configures the reception filter
1190 | ///
1191 | /// The message filter will be expanded with every call to
1192 | /// this function. If it is desired to reset the filter, please use
1193 | /// the 'SetValue' function
1194 | /// The handle of a PCAN Channel
1195 | /// The lowest CAN ID to be received
1196 | /// The highest CAN ID to be received
1197 | /// Message type, Standard (11-bit identifier) or
1198 | /// Extended (29-bit identifier)
1199 | /// A TPCANStatus error code
1200 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_FilterMessages")]
1201 | public static extern TPCANStatus FilterMessages(
1202 | [MarshalAs(UnmanagedType.U2)]
1203 | TPCANHandle Channel,
1204 | UInt32 FromID,
1205 | UInt32 ToID,
1206 | [MarshalAs(UnmanagedType.U1)]
1207 | TPCANMode Mode);
1208 |
1209 | ///
1210 | /// Retrieves a PCAN Channel value
1211 | ///
1212 | /// Parameters can be present or not according with the kind
1213 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1214 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1215 | /// The handle of a PCAN Channel
1216 | /// The TPCANParameter parameter to get
1217 | /// Buffer for the parameter value
1218 | /// Size in bytes of the buffer
1219 | /// A TPCANStatus error code
1220 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
1221 | public static extern TPCANStatus GetValue(
1222 | [MarshalAs(UnmanagedType.U2)]
1223 | TPCANHandle Channel,
1224 | [MarshalAs(UnmanagedType.U1)]
1225 | TPCANParameter Parameter,
1226 | StringBuilder StringBuffer,
1227 | UInt32 BufferLength);
1228 |
1229 | ///
1230 | /// Retrieves a PCAN Channel value
1231 | ///
1232 | /// Parameters can be present or not according with the kind
1233 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1234 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1235 | /// The handle of a PCAN Channel
1236 | /// The TPCANParameter parameter to get
1237 | /// Buffer for the parameter value
1238 | /// Size in bytes of the buffer
1239 | /// A TPCANStatus error code
1240 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
1241 | public static extern TPCANStatus GetValue(
1242 | [MarshalAs(UnmanagedType.U2)]
1243 | TPCANHandle Channel,
1244 | [MarshalAs(UnmanagedType.U1)]
1245 | TPCANParameter Parameter,
1246 | out UInt32 NumericBuffer,
1247 | UInt32 BufferLength);
1248 |
1249 | ///
1250 | /// Retrieves a PCAN Channel value
1251 | ///
1252 | /// Parameters can be present or not according with the kind
1253 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1254 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1255 | /// The handle of a PCAN Channel
1256 | /// The TPCANParameter parameter to get
1257 | /// Buffer for the parameter value
1258 | /// Size in bytes of the buffer
1259 | /// A TPCANStatus error code
1260 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
1261 | public static extern TPCANStatus GetValue(
1262 | [MarshalAs(UnmanagedType.U2)]
1263 | TPCANHandle Channel,
1264 | [MarshalAs(UnmanagedType.U1)]
1265 | TPCANParameter Parameter,
1266 | out UInt64 NumericBuffer,
1267 | UInt32 BufferLength);
1268 |
1269 | ///
1270 | /// Configures or sets a PCAN Channel value
1271 | ///
1272 | /// Parameters can be present or not according with the kind
1273 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1274 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1275 | /// The handle of a PCAN Channel
1276 | /// The TPCANParameter parameter to set
1277 | /// Buffer with the value to be set
1278 | /// Size in bytes of the buffer
1279 | /// A TPCANStatus error code
1280 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
1281 | public static extern TPCANStatus SetValue(
1282 | [MarshalAs(UnmanagedType.U2)]
1283 | TPCANHandle Channel,
1284 | [MarshalAs(UnmanagedType.U1)]
1285 | TPCANParameter Parameter,
1286 | ref UInt32 NumericBuffer,
1287 | UInt32 BufferLength);
1288 |
1289 | ///
1290 | /// Configures or sets a PCAN Channel value
1291 | ///
1292 | /// Parameters can be present or not according with the kind
1293 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1294 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1295 | /// The handle of a PCAN Channel
1296 | /// The TPCANParameter parameter to set
1297 | /// Buffer with the value to be set
1298 | /// Size in bytes of the buffer
1299 | /// A TPCANStatus error code
1300 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
1301 | public static extern TPCANStatus SetValue(
1302 | [MarshalAs(UnmanagedType.U2)]
1303 | TPCANHandle Channel,
1304 | [MarshalAs(UnmanagedType.U1)]
1305 | TPCANParameter Parameter,
1306 | ref UInt64 NumericBuffer,
1307 | UInt32 BufferLength);
1308 |
1309 | ///
1310 | /// Configures or sets a PCAN Channel value
1311 | ///
1312 | /// Parameters can be present or not according with the kind
1313 | /// of Hardware (PCAN Channel) being used. If a parameter is not available,
1314 | /// a PCAN_ERROR_ILLPARAMTYPE error will be returned
1315 | /// The handle of a PCAN Channel
1316 | ///
1317 | /// Buffer with the value to be set
1318 | /// Size in bytes of the buffer
1319 | /// A TPCANStatus error code
1320 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
1321 | public static extern TPCANStatus SetValue(
1322 | [MarshalAs(UnmanagedType.U2)]
1323 | TPCANHandle Channel,
1324 | [MarshalAs(UnmanagedType.U1)]
1325 | TPCANParameter Parameter,
1326 | [MarshalAs(UnmanagedType.LPStr,SizeParamIndex=3)]
1327 | string StringBuffer,
1328 | UInt32 BufferLength);
1329 |
1330 | ///
1331 | /// Returns a descriptive text of a given TPCANStatus error
1332 | /// code, in any desired language
1333 | ///
1334 | /// The current languages available for translation are:
1335 | /// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
1336 | /// Italian (0x10) and French (0x0C)
1337 | /// A TPCANStatus error code
1338 | /// Indicates a 'Primary language ID'
1339 | /// Buffer for the text (must be at least 256 in length)
1340 | /// A TPCANStatus error code
1341 | [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")]
1342 | public static extern TPCANStatus GetErrorText(
1343 | [MarshalAs(UnmanagedType.U4)]
1344 | TPCANStatus Error,
1345 | UInt16 Language,
1346 | StringBuilder StringBuffer);
1347 | #endregion
1348 | }
1349 | #endregion
1350 | }
1351 |
--------------------------------------------------------------------------------