Reputation: 89
I have a windows service which is intended to listen on a COM port for incoming texts from a GSM modem, which then inserts that message into an SQL Database.
I've told my Service to log any exceptions to a text file on my server, and apparently it keeps throwing an exception saying the requested resource is in use.
I assume this is to do with the COM port, However, I can't see where I'm going wrong and I'd like some guidance on how to free up the COM Port and enable my code to function properly, my code is below, any and all help would be greatly appreciated.
I'm using a third party library called GSMComm to achieve this.
private SmsServer server;
private string appDataDirectory;
private string logFilePath;
GsmCommMain Comm = new GsmCommMain("COM7",9600,300);
GsmPhone GSM = new GsmPhone("COM7", 115200, 30000);
private void SMSGetter()
{
Log("Getter Fired");
//var message = GSM.ReadMessage(4);
//GSM.ReadMessage(4);
//TcpClientChannel client = new TcpClientChannel();
//ChannelServices.RegisterChannel(client, false);
//string url = "192.168.100.67:2000";
//ISmsSender smssender = (ISmsSender)Activator.GetObject(typeof(ISmsSender), url);
try
{
if (GSM.IsOpen()==false && Comm.IsOpen()==false)
{
GSM.Open();
Comm.Open();
DecodedShortMessage[] messages = Comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
SqlConnection Conn = new SqlConnection("Data Source=tcp:crusader,49172;Initial Catalog=HermesSMS;User ID=admin;Password=w");
SqlCommand com = new SqlCommand();
com.Connection = Conn;
Conn.Open();
com.CommandText = ("INSERT INTO SMSArchives(ID,Message,Blacklist) VALUES ('" + messages + "', 'Yes')");
com.ExecuteNonQuery();
Conn.Close();
GSM.Close();
Comm.Close();
return;
}
else if (GSM.IsOpen()==true && Comm.IsOpen()==true)
{
DecodedShortMessage[] messages = Comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
SqlConnection Conn = new SqlConnection("Data Source=tcp:crusader,49172;Initial Catalog=HermesSMS;User ID=admin;Password=w");
SqlCommand com = new SqlCommand();
com.Connection = Conn;
Conn.Open();
com.CommandText = ("INSERT INTO SMSArchives(ID,Message,Blacklist) VALUES ('" + messages + "', 'Yes')");
com.ExecuteNonQuery();
Conn.Close();
GSM.Close();
return;
}
}
}
Upvotes: 1
Views: 491
Reputation: 2653
Typically you cannot open a COM port in shared mode, so you cannot open COM7 twice and this is the reason for the exception.
Even if for some reason you could open the same COM port with two handles you have two baud rates specified, which means that one stream or the other would show up corrupted to your service.
You need to pick one or the other and stream all data through a single pipe. I don't know how your system is intended to work, but you need to read the documentation or contact the vendor to see how you are supposed to acquire data.
A possible fix is to simply eliminate all the GSM.xxx() calls from your code - it looks like you are just opening and closing that resource but it's never used in your code to acquire any data.
Upvotes: 1