Real time event handler in ZKemKeeper library not responding - c#

I have created a Windows Service to fetch attendance by using real time event from a fingerprint device using Interop.zkemkeeper library. In service Machine is successfully connected but the service is not responding to the OnAttTransactionEx real time event means that after succesfull connection to machine attendance is not fetched using OnAttTransactionEx event. I don't know what is the problem.
Here's the code for Windows Service:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.IO;
using zkemkeeper;
using System.Threading;
using System.Windows.Forms;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
// private System.Timers.Timer timer1 = null;
string filePath = #"E:\file1.txt";
bool connSatus = false;
CZKEMClass axCZKEM1;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
/* timer1 = new Timer();
this.timer1.Interval = 10000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
*/
axCZKEM1 = new zkemkeeper.CZKEMClass();
Thread createComAndMessagePumpThread = new Thread(() =>
{
connSatus = axCZKEM1.Connect_Net("192.169.9.34", 4370);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Machine is connected on" + "Date :" + DateTime.Now.ToString() + "status" + connSatus);
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
if (connSatus == true)
{
this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
if (axCZKEM1.RegEvent(1, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
{
this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("finger print Event is registered... ");
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
Application.Run();
});
createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);
createComAndMessagePumpThread.Start();
}
public void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
{
// if (OnAttTransactionEx != null) OnAttTransactionEx(sEnrollNumber, iIsInValid, iAttState, iVerifyMethod, iYear, iMonth, iDay, iHour, iMinute, iSecond, iWorkCode, axCZKEM1.MachineNumber, Tag);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine(" OnAttTrasactionEx Has been Triggered,Verified OK on" + "Date :" + "Enrollnumber" + sEnrollNumber + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
protected override void OnStop()
{
// timer1.Enabled = false;
this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
axCZKEM1.Disconnect();
}
private void timer1_Tick(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message is running on" + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
}

corret version is that
protected override void OnStart(string[] args)
{
Thread createComAndMessagePumpThread = new Thread(() =>
{
axCZKEM1 = new zkemkeeper.CZKEMClass();
connSatus = axCZKEM1.Connect_Net("192.169.9.34", 4370);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Machine is connected on" + "Date :" + DateTime.Now.ToString() + "status" + connSatus);
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
if (connSatus == true)
{
this.axCZKEM1.OnAttTransactionEx -= new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
if (axCZKEM1.RegEvent(1, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
{
this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("finger print Event is registered... ");
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
Application.Run();
});
createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);
createComAndMessagePumpThread.Start();
}

Related

Crash in MouseKeyHook

can some one take a look to my C# source that has a crash issue? The program is supposed to omit unwanted double clicks the mouse sometimes sends and it works but after a while using the program it crashes.
The line that crashes:
Application.Run(new TheContext());
This is the error code:
An unhandled exception of type 'System.NullReferenceException' occurred in Gma.System.MouseKeyHook.dll
I'm using Visual studio community 2017
Source:
program.cs:
https://pastebin.com/AX9VRi00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace MouseFixer
{
static class Program
{
static Mutex pmu;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Console.WriteLine("Hello");
try
{
Mutex.OpenExisting("MouseFixer");
MessageBox.Show("MouseFixer is already running", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
catch
{
pmu = new Mutex(true, "MouseFixer");
}
Application.Run(new TheContext());
}
}
}
thecontext.cs:
https://pastebin.com/G1cNzj4d
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using System.Diagnostics;
namespace MouseFixer
{
public class TheContext : ApplicationContext
{
// https://stackoverflow.com/questions/30061813/intercept-mouse-click
StringBuilder logText;
long lastClickTime;
long lastMouseUp;
bool ignoreClick = false;
void writeLog(string msg)
{
logText.Append(msg + Environment.NewLine);
File.AppendAllText("log.txt", logText.ToString());
logText.Clear();
}
bool errorShown = false;
void errorMsg(string str)
{
if(!errorShown)
MessageBox.Show(str);
errorShown = true;
}
long getTime()
{
return DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
public TheContext()
{
Application.ApplicationExit += new EventHandler(this.OnExit);
logText = new StringBuilder();
lastClickTime = getTime();
lastMouseUp = getTime();
Hook.GlobalEvents().MouseDownExt += async (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
// e.Handled = true;
// writeLog("Handling click DOWN! " + e.Delta);
long lmu = (getTime() - lastMouseUp);
if (lmu < 10)
{
Debug.WriteLine("Too fast click - ignoring " + (getTime() - lastMouseUp) + Environment.NewLine);
e.Handled = true;
ignoreClick = true;
}
long lct = getTime() - lastClickTime;
lastClickTime = getTime();
Debug.WriteLine("MouseDOWN " + lct + " ( " + lmu + " ) " + Environment.NewLine);
}
};
Hook.GlobalEvents().MouseUpExt += async (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
if (!ignoreClick)
{
// e.Handled = true;
// writeLog("Handling click UP! " + e.Delta);
long lct = getTime() - lastClickTime;
lastClickTime = getTime();
Debug.WriteLine("MouseUP " + lct + Environment.NewLine);
lastMouseUp = getTime();
}
else
{
Debug.WriteLine("Ignoring click " + Environment.NewLine);
e.Handled = true;
ignoreClick = false;
}
}
};
}
private void OnExit(object sender, EventArgs e)
{
// File.AppendAllText("log.txt", logText.ToString());
}
}
}

Csvhelper write with windows service

I am using csvhelper with windows service which runs non stop. I want to create a new csv log file and put the header values only once until the file size grown to 1000 kb and then create another new file so the file size grow too big. At the moment csv file is created once when windows service starts but it repeats header values every time it writes into it.
Do we have some config properties in csv helper to flag size file?
Please have a look at the code and suggest if it can be done efficiently in .net 4.0.
Windows Service code
public partial class Service1 : ServiceBase
{
private Thread executeThread;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
Thread.Sleep(30000);
executeThread = new Thread(new ThreadStart(Execute));
executeThread.Start();
}
catch (Exception e)
{
Library.LogData("Error : " + DateTime.UtcNow + " " + e.Message);
OnStop();
}
}
protected override void OnStop()
{
try
{
Library.LogData("Stopped: " + DateTime.UtcNow);
executeThread.Abort();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public void Execute()
{
try
{
Library.LogData("Started");
while (true)
{
Library.LogData("Logging Execute" + DateTime.UtcNow);
Thread.Sleep(5000);
}
}
catch (Exception e)
{
Library.LogData("Error : " + DateTime.UtcNow + " " + e.Message);
OnStop();
}
Library.LogData("Out of Execute" + DateTime.UtcNow);
}
}
Logging code called by windows service
public static class Library
{
public static void LogData(string Message)
{
StreamWriter sw = null;
GetData getData = new GetData();
var dataClasslist = new List<DataClass>
{
new DataClass {}
};
try
{
dataClasslist = getData.ReadData();
using (sw = new StreamWriter("E:\\DataLogs\\LogFile.csv", true))
using (var csv = new CsvWriter(sw))
{
csv.Configuration.RegisterClassMap<DataClassMap>();
csv.WriteHeader<DataClass>();
csv.Configuration.TrimHeaders = true;
csv.Configuration.HasHeaderRecord = false;
csv.WriteRecords(dataClasslist);
sw.Flush();
sw.Close();
}
}
catch (Exception e)
{
var err = new StreamWriter("E:\\DataLogs\\Errors", true);
err.WriteLine(DateTime.Now.ToString() + ": " + e.Message.ToString());
err.Flush();
err.Close();
}
}
}
Main windows service
public partial class Service1 : ServiceBase
{
private Thread executeThread;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
Thread.Sleep(10000);
executeThread = new Thread(new ThreadStart(Execute));
executeThread.Start();
}
catch (Exception e)
{
var err = new StreamWriter("E:\\DataLogs\\Errors", true);
err.WriteLine(DateTime.Now.ToString() + " OnStart: " + e.Message.ToString());
err.Flush();
err.Close();
OnStop();
}
}
protected override void OnStop()
{
try
{
executeThread.Abort();
}
catch (Exception e)
{
var err = new StreamWriter("E:\\DataLogs\\Errors", true);
err.WriteLine(DateTime.Now.ToString() + "OnStop exception: " + e.Message.ToString());
err.Flush();
err.Close();
}
}
public void Execute()
{
try
{
StreamWriter sw = null;
string FileNameAndPath = null;
FileNameAndPath = "E:\\DataLogs\\LogFile" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";
using (sw = new StreamWriter(FileNameAndPath, true))
{
sw.WriteLine("TimeStamp " + "," + "col1" + "," + "col2" + "," + "col3"
+ "," + "col4" + "," + "col5" + "," + "col6" + ","
+ "col8" + "," + "col9" + "," + "col7" + "," + "col10");
sw.Flush();
sw.Close();
}
while (true)
{
Library.LogData(FileNameAndPath);
Thread.Sleep(5000);
}
}
catch (Exception e)
{
var err = new StreamWriter("E:\\DataLogs\\Errors", true);
err.WriteLine(DateTime.Now.ToString() + " Execute: " + e.Message.ToString());
err.Flush();
err.Close();
OnStop();
}
}
public void TestInConsole(string[] args)
{
Console.WriteLine("Starting.....");
this.OnStart(args);
}
So OnExecute will be called as soon as file hits 100kb and new file will be created with headers written in it only once.
public static class Library
{
public static void LogData(string FileNameAndPath)
{
StreamWriter sw = null;
GetData getData = new GetData();
var dataClasslist = new List<DataClass>
{
new DataClass {}
};
try
{
//System.Diagnostics.Debugger.Break();
dataClasslist = getData.ReadData();
//Create a file seperately
using (sw = new StreamWriter(FileNameAndPath, true))
using (var csv = new CsvWriter(sw))
{
csv.Configuration.RegisterClassMap<DataClassMap>();
//csv.WriteHeader<DataClass>();
csv.Configuration.TrimHeaders = true;
csv.Configuration.HasHeaderRecord = false;
csv.WriteRecords(dataClasslist);
sw.Flush();
sw.Close();
}
var FileSize = new FileInfo(FileNameAndPath);
//for each 1000 kb
if (FileSize.Length >= 1e+6)
{
//go back to the start to create a new file and column headers
Service1 serive1 = new Service1();
serive1.Execute();
}
}
catch (Exception e)
{
var err = new StreamWriter("E:\\DataLogs\\Errors", true);
err.WriteLine(DateTime.Now.ToString() + " LogData: " + e.Message.ToString());
err.Flush();
err.Close();
}
}
}
Im open for suggests, please let me know if you can thing of a better way to to do this.

Unable to start Windows Service if reference is used

I have create a windows service. when i try to start the service, i recieve the error
Windows could not start the Auto Process service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion.
This is my code
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Timers;
using Drone.Engine; //
namespace Auto
{
public partial class Service1 : ServiceBase
{
static string month = DateTime.Now.ToString("MMM");
private Timer timer = new Timer();
private Dictionary<string, bool> processStatus = new Dictionary<string, bool>();
private Executor worker = new Executor(); //
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
processStatus.Add("start", false);
processStatus.Add("stop", false);
System.Threading.Thread.Sleep(10000);
WriteToFile("Service Started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 2000;
timer.Enabled = true;
}
private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
foreach (var process in processStatus.Where(v => v.Value == false))
{
WriteToFile(process.Key + " Sync Started at " + DateTime.Now);
ChangeProcessStatus(process.Key, true);
worker.Execute(Executor.sender.Navision, process.Key); //
ChangeProcessStatus(process.Key, false);
WriteToFile(process.Key + " Sync Finished at " + DateTime.Now);
}
}
protected override void OnStop()
{
WriteToFile("Service stopped at " + DateTime.Now);
}
private void ChangeProcessStatus(string key, bool status)
{
processStatus[key] = status;
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs\\ServiceLog_" + DateTime.Now.Date.Day + " " + month + ".log";
if (!File.Exists(filepath))
{
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}
But if i remove the lines
using Drone.Engine;
The service is starting successfully. Why am i unable to use it in the service?
The dll is present in the same directory. I have used the release build for installing. Still this issue.
Previously i was using filepaths like
filePath=#"data\log";
This was working fine in my application. But when i reference it to my windows service, i was getting System.IO.DirectoryNotFoundException error.
I changed the filepath to
filepath=AppDomain.CurrentDomain.BaseDirectory + "\\data\\logs";
It worked for both my service and application.

Try to launch an application from another application continue to crash

I wrote a program in C # that takes, from a WPF interface, a folder path, a prefix, and a prefix of substitution. The purpose of the program is to search in the folder all the files that start with the prefix and rename them with the new prefix. This program works without any problems.
Now I have to write a second program that must call the previous, passing the parameters using args []. I wrote this second program, it seems that all the parameters have passed correctly to the other (I checked it), but every time the replacement program runs out after a few seconds (while the normal run in my folder of Trial is almost instantly). I'm not able to have any message error or exception, I can only obtain the windows alert message that reports the crash.
Here is the significant part of the code of both programs ... Can anyone help me to find the problem? Is it a problem with settings passed from one program to another? Thanks a lot to everyone.
Can it maybe be a problem about the setting parameters of the calling?
Replace prefix program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace replace_prefix
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] args;
args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
((MainWindow)App.Current.MainWindow).Close();
sf_textBox.Text = args[1];
rp_textBox.Text = args[2];
np_textBox.Text = args[2];
replace();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void ok_button_Click(object sender, RoutedEventArgs e)
{
replace();
}
private void replace() {
if (sf_textBox.Text == "" || rp_textBox.Text == "" || np_textBox.Text == "")
MessageBox.Show(this, "Insert all required parameters", "Parameter missing", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
using (StreamWriter w = File.CreateText("log.txt"))
{
DirectoryInfo d = new DirectoryInfo(sf_textBox.Text);
FileInfo[] Files = d.GetFiles("*.*");
string filename, log;
foreach (FileInfo file in Files)
{
filename = file.Name;
int Place = filename.IndexOf(rp_textBox.Text);
if (Place == 0)
{
log = "file " + filename + " renamed ";
filename = filename.Remove(Place, rp_textBox.Text.Length).Insert(Place, np_textBox.Text);
file.MoveTo(file.Directory.FullName + "\\" + filename);
Log(log + filename, w);
}
}
w.Close();
}
Environment.Exit(0);
}
}
public static void Log(string logMessage, TextWriter w)
{
w.Write(DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
w.Write(" --> ");
w.WriteLine(logMessage);
}
}
}
Calling method in the other program:
public void LaunchCommandLineApp()
{
using (StreamWriter wl = File.CreateText(job.name + "_log.txt"))
{
lock (wl) wl.WriteLine("\\n" + DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " launched");
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = job.process;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
var count = job.args.Count(c => c == ';');
startInfo.Arguments = "";// "-f ";
while (count > 1)
{
startInfo.Arguments += job.args.Substring(0, job.args.IndexOf(';', 0));
int i = job.args.IndexOf(';', 0) + 1, k = job.args.Length - 1;
job.args = job.args.Substring((job.args.IndexOf(';', 0) + 1), (job.args.Length - 1) - (job.args.IndexOf(';', 0)));
count--;
}
if (count == 1) {
int i = job.args.IndexOf(';', 0);
startInfo.Arguments += job.args.Substring(0, job.args.IndexOf(';', 0));
}
if (job.recurrency) job.updateRecurrency();
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
string tl;
try
{
tl = Path.GetDirectoryName(job.process);
}
catch (ArgumentException e) {
tl = null;
}
if (tl != null)
{
try
{
tl = tl + "\\log.txt";
StreamReader input = new StreamReader(tl);
tl = input.ReadLine();
while (tl != null)
{
wl.WriteLine(tl);
tl = input.ReadLine();
}
input.Close();
}
catch (Exception e) { }
}
lock (wl) wl.WriteLine(DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " successfully ended.");
}
}
catch (Exception e)
{
//add local process log
lock (wl) wl.WriteLine(DateTime.Now.ToString() + " --> " + job.name + ": " + job.process + " failed to ended. " + e.Message.ToString());
}
wl.Close();
InvokeExecutionFinished(new EventArgs());
}
//Send log via email
//sendNotification();
}
Crash report

AT commands Send/receive SMS

I am new to AT commands. I am using Nokia E71 to send and receive SMS. I am designing an application for sending SMS, but my code is not working.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace AT_commands
{
public partial class Form1 : Form
{
SerialPort serialPort;
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
this.serialPort = new SerialPort();
this.serialPort.PortName = "COM23";
this.serialPort.BaudRate = 9600;
this.serialPort.Parity = Parity.None;
this.serialPort.DataBits = 8;
this.serialPort.StopBits = StopBits.One;
this.serialPort.Handshake = Handshake.RequestToSend;
this.serialPort.DtrEnable = true;
this.serialPort.RtsEnable = true;
this.serialPort.NewLine = System.Environment.NewLine;
send_sms();
}
public bool send_sms()
{
label1.Text = "Loaded Successfuly";
String SMSMessage = "Message to send";
String CellNumber = "+923333333333";
String messageToSend = null;
if (SMSMessage.Length <= 160)
{
messageToSend = SMSMessage;
}
else
{
messageToSend = SMSMessage.Substring(0, 160);
}
if (this.IsOpen == true)
{
this.serialPort.WriteLine(#"AT" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine(#"AT+CMGS=""" + CellNumber + #"""" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine(SMSMessage + (char)(26));
return true;
}
return false;
}
public void Open()
{
if (this.IsOpen == false)
{
this.serialPort.Open();
}
}
public void Close()
{
if (this.IsOpen == true)
{
this.serialPort.Close();
}
}
public bool IsOpen
{
get
{
return this.serialPort.IsOpen;
}
}
public void Dispose()
{
if (this.IsOpen)
this.Close();
}
}
}
Please help me with this code!
Here's my code
using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
namespace CSharp_SMS
{
public partial class Form_SMS_Sender : Form
{
private SerialPort _serialPort;
public Form_SMS_Sender()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
string number = textBoxNumber.Text;
string message = textBoxMessage.Text;
//Replace "COM7"withcorresponding port name
_serialPort = new SerialPort("COM7", 115200);
Thread.Sleep(1000);
_serialPort.Open();
Thread.Sleep(1000);
_serialPort.Write("AT+CMGF=1\r");
Thread.Sleep(1000);
_serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");
Thread.Sleep(1000);
_serialPort.Write(message + "\x1A");
Thread.Sleep(1000);
labelStatus.Text = "Status: Message sent";
_serialPort.Close();
}
}
}
Here's a link http://circuitfreak.blogspot.com/2013/03/c-programming-sending-sms-using-at.html
Why dont you open the port connection in the form_load() itself, later you can close it at the end as you did.
And do these too in Form_Load():
string cmd = "AT";
port.WriteLine(cmd + "\r");
port.Write(cmd + "\r");
port.WriteLine("AT+CMGF=1");
And simplifying the sms sending code:
port.WriteLine("AT+CMGS=\"" + PhNumber + "\"");
port.Write(Message + char.ConvertFromUtf32(26));
Try it this way.. You are not opening the connection to the serial port. I have tried it and it is working fine for me.
private void button1_Click(object sender, EventArgs e)
{
this.serialPort = new SerialPort();
this.serialPort.PortName = "COM5";
this.serialPort.BaudRate = 9600;
this.serialPort.Parity = Parity.None;
this.serialPort.DataBits = 8;
this.serialPort.StopBits = StopBits.One;
this.serialPort.Handshake = Handshake.RequestToSend;
this.serialPort.DtrEnable = true;
this.serialPort.RtsEnable = true;
this.serialPort.NewLine = System.Environment.NewLine;
serialPort.Open();
send_sms();
}
public bool send_sms()
{
String SMSMessage = "gsm MESSAGE FROM .NET C#";
String CellNumber = "+9233333333333";
String messageToSend = null;
if (SMSMessage.Length <= 160)
{
messageToSend = SMSMessage;
}
else
{
messageToSend = SMSMessage.Substring(0, 160);
}
if (serialPort.IsOpen)
{
this.serialPort.WriteLine(#"AT" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine(#"AT+CMGS=""" + CellNumber + #"""" + (char)(13));
Thread.Sleep(200);
this.serialPort.WriteLine(SMSMessage + (char)(26));
return true;
}
return false;
}

Categories