|
Hi:
I am somewhat new to C# and I am trying to create a class that my forms use. The SCICommunication class contains a method called SCIgetCal. See below for code. My goal is to get the calibration data (X0g, X1g, Y0g, Y1g, Z0g, Z1g) from this function and work with it in my form class RawData.cs. I call it in my RawData.cs by the following: SCI_Communication.SciGetCal(X0g, X1g, Y0g, Y1g, Z0g, Z1g); How can I do this? What am I doing wrong? Do I need to add any headers or anything to RawData.cs to use this function? Please help!
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO.Ports;<br />
using System.IO;<br />
using System.Threading;<br />
<br />
namespace Triax_ReVamp<br />
{<br />
public partial class SCI_Communication<br />
{<br />
<br />
<br />
public static int SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g)<br />
{<br />
int readCal;<br />
int[] readXYZCalBytes = new int [9];<br />
<br />
COM_Port.SCIPort.Write("K");<br />
int readCalBytes = COM_Port.SCIPort.BytesToRead;<br />
if (readCalBytes == 9)<br />
{<br />
for (int j = 0; j < readCalBytes; j++)<br />
{<br />
readCal = COM_Port.SCIPort.ReadByte();<br />
readXYZCalBytes[j] = readCal;<br />
return 1; <br />
}<br />
if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90)<br />
{<br />
X0g = readXYZCalBytes[1];<br />
X1g = readXYZCalBytes[2];<br />
Y0g = readXYZCalBytes[4];<br />
Y1g = readXYZCalBytes[5];<br />
Z0g = readXYZCalBytes[7];<br />
Z1g = readXYZCalBytes[8];<br />
return 1;<br />
}<br />
else<br />
{<br />
X0g = Y0g = Z0g = 128;<br />
X1g = Y1g = Z1g = 189;<br />
return 0;<br />
}<br />
<br />
}<br />
}<br />
<br />
<br />
<br />
}<br />
}<br />
<br />
<br />
}<br />
Laura
|
|
|
|
|
You need to provide some additional information. Like what error are you getting? It would be helpful if you gave a slightly more comprehensive idea of what you are doing.
I have two comments:
1) In your first "for" loop you have a "return 1" statement. So you won't get to the rest of your code.
2) You may need to add references to the SCICommunications class. Visual Studio will complain if these references are needed.
Is SCICommunications class something you wrote, or is it from a third party?? I Googled it but I didn't find anything.
Best regards-
Squeaker
|
|
|
|
|
My goal in this project is to read in accelerometer data from a semiconductor board. The board responds to a protocol. If I write to the board a "K" it responds with 9 bytes of calibration data. The calibration data is as follows x, X0g, X1g, y, Y0g, Y1g, z, Z0g, Z1g. I am trying to create a class that has a built in function that writes a K to get the 9 calibration bytes. So that is why I wrote the class SciCommunications. I want to then call the function from a different class in order to get the calibration values in the RawData Class and other form classes. I am not getting an error but I am also not sure how to get the 9 calibration bytes in my RawData.cs. In my Raw Data I call it by doing
? = SCI_Communication.SciGetCal(X0g, X1g, Y0g, Y1g, Z0g, Z1g); Is this how I can get the 9 calibration bytes? What should I set the question mark to be, int or array? Hope this makes more sense!;P
Laura
|
|
|
|
|
Looking at your code for SciGetCal() it looks like it should work. Since this method returns an integer, you need to define a variable like ( int result; ) and then call your function like ( result = SciGetCal(...); }
1. Are you getting any errors?
2. Have you tried a normal serial communications program to see that the accelerometer board is working properly? HyperTerminal is the worst choice. If you need to get a simple one, I recommend Br@y Terminal. Free. And it doesn't install anything on your computer.
3. Do you have the serial port set up with proper baud rate, parity, stop bits, etc. ??
4. In Visual Studio you should put a break point when you start into your SciGetCal() method and step through the code line by line. This is pretty straight-forward since you only have a few lines of code. Are you using Visual Studio? What version?
5. Are you using Net 2.0??
Don't despair! Serial communications is tricky.
Regards-
Squeak
|
|
|
|
|
1. The following is the error that I am getting:
Error 4 'Triax_ReVamp.SCI_Communication.SciGetCal(int, int, int, int, int, int)': not all code paths return a value
I want it to be a boolean function.
2. I know that the serial communication is working, I have it working with a C++ program that we wrote previously which is very similair to this one and it works perfect!
3. I do have it set up properly: baud rate, stop parity, etc. I actually have this same code in my RawData.cs and it works. The reason I am trying to write a SCICommunication class is because I have multiple forms that will be calling the calibration function so I thought it would be better to have an outside serial class that has the function that I can call from all my forms.
4. I am using Visual Studio 2005 C#
I have attatched my code. I am trying to make it a boolean function, so that if it is successful in getting the calibration data it returns true, if it is not then it returns false.
Thanks so much for your help! I appreciate it!
<br />
public static bool SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g)<br />
{<br />
int readCal;<br />
int[] readXYZCalBytes = new int [9];<br />
<br />
COM_Port.SCIPort.Write("K");<br />
int readCalBytes = COM_Port.SCIPort.BytesToRead;<br />
if (readCalBytes == 9)<br />
{<br />
for (int j = 0; j < readCalBytes; j++)<br />
{<br />
readCal = COM_Port.SCIPort.ReadByte();<br />
readXYZCalBytes[j] = readCal;<br />
<br />
}<br />
if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90)<br />
{<br />
X0g = readXYZCalBytes[1];<br />
X1g = readXYZCalBytes[2];<br />
Y0g = readXYZCalBytes[4];<br />
Y1g = readXYZCalBytes[5];<br />
Z0g = readXYZCalBytes[7];<br />
Z1g = readXYZCalBytes[8];<br />
return true;<br />
}<br />
else<br />
{<br />
X0g = Y0g = Z0g = 128;<br />
X1g = Y1g = Z1g = 189;<br />
return false;<br />
}<br />
<br />
}<br />
Laura
|
|
|
|
|
"not all code paths return..." means that you have some "if" statements and there is some possible condition where you can get to the end of your Method without encountering a "return" statement.
I can't follow the braces in your code, but you probably need a return somewhere. Look at your statement: if (readCalBytes == 9) If the condition is false then there is no return. I think a return belongs after your last brace.
You have to make incremental discovery.
1. Can you verify that the "K" command that you are sending out is actually being transmitted??? With a scope, a terminal program, or perhaps a LED indicator on the acc. board???
2. Did you try putting a break point at: readCal = COM_Port.SCIPort.ReadByte();
Squeak
|
|
|
|
|
This is the reason I always try to minimize the exit points from a method or function. The reason for the error message is that a return statement is missing at the end of the method. Put a "return false;" statement just before the last brace and your method will not have this problem.
Phil
|
|
|
|
|
By the way, Step 2 in my previous message; about verifying that the accelerometer board is communicating by using a known good terminal program is the most important step you can take.
Because if something is wrong with it or the cable, you can work for a year on your program and you will find no joy.
Joy is important!
Squeak
|
|
|
|
|
:-DI am just starting to learn C#. I would appreciate if anybody could suggest some beginners books to study. If you happen to know the author or the ISBN number that would help also. Thank you.
|
|
|
|
|
I'm also quite new to C# but I find "Programming Microsoft Visual C# 2005: The Language" by Donis Marshall (ISBN-10: 0-7356-2181-0, published by Microsoft Press) to be a pretty good reference. It isn't for the raw beginner but if you are familiar with programming and object-oriented concepts it is pretty good.
---
"No one would surrender to the Dread Pirate Westley."
|
|
|
|
|
See Richter's "CLR via C#" Second Edition. The online C# language specifications aren't a bad introduction (if you already understand OOP), and they're free. If you are dipping clear down to raw C syntax, it's not a bad idea to casually study early sources such as "The C Programming Language." But really, learning the language is the easy part. Practice requires learning the libraries -- and there isn't much help there but in library documentation.
|
|
|
|
|
I'd start with C# in Easy Steps.
Only $10, and very basic... but everybody needs to start with the basics, don't they? Gives information in a very clear form.
By Tim Anderson, ISBN 0-7607-5733-X.
(I found mine at a Barnes and Noble store.)
|
|
|
|
|
You know, for my money, the Troelsen book "Pro C# 2005 and the .NET 2.0 Platform" (ISBN 1-59059-419-3), published by Apress, is a good investment particularly if you pick things up quickly. It is a thick book but very well written.
Have fun,
Matt
|
|
|
|
|
Hi,
I have saved a DateTime object to a configuration file using the DateTime.ToString() method. I now want to read the setting and create a new DateTime object from it. How is this possible? I need both the date and time. I think I could store the day, date, month, year, hours and minutes but this seems overkill and would mean me reading many settings from my configuration file. Surely there is a better solution?
Thanks
|
|
|
|
|
Use the DateTime.TryParse or DateTime.Parse method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
If you want to store the value in a simple format, use the Ticks property to get an Int64 value. This can be used in the DateTime constructor to recreate the DateTime value exactly.
---
single minded; short sighted; long gone;
|
|
|
|
|
hey guys,
what`s the fastest way to use my C# methods within my VB6 app? I`ve done it before, but i can`t remember where exactly i found the article about how to accomplish it.
do you know any reference about the subject?
thanks in advance
cheers
|
|
|
|
|
Just reference the C# DLL in your VB App and start programming.
Brent
|
|
|
|
|
with VB6, you'll need to compile your C# as a COM object, or a completely independent app and use standard windows interprocess communications to talk between then. It's only easy if you're using VB.net since it uses the same runtime.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
Like Dan said, you have to decorate your C# component with the proper COM appointments in order to use it in VB6. COM is the only interface you have to enable your component to work with VB6. C#, or and managed code library for that matter, doesn't support the necessary exports to use simple Declare statements in your VB6 code.
Keep in mind, that when you use a .NET component in your VB6 code, you're bringing with it the entire weight of the .NET CLR. Be prepared to see your memory counters jump up considerably.
You can learn a bit more about this process in this article[^] on MSDN. The .NET code is in VB.NET, but is easily translatable to C#.
There's more in the next article[^].
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
2 questions:
1. where can I find explanations about Excel and Word namespace and how to use office classes?
2. how can I control things not inside my form like
adding the ability when clicking right click on the desktop that another function will appear in the context menu.
thanks
|
|
|
|
|
Hi everybody,
I'm building a c# application that reads data messages from an external device at a rate of 1MBit/sec.
I need to log in a window all messages.
I tried to redirect console output to a textbox, but the textbox was not fast enough to update and a half of messages were lost.
I have a thread that read continuously from the device and put messages on a buffer. A consumer thread read messages and pass them to the textbox.
So...I need a very fast logging window and, if someone will give me some suggestion to speed up .Net controls it will be great for me.
|
|
|
|
|
A textbox cannot 'lose' messages. You need to write code to cache the messages, that's where they are being lost. The textbox should just display them.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thank you Christian...
I would say that i can't add a new line to the textbox each 300-400 microsecond...
If I'm not wrong you're suggesting me to bufferize a number of messages then update text in textbox only after this number, is it?
Sorry for my bad english
|
|
|
|
|
If you can't write to the control as fast as your data stream requires, even bufferizing hasn't got a prayer of helping you. You need to revise your design to use a control that can keep up with the stream. Why you aren't writing to a console isn't clear. I hope nobody is supposed to read all this at that rate... so why the text box?
|
|
|
|