Hi,
I am trying to access a file in two different classes simultaneously. Is there any solution ?
thank you in advance,
Regards, Syed Ahmed Hussain 9 2116
For what purposes? Read in both, write in both? What have you tried so far?
Sorry guys I was actually in a bit of hurry...
I am using sensors to get the humidity and temperature values in a hexadecimal string. I am writing those values into a file. And at the same moment this file is accessed from another class to get the values and convert them into integer valuesand then writing a delimiter in the file in order to remember that upto where I have read the file. But the problem is actually when I run the code it gives me error on file close.
Is there any solution??
This is of my first class where I am getting the values from the sensor... and writing it in a file.... -
-
-
private void separateTemperaturenHumidity(string reading)
-
{
-
Int32 RawTemp = 0;
-
Int32 RawHum = 0;
-
Int32 RawLight = 0;
-
double TempinDegC = 0;
-
double RelHuminPer = 0;
-
double LightinLx = 0;
-
string NodeID, Temperature, Humidity, Light;
-
-
-
// Node ID
-
NodeID = reading.Substring(28, 4);
-
// NodeID = NodeID + reading.Substring(45, 2);
-
-
// Light Reading
-
Light = reading.Substring(36, 4);
-
// Light = Light + reading.Substring(57, 2);
-
-
// Temperature Reading
-
Temperature = reading.Substring(40, 4);
-
// Temperature = Temperature + reading.Substring(63, 2);
-
-
// Humidity Reading
-
Humidity = reading.Substring(44, 4);
-
//Humidity = Humidity + reading.Substring(69, 2);
-
-
-
-
//Converting string into Decimal value
-
RawTemp = Convert.ToInt32(Temperature, 16);
-
RawHum = Convert.ToInt32(Humidity, 16);
-
RawLight = Convert.ToInt32(Light, 16);
-
-
// Converting Raw Temperature into Degree Centigrade
-
TempinDegC = 0.01 * RawTemp - 39.6;
-
-
// Converting Raw Humidity into Relative Humidity in Percentage
-
RelHuminPer = (TempinDegC - 25) * (0.01 + 0.00008 * RawHum) + (-4 + 0.0405 * RawHum - 0.0000028 * RawHum * RawHum);
-
-
// Converting Raw Light into Lux unit
-
double V = (float)RawLight / 4096 * 1.5;
-
double I = V / 100000;
-
-
LightinLx = 1e6 * I * 1000;
-
-
-
// Converting values into strings
-
string Str_T = RawTemp + "";
-
string Str_TC = TempinDegC + "";
-
string Str_H = RawHum + "";
-
string Str_RH = RelHuminPer + "";
-
string Str_L = RawLight + "";
-
string Str_Llx = LightinLx + "";
-
-
// Writing to File namely SensorData.txt
-
writeFile(reading, NodeID, Str_T, Str_TC, Str_H, Str_RH, Str_L, Str_Llx);
-
}
-
-
private void writeFile(string str_packet, string NID, string RT, string TC, string H, string RH, string L, string LLx)
-
{
-
try
-
{
-
//Pass the filepath and filename to the StreamWriter Constructor
-
-
StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\syed\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\sensordatapackets.text", true);
-
//Write a line of text
-
sw.WriteLine(str_packet + " " + DateTime.Now);
-
//Close the file
-
sw.Close();
-
-
sw = new StreamWriter(@"C:\Documents and Settings\syed\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\" + NID + "datapackets.text", true);
-
//Write a line of text
-
sw.WriteLine(str_packet + " " + DateTime.Now);
-
//Close the file
-
sw.Close();
-
-
}
-
catch (Exception e)
-
{
-
rtfTerminal.AppendText(e.StackTrace);
-
System.Console.Write(e.StackTrace);
-
MessageBox.Show("Exception: " + e.StackTrace);
-
-
}
-
-
}
-
-
And the below one is from another class... -
-
-
public void getUpdate()
-
{
-
char marker = '=';
-
string delimitData = null;
-
int k = 0;
-
int j = 0;
-
-
-
FileStream fs = new FileStream(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\Sensordatapackets.text", FileMode.Open);
-
-
StreamReader sr = new StreamReader(fs);
-
StreamWriter sw = new StreamWriter(fs);
-
data = sr.ReadToEnd();
-
j = data.Length -1;
-
while ((j >= 0)&&(!(data[j].Equals('='))))
-
{
-
delimitData= delimitData+ data[j];
-
j--;
-
}
-
-
char[] c = delimitData.ToCharArray();
-
Array.Reverse(c);
-
delimitData = new String(c);
-
this.packetSplitter(delimitData);
-
sw.WriteLine(marker);
-
k++;
-
-
-
}
-
-
-
public void packetSplitter(string args)
-
{
-
Convertor convert = new Convertor();
-
-
string sensorID;
-
string splitter = "\r\n";
-
char replacementSplit = '-';
-
string[] seperatePacketsTimeStamp = new string[3];
-
int i = 0;
-
char seperator = ' ';
-
string timepatch;
-
-
args = args.Replace(splitter, replacementSplit.ToString());
-
string[] packets = args.Split(replacementSplit);
-
int length = packets.Length;
-
-
while (i < length)
-
{//seperator will be used here in order to seperate the packets from eachother...
-
seperatePacketsTimeStamp = packets[i].Split(seperator);
-
-
if (seperatePacketsTimeStamp.Length==1)
-
{ i++; }
-
else
-
{
-
sensorID = seperatePacketsTimeStamp[0].Substring(28, 4);
-
-
timepatch = seperatePacketsTimeStamp[2] + ' ' + seperatePacketsTimeStamp[3];
-
-
this.DBCheckAndFill(timepatch, seperatePacketsTimeStamp[1], seperatePacketsTimeStamp[0], sensorID);
-
i++;
-
}
-
-
}
-
-
}
-
Just a reminder
both class should run simultaneously. and then their would be an other class of convertor as well which will convert the values but thats the part for the DB...
thanks again
Regards, Syed Ahmed Hussain
I am using sensors to get the humidity and temperature values in a hexadecimal string. I am writing those values into a file. And at the same moment this file is accessed from another class to get the values and convert them into integer valuesand then writing a delimiter in the file in order to remember that up to where I have read the file.
Is there a reason other than the passing of the values, for writing this data to a file? This is a rather clunky and slow way to pass data.
I would suggest you have only one class that accesses the file. Give it methods for reading and writing and updating. Your other classes can pass data to it, and ask for data from it.
But the problem is actually when I run the code it gives me error on file close
And that error would be.... ????
That error comes when the first class closes the file. I dont exactly remember the error and I cant provide you the error right now because I am using Virtual PC. And its not enabling the hardware virtualization, so I can check the error when sensor passes the values.
Is there any one who could help me out in VPC problems as well. I have to submit this on monday and I am totally stucked here.
Regards,
Syed Ahmed Hussain
I use VMware, sorry.
That error comes when the first class closes the file.
Right. You mentioned that. And it makes sense, since two files have the file open, then one closes it and the other is still expecting it to be open. Hence the earlier suggestion to only have one class controlling one file. Did you give that a try?
I have to submit this on monday
Homework assignment?
I will ask again
Is there a reason other than the passing of the values, for writing this data to a file? This is a rather clunky and slow way to pass data.
And at the same moment this file is accessed from another class to get the values
If you want to write the data to a file so you can have a history, that's one thing... but its silly to read them back from another class in the same application. There are a number of ways to pass them directly from one class to another, completely eliminating the cause of your two-class-reading-one-file problem.
Put the data in a custom arguments class.
Raise an event with the arguments.
Have the second class subscribe to this event.
Every time class1 raises the event with the arguments, class2 will react. Just like reacting to button_click event.
Dear tlhintoq,
You can say its my assignment, because I am doing my internship in CRUC (Center of Research in Ubiquitous Computing) . I am working on Ontology . We are actually creating a software that will use a reasoner in .NET to reason certain information which will be provided.
And you got that right that when the file is closed other classes are using it.
Perhaps there is a reason for writing it into a file. Can we use any file locking instead of changing the classes. I totally agreed with you for making an event handler or do all in a single class, but I talked to my professor and he asked me that he dont want to change the class schema.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tom Loredo |
last post by:
Hi folks-
I'm about to move from a Solaris 8/SPARC environment to a Dell running
RedHat 9. Are there any issues I need to be aware of in bringing my
Python code over (mostly scientific...
|
by: malcolm |
last post by:
Hello,
We have a robust (.NET 1.1 c# winforms) client-server application that
utilizes many typed DataSets, typed DataTables and typed DataRows. Our
application is a series of windows and popup...
|
by: j. shepherd |
last post by:
I am looking for a filing software for the office. I need to get all
of the files in the office entered into some type of software or
database, that I can pull up scanned documents, location of...
|
by: editprod |
last post by:
I'd like to create an Access database for my the central filing system
of my office. My boss can never find where files are. We have approx.
2000 files in a dozen file drawers, each drawer with a...
|
by: RANIA |
last post by:
i wana make a program in which user find the index of agiven string through filing and also user can delete the given string from file replace and isert a string into fil.
|
by: GaryDean |
last post by:
We have been developing all of our .net applications on 32 bit windows using
32 bit SQL Server. We are being asked to now deploy to servers running
64bit windows and 64bit SQL Server. Are there...
|
by: reddog |
last post by:
Can anyone direct me to a site (or help me) that will tell me how to set up
Direct Digitial Filing? Iam running Windows XP.
Thank You for Your Valued Time
Reddog
|
by: Chicago |
last post by:
$9.95 Online Tax Filing www.MHBSgloabl.com
|
by: eschneider |
last post by:
Just some common issues with WS:
Using custom objects: When objects change, seems you are always fixing some
issue. Update references, which sometimes does not work.
Deployment:
Weird errors...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |