Estoy haciendo una mini aplicacion en C# para hacer una consola de comandos AT para enviar a mi telefono por el puerto com, pero solo e logrado conectarme al equipo. pero este no atiene los comandos que le envio, alguien ya ah hecho esto?
public partial class Form1 : Form
{
private SerialPort port = new SerialPort("COM7", 96000, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
txtLog.AppendText("----------------\n");
txtLog.AppendText(port.ReadExisting());
}
private void btnConectar_Click(object sender, EventArgs e)
{
if (port.IsOpen == false)
{
try
{
port.Open();
txtLog.AppendText("Conectado COM7\n");
}
catch (Exception oex)
{
MessageBox.Show(oex.ToString());
}
}
}
private void btnDesconectar_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
txtLog.AppendText("Cerrada la conexion...\n");
}
}
private void btnEnviar_Click(object sender, EventArgs e)
{
try
{
port.WriteLine(txtCmd.Text);
}
catch (Exception wex)
{
MessageBox.Show(wex.ToString());
}
}
}
En esta página tienes la documentación de Pocket Bluesnarfer, una herramienta para Pocket PC q hace lo mismo que tu quieres: enviar comandos AT y capturar respuestas.
http://foro.elhacker.net/index.php/topic,109187.0.html
Puedes echarle un ojo al código eVC++, q se distribuye libremente.
¿Te conectas por cable de serie, por IrD, Bluetooth...??
¿Has probado a enviar comandos AT q no requieran respuesta, por ejemplo: ATD619123456;? A lo mejor envías bien pero no capturas la respuesta adecuadamente...
Por si acaso échale un ojo a mi código de Pocket Bluesnarfer, en especial, la parte en q se recibe la respuesta y se parsean los retornos de carro.
Te adjunto algo de código:
BOOL CIrdaPort::WaitForResponse(CString& strResponse, DWORD dwTimeout) const
{
ASSERT(IsOpen());
BOOL bData = FALSE;
BOOL bBucle = FALSE;
unsigned char cCurrentChar = '\0';
unsigned char cLastChar = '\0';
unsigned char cLastLastChar = '\0';
strResponse.Empty();
DWORD dwStartTicks = GetTickCount();
while (TRUE)
{
// Has the timeout occured?
if ((GetTickCount() - dwStartTicks) >= dwTimeout)
{
return FALSE;
}
// Receive the data from the serial port
if (Read(&cCurrentChar, sizeof(cCurrentChar)) > 0)
{
// Reset the idle timeout since data was received
dwStartTicks = GetTickCount();
if (isprint(cCurrentChar) && cLastLastChar == 0x0D && cLastChar == 0x0A && !bData)
{
bData = TRUE; // Received start sequence (<CR><LF>)
}
else if (isprint(cLastLastChar) && cLastChar == 0x0D && cCurrentChar == 0x0A && bData)
{
//Tratamiento del comando LEER SMS
if (strResponse.Find(_T("CMGR")) > -1)
{
if (cLastLastChar == 0x22) //Si el último carácter es ", entonces es la cabecera.
{
//AfxMessageBox(strResponse);
}
else //Sino, entonces es el cuerpo del sms
{
break;
}
}
else //Tratamiento del resto de comandos
{
break; // Received end sequence (<CR><LF>)
}
}
// Add the character to the string
if (bData && isprint(cCurrentChar))
{
char szANSI[2] = { cCurrentChar, '\0' };
strResponse += CString(szANSI);
}
cLastLastChar = cLastChar;
cLastChar = cCurrentChar;
}
else
{
Sleep(10);
}
} // while (TRUE)
return TRUE;
}
BOOL CIrdaPort::Send(const CString& strSend, DWORD dwTimeout /*= 5000*/) const
{
ASSERT(IsOpen());
USES_CONVERSION; // Need for T2CA() macro
VERIFY(PurgeComm(m_hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR));
CString str(strSend);
str += _T("\r"); // Add <CR> at the end of the string
return WriteWithTimeout(T2CA((LPCTSTR) str), str.GetLength(), dwTimeout);
}
Hola, estaba examinando el SRC, de bluesnarfer pero al abrir el proyecto en el vEVc++ me dice que hubo un error al cargar el workspace ya que el IrdaMobile.rc(10)
fatal error RC1015: cannot open include file 'afxres.h'
eso me impide cargar todo el proyecto y menos compilarlo, ademas que afxres.h no lo encuentro
saludos
Vaya, q extraño...
Tienes embedded Tools 3.0 y el SDK Windows Mobile 2002 instalado? Necesita esa configuración. Con EVC++ 4 no tira...
Efectivamente estoy usando SDK 2003 con eVC++ 4.0 + SP3
Gracias Gospel
si es muy sencillo
lo que pasa es q cuando ejecutas comandos AT es necesario enviarle al fin de la trama un tab y enter siempre y cuando los comandos no sean +++ (colgar la llamada) o a\ repetir comando anterior ejecutado.
Te envio un metodo que cree para este fin:
private void WritePortSerie(string strWrite, bool blnComandoAT)
{
objTxModem.ExecCommandAT = blnComandoAT;
try
{
if (objTxModem.ExecCommandAT == true && (strWrite.Trim().Length > 0 && RS232.IsOpen))
{
/*salida*/
/*************************/
RS232.DtrEnable = true; // DTR data terminal ready
RS232.RtsEnable = true; // RTS request to sent
/*************************/
//ejecuta comandos at
if (strWrite.Trim() == "+++")
RS232.Write(strWrite);
else if (strWrite.Trim() == "A/")
RS232.Write(strWrite);
else
RS232.Write(strWrite + "\r" + "\n");
//cadena enviada
txtComandoEnviado.AppendText("Cadena enviada: " + strWrite.ToString().Trim() + "\n");
}
else if ((strWrite.Trim().Length > 0 && RS232.IsOpen) && objTxModem.ExecCommandAT == false)
{
RS232.DtrEnable = true; // DTR data terminal ready
RS232.RtsEnable = true; // RTS request to sent
RS232.Write(strWrite);
}
}
catch (Exception ex)
{
txtComandoEnviado.AppendText("--- Error--- general Escribiendo en el puerto: " + ex.Message + "\n");
}
}
Cordialmente,
Omar Acosta
oeac_llll@hotmail.com
www.dyetron.com
una pregunta, esto sirve para cuando el celular se conecta a un puerto usb y es reconocido como un modem??
Hola,
Depende de como se haga el tratamiento del USB, si se virtualiza a un COM.
No obstante tienes otra solucion. Si lo haces en Bluetooth y con el Stack de Microsoft, te va a vitualizar los puertos COM en el perfil del modem y lo vas hacer como si fuera un puerto serie en USB e inalambrico. ¿¿¿???
Saludos,
Sir Graham.
Un pequeño detalle que a mi me sirvio cuanda andaba haciendo lo mismo, espera 4 segundos desde que estbleces la conexion hasta que empiezes a enviar datos :D