fSuccess=BuildCommDCB("COM1: baud=2400 parity=N data=8 stop=1",&dcb);
//上面的BuildCommDCB()怎麼用呀??因為程式執行到這一行就有error!!
fSuccess=ClearCommBreak(hCom);//這行的作用是不是清除buffer呀??
if(!fSuccess)
{// handle error}
// 我需要的設定值 baud=2400, 8 data bits, no parity, 1 stop bit.
dcb.BaudRate = 2400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;//我要丟binary資料,這邊如果設false會有什麼差別??
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{ // Handle the error. }
char x;
x=1;
fSuccess = TransmitCommChar(hCom,x);
//上面的TransmitCommChar()不是可以傳送資料嗎,怎麼都沒丟出去
//fSuccess 竟然還會是true
if(!fSuccess)
{// Handle the error. }
int data[1] ;
DWORD byt[1];
byt[0] = 1;//我要丟一個位元組的資料
data[0]= 0x88;//要丟的資料
fSuccess=0;
fSuccess=WriteFile(hCom,data,byt[0],byt,0);
//結果程式就停在上面這一行,資料沒丟出去,fSuccess=false
//這程序也無法自動結束
}
所以,下面的程式:
BuildCommDCB("baud=2400 parity=N data=8 stop=1", &dcb);// "COM1:" 也可以不
寫
其實就是這個意思:
dcb.BaudRate = 2400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
在 "Win32 System Services 2nd Edition" p.686 有個簡單的範例程式,
大致的步驟如下所列:
// Open the Comm port, include COM, LPT or \\\\.\\TELNET
1> _hComX = CreateFile(...);
// Get the current settings of the Comm port
2> DCB dcb;
GetCommState(_hComX, &dcb);
// Modify the baud rate, byte size, parity, stopbit, etc.
3> dcb.BaudRate = ... ;
// Apply the new Comm port settings
4> SetCommState(_hComX, &dcb);
// Change the ReadIntervalTimeout so that ReadFile will return immediately.
5> COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = ... ;
SetCommTimeouts(_hComX, &timeouts);
// Set the Data Terminal Ready line (DTR)
6> EscapeCommFunction(_hComX, SETDTR);
// Send an "at" command to the modem.
// Be sure to use /r rather than /n
7> WriteFile(_hComX, ... );
你的程式好像忘了把 DTR 信號線給 Enable 吧? :)
TransmitCommChar 只是優先處理函式內的資料吧!
Good Luck to You
08/17/2000 13:57
---