Troubleshooting
Fix the IS7Project3 cast error in a STEP 7 station upload
Short answer
The compile error on the Projects.Add line is not a missing enumeration member. Keep S7ProjectType.S7Project as the third argument, take the return into an object, query it for IS7Project3 with an as-cast, and call UploadStation through that interface; a null result points at the interop references, not at S7ProjectType.
On this page
A station upload from an S7-300 through the STEP 7 command interface dies at compile time on the Projects.Add line with "Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you missing a cast?)". The fix takes two moves, not a new enum member: store the return of Projects.Add in an object, then query that COM object for IS7Project3 with an explicit cast and call UploadStation through the interface you get back. S7ProjectType.S7Project stays as the third argument, because the enumeration selects the kind of project created while IS7Project3 is the interface that carries the upload method, and no enum member can ever stand in for it. This article works the corrected code end to end with S7HCOM_XLib and SimaticLib, then splits everything that can still go wrong into cast problems and online-access problems.
Why the compiler rejects the Projects.Add assignment #
The message is literal. Projects.Add is declared to return a type with no implicit conversion to IS7Project3, and C# binds every call through the type it knows at compile time, never through what the runtime object may also support. The explicit conversion the message mentions exists because the underlying COM object does expose IS7Project3; the query only has to be written out.
No enum member can help, because the two names answer different questions. S7ProjectType.S7Project answers which category of object Projects.Add should create; IS7Project3 answers which set of methods you can call on the object that comes back. A category cannot carry a method list, so a search of S7ProjectType for an IS7Project3-shaped member finds nothing and is supposed to find nothing. The failing assignment asks the compiler to bind UploadStation on a type that has no such member at compile time, and that is the entire error.
What each name in the failing call is for #
| Name in the call | What it is | What you do with it |
|---|---|---|
ISimatic3 |
Interface to the automation object model | Reach the Projects collection through it |
Simatic |
The class you instantiate | new Simatic() opens the automation session |
Projects.Add |
Method that creates a project | Pass the name, the folder and the project type |
S7ProjectType.S7Project |
Enum member selecting the project kind | Third argument; leave it as it is |
IS7Project3 |
Interface that exposes the upload | Query the returned object for it before calling |
UploadStation |
Method that uploads the station | Call it only through IS7Project3 |
Keep the split in your code as well as in your head: the third argument classifies, the cast acquires the calling surface. The same object model is walked in more depth by the page on accessing STEP 7 projects programmatically through the SimaticLib COM object, if you want the full tour before touching the upload.
Example: uploading an S7-300 station into a new Test1 project at C:\temp #
Inputs, carried over unchanged from the failing version:
- destination project name
Test1in folderC:\temp, which the call already creates; - project kind
S7ProjectType.S7Project, already the third argument; - the interface
IS7Project3, because that is whereUploadStationlives; - the exception log
C:\temp\log.txt, already written by the catch block.
The rule in words: create the project and keep the result in an object; query that object for IS7Project3 with an as conversion; refuse to continue on null; call UploadStation through the interface variable with exactly the parameter list the interop generated. The upload fills the offline project from the online station, so the destination has to be a real project; What STEP 7 is in a PLC project, and which one you need breaks down what one contains. The call only reads from the CPU and writes nothing to it, so the S7-300 keeps running its loaded program throughout.
public void UploadStationFromPlc()
{
try
{
int retcode = 0;
ISimatic3 simatic = new Simatic();
object created = simatic.Projects.Add("Test1", @"C:\temp", S7ProjectType.S7Project);
IS7Project3 project = created as IS7Project3;
if (project == null)
{
throw new InvalidCastException(
"Created project does not expose IS7Project3.");
}
project.UploadStation(retcode);
}
catch (Exception ex)
{
using (StreamWriter sw = File.AppendText(@"C:\temp\log.txt"))
{
sw.WriteLine("Error: " + ex + " " + Convert.ToString(DateTime.Now));
}
}
}
object created keeps project creation separate from interface acquisition, so a failure there means the name, the folder or the automation startup, while a failure at the next line means the interface. as IS7Project3 is the query the compiler asked for: at run time, COM tests whether the object supports the interface and returns null when it does not. The null check is the decision point, and its threshold is exact: project == null stops the method before UploadStation and blames the references, never S7ProjectType. Pass retcode exactly as the generated signature declares; the interop decides whether that argument is input, output or both, and the variable's name proves nothing about it.
Result: the file compiles, and on a run with the station reachable the Test1 project appears under C:\temp with the upload executed through the interface. Decision: a null cast sends you to the reference check below; a non-null cast with a thrown UploadStation leaves the cast proven and moves the fault to the online path or the transfer.
When the as-cast returns null #
A null after a clean compile means the loaded COM object did not hand back IS7Project3 through the references your project currently uses. S7HCOM_XLib and SimaticLib are COM libraries, and .NET imports their types as interop types, so two imported types can share a name and still be incompatible when they come from different references or different generated interop assemblies. Work through this in order and expect the stated outcome at each step:
- Open the type information for Projects.Add in the IDE and record its declared return type.
- Navigate from IS7Project3 to the reference that defines it and confirm it is the interop you intend to use.
- Remove duplicate or unintended references, but only after checking that nothing else in the project depends on them.
- Clean and rebuild, so stale generated interop metadata cannot keep the old types alive.
The wrong turn is silencing the compiler by pushing the object through unrelated managed types: the object must genuinely expose IS7Project3, and a change to the managed declaration cannot add that capability. Decision: if the cast still returns null after a rebuild on a single intended reference set, stop editing the upload logic and fix the references, because the code is no longer the fault.
When the cast passes but UploadStation throws #
Once project is non-null the type problem is closed, and every failure after that belongs to another layer. Split them by where the call stops:
| Where it stops | What that failure is about |
|---|---|
| The assignment, at compile time | Managed type conversion, fixed above |
| The cast returns null | Interface support or interop-reference identity |
UploadStation throws |
Online connection, station selection, access or the transfer itself |
| The call returns but the project stays thin | Online-retrievable data versus project-only data |
A thrown UploadStation is usually the online path rather than the code: whether the automation can reach the S7-300 at all is decided by the PG/PC interface setting on the engineering station, and the page on configuring the PG/PC interface for a SimaticNet PC station covers that setup. The logged exception with its timestamp tells you which layer threw, so read it before changing anything.
Troubleshooting checklist #
Run the case in this order and hold each step to its expected result:
- Store the Projects.Add result in an object and rebuild; the compiler message should now survive only at the cast line you are about to add.
- Add
IS7Project3 project = created as IS7Project3;and expect a non-null project with your intended references. - If the cast returns null, reduce to a single interop reference set, clean and rebuild, and expect non-null before touching anything else.
- Call UploadStation with the generated signature unchanged, adding no modifiers and no invented result interpretation.
- On an exception, read the logged exception and check the online path before editing code.
- After a clean return, open the uploaded project and compare it with what the station actually offers online; that comparison, not the method's return, is the proof the transfer delivered.
Questions asked
Why does S7ProjectType have no S7Project3 member?
The enumeration and the interface count different things. S7ProjectType.S7Project tells Projects.Add which category of object to create; IS7Project3 is one numbered revision of the interface the created object exposes, each revision carrying its own set of callable members. You never pick an interface revision in the Add call: create the project with the enum member, then query the returned object for the interface that carries the method you need.
Which data comes back with an uploaded station?
An upload retrieves what the CPU can actually hand over online into the destination project. Anything that existed only offline at the original engineering station does not come back: symbol tables, block and network comments, and source files are project-only material. If the task needs those artifacts, move them separately, because no cast or call setting changes what the CPU holds.
Can I keep the return in the interface type with a direct cast?
Yes. A direct cast such as (IS7Project3)simatic.Projects.Add(name, path, S7ProjectType.S7Project) performs the same interface query in one line and throws InvalidCastException instead of returning null when the object lacks the interface. Use the as-form with the null check while you are still sorting out references, because you can log and stop cleanly; in code that already runs reliably, the direct cast is compact and equally correct.
Do these libraries also drive TIA Portal projects?
No. S7HCOM_XLib and SimaticLib belong to the classic STEP 7 automation model around the SIMATIC Manager project, which is the model an S7-300 station upload uses. TIA Portal exposes its own automation interface with its own types, so ISimatic3, IS7Project3 and S7ProjectType do not exist there; automating TIA Portal means using the interface that product itself documents, not these interop libraries.