End Google Ads 201810 - BS.net 01 --> hello
this is shikha, i m new in programming in Com Port, Actually my previous i did one project in which i have to receive/send SMS using COM1,
now the Device changed and it is supporting the Com3, Sending is working perfectly but unable to recive SMS using Port3.. here i m using Third party DLL. GSMCOMM DLL..just i m struck here,
one event is there "GsmComm.GsmCommunication.MessageReceivedEventArgs" is not firing, it should fired when message is recive BY the Port 3.. i m just unable to understand how to reslove the problem, i tried a lot, searched a lot....


public partial class Receive : Form
{
private DataTable dt = new DataTable();
private delegate void SetTextCallback(string text);
System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort();
public Receive()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

//string storage = GetMessageStorage();
string storage = PhoneStorageType.Sim;
try
{
// Read all SMS messages from the storage
DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, storage);
foreach (DecodedShortMessage message in messages)
{
Output(string.Format("Message status = {0}, ******** = {1}/{2}",
StatusToString(message.Status), message.Storage, message.Index));
ShowMessage123(message.Data);
Output("");
}
Output(string.Format("{0,9} messages read.", messages.Length.ToString()));
Output("");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Cursor.Current = Cursors.Default;
}
private void BindGrid(SmsPdu pdu)
{
DataRow dr = dt.NewRow();
SmsDeliverPdu data = (SmsDeliverPdu)pdu;
dr[0] = data.OriginatingAddress.ToString();
dr[1] = data.SCTimestamp.ToString();
dr[2] = data.UserDataText;
//dr[1] = data.UserDataText;
dt.Rows.Add(dr);
//Thread.Sleep(500);
dataGrid1.DataSource = dt;
}
SmsDeliverPdu data;
private void ShowMessage(SmsPdu pdu)
{
if (pdu is SmsSubmitPdu)
{
// Stored (sent/unsent) message
SmsSubmitPdu data = (SmsSubmitPdu)pdu;
Output("SENT/UNSENT MESSAGE");
Output("Recipient: " + data.DestinationAddress);
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");
return;
}
if (pdu is SmsDeliverPdu)
{
// Received message
data = (SmsDeliverPdu)pdu;
Output("RECEIVED MESSAGE");
Output("Sender: " + data.OriginatingAddress);
Output("Sent: " + data.SCTimestamp.ToString());
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");
//BindGrid(pdu);
return;
}
if (pdu is SmsStatusReportPdu)
{
// Status report
SmsStatusReportPdu data = (SmsStatusReportPdu)pdu;
Output("STATUS REPORT");
Output("Recipient: " + data.RecipientAddress);
Output("Status: " + data.Status.ToString());
Output("Timestamp: " + data.DischargeTime.ToString());
Output("Message ref: " + data.MessageReference.ToString());
Output("-------------------------------------------------------------------");
return;
}
Output("Unknown message type: " + pdu.GetType().ToString());
}
private void ShowMessage123(SmsPdu pdu)
{
if (pdu is SmsSubmitPdu)
{
// Stored (sent/unsent) message
SmsSubmitPdu data = (SmsSubmitPdu)pdu;
Output("SENT/UNSENT MESSAGE");
Output("Recipient: " + data.DestinationAddress);
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");
return;
}
if (pdu is SmsDeliverPdu)
{
// Received message
data = (SmsDeliverPdu)pdu;
Output("RECEIVED MESSAGE");
Output("Sender: " + data.OriginatingAddress);
Output("Sent: " + data.SCTimestamp.ToString());
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");
BindGrid(pdu);
return;
}
if (pdu is SmsStatusReportPdu)
{
// Status report
SmsStatusReportPdu data = (SmsStatusReportPdu)pdu;
Output("STATUS REPORT");
Output("Recipient: " + data.RecipientAddress);
Output("Status: " + data.Status.ToString());

Output("Timestamp: " + data.DischargeTime.ToString());
Output("Message ref: " + data.MessageReference.ToString());
Output("-------------------------------------------------------------------");
return;
}
Output("Unknown message type: " + pdu.GetType().ToString());
}
private string StatusToString(PhoneMessageStatus status)
{
// Map a message status to a string
string ret;
switch (status)
{
case PhoneMessageStatus.All:
ret = "All";
break;
case PhoneMessageStatus.ReceivedRead:
ret = "Read";
break;
case PhoneMessageStatus.ReceivedUnread:
ret = "Unread";
break;
case PhoneMessageStatus.StoredSent:
ret = "Sent";
break;
case PhoneMessageStatus.StoredUnsent:
ret = "Unsent";
break;
default:
ret = "Unknown (" + status.ToString() + ")";
break;
}
return ret;
}
private void Output(string text)
{
if (this.txtOutput.InvokeRequired)
{
SetTextCallback stc = new SetTextCallback(Output);
this.Invoke(stc, new object[] { text });
}
else
{
txtOutput.AppendText(text);
txtOutput.AppendText("\r\n");
}
}
private void Receive_Load(object sender, EventArgs e)
{
//Prompt user for connection settings
int port = 3 ;
int baudRate = 9600; // We Set 9600 as our Default Baud Rate
int timeout = 300;
//string Parity = "None";
//int DataBits = 8;
//int StopBits = 1;

TestConnection dlg = new TestConnection();
//frmConnection dlg = new frmConnection();
dlg.StartPosition = FormStartPosition.CenterScreen;
//dlg.SetData(port, baudRate, timeout,Parity,DataBits,StopBits);
dlg.SetData(port, baudRate, timeout);
if (dlg.ShowDialog(this) == DialogResult.OK)
{
dlg.GetData(out port, out baudRate, out timeout);
//dlg.GetData(out port, out baudRate, out timeout, out Parity, out DataBits, out StopBits);
CommSetting.Comm_Port = port;
CommSetting.Comm_BaudRate = baudRate;
CommSetting.Comm_TimeOut = timeout;
//CommSetting.Comm_Parity = Parity;
//CommSetting.Comm_DataBits = DataBits;
//CommSetting.Comm_StopBits = StopBits;

}
else
{
//Close();
return;
}
Cursor.Current = Cursors.WaitCursor;
CommSetting.comm = new GsmCommMain(port, baudRate, timeout);
Cursor.Current = Cursors.Default;
CommSetting.comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);
bool retry;
do
{
retry = false;
try
{
Cursor.Current = Cursors.WaitCursor;
CommSetting.comm.Open();
Cursor.Current = Cursors.Default;
}
catch (Exception)
{
Cursor.Current = Cursors.Default;
if (MessageBox.Show(this, "Unable to open the port.", "Error",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
retry = true;
else
{
Close();
return;
}
}
}
while (retry);
//Prompt user for connection settings

dt.Columns.Add("Number", typeof(string));
dt.Columns.Add("Time", typeof(string));
dt.Columns.Add("Message", typeof(string));

}
private void comm_MessageReceived(object sender, GsmComm.GsmCommunication.MessageReceivedEventArgs e)
{
MessageReceived();
}


bool _flagNumber;
bool _FlagName;
int i;
private void MessageReceived()
{
Cursor.Current = Cursors.WaitCursor;
string storage = PhoneStorageType.Sim;
int port = 3;
int baudRate = 9600;

int timeout = GsmCommMain.DefaultTimeout;
// string Parity = "None";
//int DataBits = 8;
//int StopBits = 1;
TestConnection dlg = new TestConnection();
//frmConnection dlg = new frmConnection();
dlg.StartPosition = FormStartPosition.CenterScreen;
dlg.SetData(port, baudRate, timeout);
dlg.GetData(out port, out baudRate, out timeout);
//dlg.SetData(port, baudRate, timeout, Parity, DataBits, StopBits);
//dlg.GetData(out port, out baudRate, out timeout, out Parity, out DataBits, out StopBits);
CommSetting.Comm_Port = port;
CommSetting.Comm_BaudRate = baudRate;
CommSetting.Comm_TimeOut = timeout;
DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, storage);
foreach (DecodedShortMessage message in messages)
{
Output(string.Format("Message status = {0}, ******** = {1}/{2}",
StatusToString(message.Status), message.Storage, message.Index));
ShowMessage(message.Data);
Output("");
SMS si = new SMS();
SmsBL SmsBal = new SmsBL();
si.OrderDateTime = data.SCTimestamp.ToDateTime().ToShortDateString();
si.OrderFrom = data.OriginatingAddress;

si.OrderMsg = data.UserDataText;
si.ReadStatus = "Read";
si.OrderStatus = "Active";
SmsBal.InsertSMSDetails(si);
SMS sms1 = new SMS();
SmsBL smsbal = new SmsBL();
//sms1.Number = data.OriginatingAddress.ToString();

//string _strMessage = smsbal.InsertNumber(si);
SmsSubmitPdu AutoSend = new SmsSubmitPdu();
try
{
DataTable dt = new DataTable();
dt = smsbal.GetNumber();
DataTable dt1 = new DataTable();
dt1 = smsbal.GetDealer();

for ( i=0; i