Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VC++<<vs>>RS232

9 views
Skip to first unread message

邱比特

unread,
Aug 11, 2000, 3:00:00 AM8/11/00
to

幫幫忙吧!!我看MSDN看得似懂非懂的
程式目的,用電腦的RS232丟出Binary 資料,
並用data scope(一種資料截取設備)來觀察結果
void CConBaDeDlg::OnOut()
{
DCB dcb;
HANDLE hCom;
DWORD dwError;
BOOL fSuccess=0;
hCom = CreateFile( "COM1",
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
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{ dwError = GetLastError(); // handle error }


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
//這程序也無法自動結束
}

TAISEL 內部新聞伺服器

unread,
Aug 17, 2000, 1:58:42 AM8/17/00
to

// from MSDN - BuildCommDCB
The BuildCommDCB function only fills in the members of the DCB structure.
To apply these settings to a serial port, use the SetCommState function.

所以,下面的程式:
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
---


0 new messages