Help with my class using Serial Port Class
-
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! :sigh:
// SciGetCal function in SCI_Communication Class using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.IO; using System.Threading; namespace Triax_ReVamp { public partial class SCI_Communication { public static int SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; return 1; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return 1; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return 0; } } } } } }
Laura -
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! :sigh:
// SciGetCal function in SCI_Communication Class using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.IO; using System.Threading; namespace Triax_ReVamp { public partial class SCI_Communication { public static int SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; return 1; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return 1; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return 0; } } } } } }
LauraYou 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
-
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
-
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
-
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
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
-
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!:-D
public static bool SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return true; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return false;
-
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!:-D
public static bool SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return true; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return false;
"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
-
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!:-D
public static bool SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return true; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return false;
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