Re: [Dev-C++] How to write a Serial Communications program in standard core C ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Paul I. <pi...@ot...> - 2004-06-15 17:43:42
|
Quoting Bruce jjs <per...@ya...>:
> I have used inportb( ) and outportb( ) write a serial communication=
'
> program.But this two functions is provided by Borland C 3.1 and they ar=
e not
> standard C functions.Does ANSI C has some functions can do the similar =
jobs
> like inportb( ) and outportb( )?In my impression,the standard C doesn't
> support functions to direct operate hardware,isn't it?
>=20
>=20
>=20
> ---------------------------------
> Do You Yahoo!?
> =CF=D3=D3=CA=CF=E4=CC=AB=D0=A1=A3=BF=D1=C5=BB=A2=B5=E7=D3=CA=D7=D4=D6=FA=
=C0=A9=C8=DD=A3=A1
If you talking about Win32, then you should look at the CreateFile,
GetCommState, SetCommState, ReadFile and WriteFile API-functions (unless=
you'd
rather use some toolkit such as wxWidgets (?)).
You may want to explore OVERLAPPED and such to get a more efficient solut=
ion
than using timers as I did (Never succeded in getting that to work... :-/=
=20
This code works for me (with Borland Builder 5). (Pardon the mess ;-):
int TForm1::COMInit(void)
{
if (hCom) {CloseHandle(hCom); hCom =3D 0;}
hCom =3D 0;
int fSuccess;
ErrorForm =3D new TErrorForm(this);
SECURITY_ATTRIBUTES sa;
sa.nLength =3D sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle =3D true;
sa.lpSecurityDescriptor =3D NULL;
hCom =3D CreateFile("COM1", // FileName
GENERIC_READ | GENERIC_WRITE, // Des.Access
0, // ShareMode
&sa, // lpSecurityAttr
OPEN_EXISTING, // dwCreationDistribution
0, //FILE_FLAG_OVERLAPPED, // dwFlagsAndAttributes
NULL ); // hTemplateFile
if (hCom =3D=3D INVALID_HANDLE_VALUE)
{
AnsiString Text =3D "Could not open the COM-port";
ErrorForm->FelBesk->Caption =3D Text;
ErrorForm->ShowModal();
if (hCom) {CloseHandle(hCom); hCom =3D 0;}
return 1;
}
DCB dcb;
fSuccess =3D GetCommState(hCom,&dcb);
if (fSuccess =3D=3D 0)
{
AnsiString Text =3D "Could not get com-state";
ErrorForm->FelBesk->Caption =3D Text;
ErrorForm->ShowModal();
if (hCom) {CloseHandle(hCom); hCom =3D 0;}
return 2;
}
dcb.BaudRate =3D 9600;
dcb.fParity =3D FALSE;
dcb.fRtsControl =3D RTS_CONTROL_HANDSHAKE;// 2; // RTS_CONTROL_ENABLE;=
2!!// ?
dcb.ByteSize =3D 8;
dcb.StopBits =3D ONESTOPBIT;
dcb.fAbortOnError =3D TRUE; //0; // 1
dcb.fOutX =3D 0;
dcb.fInX =3D 0;
dcb.fErrorChar =3D 0;
dcb.fNull =3D 0;
dcb.fAbortOnError =3D FALSE; // TRUE (kr=E4ver hantering)
dcb.fOutxCtsFlow =3D TRUE; //0; // 1
dcb.fOutxDsrFlow =3D 0;
dcb.fDtrControl =3D DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity =3D 0;
dcb.fTXContinueOnXoff =3D 1;
dcb.fOutxCtsFlow =3D 0;//1; // 1 lagsamt utan HW
dcb.fBinary =3D 1;
dcb.XonLim =3D 2048;
dcb.XoffLim =3D 512;
dcb.XonChar =3D '\x11'; // Tx and Rx XON character
dcb.XoffChar =3D '\x13'; // Tx and Rx XOFF character
dcb.ErrorChar =3D '\0'; // error replacement character
dcb.EofChar =3D'\0'; // end of input character
dcb.EvtChar =3D '\0'; // received event character
dcb.Parity =3D 0;
// dcb.wReserved =3D 0;
// dcb.fDummy2 =3D 0;
// dcb.wReserved1 =3D 0;
dcb.DCBlength; // sizeof(DCB) 28
dcb.BaudRate; // current baud rate 9600
dcb.fBinary; // binary mode, no EOF check 1
dcb.fParity; // enable parity checking 0
dcb.fOutxCtsFlow; // CTS output flow control 0
dcb.fOutxDsrFlow; // DSR output flow control 0
dcb.fDtrControl; // DTR flow control type 1
dcb.fDsrSensitivity; // DSR sensitivity 0
dcb.fTXContinueOnXoff; // XOFF continues Tx 1
dcb.fOutX; // XON/XOFF out flow control 0
dcb.fInX; // XON/XOFF in flow control 0
dcb.fErrorChar; // enable error replacement 0
dcb.fNull; // enable null stripping 0
dcb.fRtsControl; // RTS flow control 2
dcb.fAbortOnError; // abort reads/writes on error 0
dcb.fDummy2; // reserved 0
//dcb.wReserved; // not currently used 0
dcb.XonLim; // transmit XON threshold 2048
dcb.XoffLim; // transmit XOFF threshold 512
dcb.ByteSize; // number of bits/byte, 4-8 '\b'
dcb.Parity; // 0-4=3Dno,odd,even,mark,space '\x02' 0
dcb.StopBits; // 0,1,2 =3D 1, 1.5, 2 0
dcb.XonChar; // Tx and Rx XON character x11
dcb.XoffChar; // Tx and Rx XOFF character x13
dcb.ErrorChar; // error replacement character 0
dcb.EofChar; // end of input character 0
dcb.EvtChar; // received event character 0
dcb.wReserved1; // reserved; do not use 0
dcb.BaudRate =3D 1200; //CBR_57600
dcb.fParity =3D FALSE;
dcb.fRtsControl =3D RTS_CONTROL_TOGGLE; // ?
dcb.ByteSize =3D 8;
dcb.StopBits =3D TWOSTOPBITS;
dcb.fAbortOnError =3D 0;
dcb.fOutX =3D 0;
dcb.fInX =3D0;
dcb.fErrorChar =3D 0;
dcb.fNull =3D 0;
dcb.fAbortOnError =3D 0;
// dcb.fOutxCtsFlow =3D 1;
dcb.fOutxDsrFlow =3D 0;
dcb.fDtrControl =3D DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity =3D 0;
// dcb.fTXContinueOnXoff
fSuccess =3D SetCommState(hCom,&dcb);
if (fSuccess =3D=3D 0)
{
AnsiString Text =3D "SetComState misslyckades!";
ErrorForm->FelBesk->Caption =3D Text;
ErrorForm->ShowModal();
if (hCom) {CloseHandle(hCom); hCom =3D 0;};
return 3;
}
return 0;
}
Change the above to suit your particular applicationfor your then read/wr=
ite
with something like this:
fStatus =3D WriteFile( hCom, // handle to file to write to
val.c_str(), //LPCVOID lpBuffer, // pointer to data to write to file
val.Length(), // DWORD nNumberOfBytesToWrite, // number of bytes to =
write
&written, //LPDWORD lpNumberOfBytesWritten, // pointer to number of =
bytes
written
NULL // &ow //LPOVERLAPPED lpOverlapped // pointer to structure nee=
ded for
overlapped I/O
);
}
BOOL fsuccess =3D ReadFile(hCom, // handle of file to read
ReadBuffer, // address of buffer that receives data
100, // number of bytes to read
&nrBytes, // address of structure for data
NULL // OVERLAP
);
|