static void GetAvailableComPorts() { Console.WriteLine("Open Serial/COM ports for use:"); if (SerialPort.GetPortNames().Length > 0) { foreach (string serialPort in SerialPort.GetPortNames()) { GsmCommunication com = new GsmCommunication(serialPort); try { com.OpenPort(); if (com.CheckPort()) { string phoneNumber = com.GetNumber(); string imei = com.GetImei(); Console.WriteLine("Port {0} is available for use.", serialPort); Console.WriteLine("Port {0} SIM card number is: {1}", serialPort, phoneNumber); Console.WriteLine("Port {0} IMEI number: {1}", serialPort, imei); } } catch (Exception e) { Console.WriteLine("[Error] exception occured: {0}", e.Message); } finally { com.ClosePort(); } } } else { Console.WriteLine("No available COM ports for use now."); } }
public void Start() { module.OpenPort(); //Register phone number on server as it is our first interaction with SIM card dynamic phonenum = new JObject(); phonenum.number = PhoneNumber; phonenum.imei = IMEI; string registerPhoneJson = JsonConvert.SerializeObject(phonenum); RegisterPhone(registerPhoneJson); Task.Factory.StartNew( () => { while (true) { Thread.Sleep(delay); CheckStatus(); } }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
static void Main(string[] args) { while (true) { if (SerialPort.GetPortNames().Length > 0) { foreach (string serialPort in SerialPort.GetPortNames()) { GsmCommunication com = new GsmCommunication(serialPort); try { com.OpenPort(); if (com.CheckPort()) { string phoneNumber = com.GetNumber(); string imei = com.GetImei(); Console.WriteLine("[Main] Port {0} is available for use.", serialPort); Console.WriteLine("[Main] Port {0} SIM card number is: {1}", serialPort, phoneNumber); Console.WriteLine("[Main] Port {0} IMEI number: {1}", serialPort, imei); Module GSM = new Module(serialPort); GSM.IMEI = imei; GSM.PhoneNumber = phoneNumber; //Find existing module by IMEI - if not found we add new if (!modules.Exists(ByIMEI(imei))) { modules.Add(GSM); } } } catch (Exception e) { Console.WriteLine("[Main][Error] exception occured: {0}", e.Message); } finally { com.ClosePort(); } } } else { Console.WriteLine("[Main] No available COM ports for use now."); } foreach (var module in modules) { if (!module.Running) { module.Start(); module.Running = true; } } Console.WriteLine("[Main][Info] Modules total connected: {0}", modules.Count); Thread.Sleep(MAIN_DELAY); } }