Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to use an ini file to set all the parameters of a Serial com port.

C#
foreach (string v in Values)
             {
                   a++;
                     char[] separator = { '=' };
                     string[] Key = v.Split(separator);
                     if (Key.Length != 2) continue;

                     Key[0] = Key[0].Trim();
                     Key[1] = Key[1].Trim();
                     if (Key[0] == "EUIUBOARD")
                     {
                         PSIONCommPort = Key[1];
                     }
                     else if (Key[0] == "BOARDRATE")
                     {
                         PSIONBaudRate = Convert.ToInt16(Key[1]);
                     }
                     else if (Key[0] == PSIONParity)
                     {
                         PSIONParity = Key[1];
                     }
                     else if (Key[0] == "DATABITS")
                     {
                         PSIONDataBits = Convert.ToInt16(Key[1]);
                     }


             }

this is fine but I need to set the Handshake, StopBits & Parity. Which are ennumerated lists ( I think!) I need to do Handshake.None as a varible which is causing some problems.
Thanks and sorry for the earlier problems!
Posted
Comments
Sergey Alexandrovich Kryukov 31-Oct-11 12:10pm    
Not clear what the problems is -- it seems trivial.
--SA

Store Handshake, StopBits & Parity as strings using the names from their respective enumerations, you can then use Enum.Parse[^] to convert to the desired value.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
glennPattonWork3 31-Oct-11 12:17pm    
Ahhh, thank you!
Espen Harlinn 31-Oct-11 15:06pm    
Excellent :)
Sergey Alexandrovich Kryukov 31-Oct-11 12:30pm    
Right method, my 5. This is better than my method where I used cycle. I also referenced my article on enumerations for more advanced techniques.
--SA
Espen Harlinn 31-Oct-11 15:06pm    
Thank you, Sergey :)
Nish Nishant 31-Oct-11 12:33pm    
I approve and endorse this reply! :-)
Are you missing the existence of Enum.Parse?

Handshake handshake = (Handshake)Enum.Parse(typeof(Handshake), Key[1]);


Or make yourself a utility method

public static T ParseEnum<T>(string value) { return (T)Enum.Parse(typeof(T), value); }


and call it

Handshake handshake = ParseEnum<Handshake>(Key[1]);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Oct-11 12:30pm    
My 5.
--SA
Espen Harlinn 31-Oct-11 15:10pm    
Yes, something like that :)
RaisKazi 1-Nov-11 10:39am    
My 5.
Not clear what is the problem. If would only recommend to get rid of all immediate constants, especially strings you hard-coded in your sample like "EUIUBOARD", "BOARDRATE", etc. (Also, there is no such work "boardrate", its "baud rate", see http://en.wikipedia.org/wiki/Baud_rate[^].) A compiler won't detect a problem if you misspell it, change it during support. So, this is not maintainable. One of the robust ways is using enumeration types.

For example:

C#
enum Key = {
    BoudRate = 0, //0 by default, assigned just to indicate is should not be modified if you want to use Length
    DataBits,
    StopBits,
    FlowControlRtsSts,
    FlowControlDtrDsr,
    FlowControlXonXOff,
    //?
    Length, //this is not a semantic but a "technical" member, see below
}

string iniKey = //... something from INI file you want to compare, like your Key[0]...
for (Key key = 0; key <length,>    if (iniKey.ToUpper() == key.ToString().ToUpper()) {
        // you have a match, process value now
        //...
        break;
    } //if
    //...
}


You see, not a single immediate string constant is hard-coded here. The key name is got from the enumeration member via key.ToString.

For simple tasks this is good enough, but you should remember the special meaning of Length as you mix semantic enumeration members with non-semantic Length, cannot use non-consecutive enumeration members, a value range starting from a non-zero value. Sometimes you need all that. So, for more advanced techniques, please read my article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

[EDIT]

As to INI storage, you can save a lot of development time if you cut it down and switch to more comprehensive technology. The best thing I know is using Data Contract. With this approach, you won't have to care about persistence of the data at all, but the class System.Runtime.Serialization.DataContractSerializer or System.Runtime.Serialization.Json.DataContractJsonSerializer will. You will only need to code data classes representing data and add DataContract and DataMember attributes to what you need to be a part of a contract. See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^] (using Data Contracts),
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^], see also related topics.

This approach will make you completely free from manual XML or JSON programming; it will also give you highly maintainable code, with wonderful mechanism of backward compatibility and more.

Good luck,
—SA
 
Share this answer
 
v3
Comments
glennPattonWork3 31-Oct-11 13:11pm    
Will have to look into this with XML and other type noe out of hole that I was in today!
Sergey Alexandrovich Kryukov 31-Oct-11 16:11pm    
Do you mean instead of INI? Certainly. However, my advice has nothing to do with that -- it just shows how to use enumeration instead of strings and explains why. I can be applicable to many technologies of storing configuration, meta-data, etc.

However... [to be continued...]
--SA
Sergey Alexandrovich Kryukov 31-Oct-11 16:24pm    
...however, if you use Data Contract, all the configuration problems will be solved automatically: you only create data classes which reflects your configuration data, the object graph will be stored and restored automatically.
--SA
Espen Harlinn 31-Oct-11 15:11pm    
Something like this has to take place inside Enum.Parse :)
Sergey Alexandrovich Kryukov 31-Oct-11 16:09pm    
No, I would say, Enum.Parse would be better. I just want to describe the general idea. As I explained in my article, both approaches would cause problems for some enumeration definitions, so I explained what to do in my article referenced above.
Thank you anyway.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900