Yes, <stdio.h> for start.
Yes, <stdio.h> for start.
hi , good news here.i opened the COM port already.
The card reader requires a request command to return the card number, i am trying to do that through writefile function.
That' sounds a quite good start, but after i unplug the card reader, the program still shows the state is connecting, what's go on?
Post your code... We're not mind readers...
hi, i found some included APIs, i tried use it ,no compile errors and run time errors, but finally the console just crashed.
Even if i compile without those API, the writfile and readfile function is still unfunctional..
Code:#include <stdio.h> #include <windows.h> void main() { DCB dcb = {0}; unsigned char str[32]={0}; DWORD wCount; BOOL bReadStat; DWORD dwNumBytesWritten; BOOL bResult; char *inbuf[10] = {"0x55","0x00","0x01","0x00","0x02","0x00","0x10","0x00","0x13","0xAA"}; HANDLE hOpenFile = (HANDLE)CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, NULL); if (hOpenFile == INVALID_HANDLE_VALUE) { printf("Can not open the port\n"); hOpenFile = NULL; } else if(hOpenFile != INVALID_HANDLE_VALUE) { printf("The port has been opened\n"); } PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT); if (!BuildCommDCB("9600,n,8,1", &dcb)) { printf("Setting error\n");} else printf("Setting ok\n"); bResult = WriteFile(hOpenFile,inbuf,10, &dwNumBytesWritten,NULL); if(!bResult || dwNumBytesWritten == 0) printf("Writing fail\n"); else printf("Writing sucessful\n"); bReadStat=ReadFile(hOpenFile,str,32,&wCount,NULL); if(!bReadStat||wCount== 0) { printf("Reading fail!"); PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT); } else { printf("Reading sucessful"); } CloseHandle(hOpenFile); }
Last edited by anthonyung; 03-08-2011 at 10:14 AM.
Where is it failing?
Is the port being opened by CreateFile?
1) There's no need to typecast the return of CreateFile. (Probably won't change anything, just unnecessary.)
2) When you get an invalid handle your program should call exit(GetLastError()) and shut down.
3) The else if condition is unnecessary... if it gets past your first error trap just assume the port is open...
Have you tried setting the Baud, parity, bits and stops in Device Manager instead of using the apis you found?
Your definition of inbuf should probably be simply char inbuf[] = instead of defining it as a char pointer. Sizing it as 10 with 10 hex bytes does not null terminate the resulting string... let the compiler handle it for you.
How can i set the Baud using DeviceIOcontrol?
which part is for the Baud parameter?Code:BOOL WINAPI DeviceIoControl( __in HANDLE hDevice, __in DWORD dwIoControlCode, __in_opt LPVOID lpInBuffer, __in DWORD nInBufferSize, __out_opt LPVOID lpOutBuffer, __in DWORD nOutBufferSize, __out_opt LPDWORD lpBytesReturned, __inout_opt LPOVERLAPPED lpOverlapped );
You might want to take a look at this link and scroll down to the section titled "Serial Settings". This section shows how to set the serial port settings using the DCB structure. You probably should also set the port for no Flow Control.
Jim