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.
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.
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.
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);
}
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.