The Label on the UI keeps reading “RING RING” and then back to empty “”. It doesn’t however display the incoming number, which is what I want. I tried to add an if function checking if there is a ‘0’ in the data but that for some reason still doesn’t work.
The following is my code:
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;
namespace CallerID
{
public partial class CallerID : Form
{
public CallerID()
{
InitializeComponent();
port.Open();
WatchModem();
SetModem();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
if (lblCallerIDTitle.InvokeRequired)
{
if (ReadData.Contains('0'))
lblCallerIDTitle.Invoke(new SetCallerIdText(() => lblCallerIDTitle.Text = ReadData));
}
else
lblCallerIDTitle.Text = ReadData;
}
}
}
It’s been a while since I’ve worked with modems, but your modem has to support CallerID (I’m sure most do nowadays), you have to have the CallerID service from your telco (I’m sure you do), and finally, there will be an AT command to send to the modem during initialization to turn on CallerID reporting. Depending on the model of modem you’re using, the command may be different, but it’s typically
AT#CID=1. Your modem’s manual should have the AT code to use.Note that the incoming number is itself sent between the first and second rings.