Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to send AT commands through C

Status
Not open for further replies.

champnim

Junior Member level 2
Joined
Sep 9, 2006
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,489
i want to control a mobile phone from my pc using a program written in C. i want to know how to send the required AT commands through the C program.
 

KH

Junior Member level 1
Joined
Nov 1, 2004
Messages
16
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
103
I assume u have all the hardware interfacing ready.

1) Open the serial communication port using CreateFile
2) Set the baudrate, stopbits, etc using SetCommState
3) Read and write to the serial com using readfile and writefile
4) Assume u wish to send AT+OK, convert "AT+OK" to ascii, and send it using writefile.

Thanks.
 

Altafhal

Member level 2
Joined
Feb 13, 2007
Messages
42
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
Pakistan
Activity points
1,573
First convert each character of the AT command to hex code and then send it serially or send it without converting to hex.
 

KH

Junior Member level 1
Joined
Nov 1, 2004
Messages
16
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
103
Convert to ASCII hex first then only send it serially.

However, if the buffer use to hold the AT command is declared as char, it may not need to convert to Ascii hex 1st, but can send the command direct.
Not sure about this method, just an idea.
 

champnim

Junior Member level 2
Joined
Sep 9, 2006
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,489
I'm trying to send "AT\r" to my phone but writeFile is giving an error(no. 87)...pls tell me what is wrong

#include <iostream>
#include <windows.h>
#include <string.h>

int main()
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
OVERLAPPED o;
DWORD dwEvtMask;
char buffer[2048];
char *pcCommPort = "COM4";

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED,
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}

// Fill in the DCB: baud=9600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}

printf ("Serial port %s is successfully reconfigured.\n", pcCommPort);

//char *command="A";
unsigned long byteswritten;
if(!WriteFile(hCom,"AT\r",3,&byteswritten,NULL))
{
printf("WriteFile failed with error %d.\n", GetLastError());
return (4);
}
//printf("String %s successfully written to %s port",command,pcCommPort);
}
 

KH

Junior Member level 1
Joined
Nov 1, 2004
Messages
16
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
103
I always use this way to send data

BYTE data[5]={0};
DWORD byteswritten;
BOOL returnValue;
BYTE bytes2send;
.......
.....

data[0]=0x41; //AScii A
data[1]=0x54; //AScii T
data[2]=0x5C;
data[3]=0x72;

bytes2send = 4;

returnValue = WriteFile(hCom,&data,bytes2send,&byteswritten,NULL);

if fail again, maybe can try replace the \r with carriage return, as below :-
data[0]=0x41; //AScii A
data[1]=0x54; //AScii T
data[2]=0x0D //Ascii for CR

bytes2send = 3;

Hope it helps.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top