|
您可以把步驟 d 放在迴圈中,直到 Non-Controller 被指定為listener為止。 ( i.e. Controller 送出一筆資料) , 接著重複步驟 e 去接受這筆資料。 您可以使用相同的方式在writes上。 但不是要等待被通知去接收訊息( listen ),而是要等待被通知要發送訊息( talk ) ( TACS 的 Status Word 為 High )
以下是用C程式語言所撰寫上面步驟的程式碼:
static int noncontroller;
static char buffer[100];
static int panelHandle;
int main ()
{
// Open a session to the GPIB board
noncontroller = ibfind ("gpib0");
// Release system control
ibrsc (noncontroller, 0);
// Change primary address from 0 to 1
ibpad (noncontroller, 1);
while (1)
{
// Update Status variable
ibwait (noncontroller, 0);
// Wait until non-controller is listener and ATN line is dropped.
if ((ibsta&LACS)&&(!(ibsta&ATN)))
{
ibrd (noncontroller, buffer, 100); // Read data bytes
buffer[ibcnt] = '\n'; // Add linefeed and 0 to string.
buffer[ibcnt + 1] = 0;
printf ("%s",buffer); print buffer
return 0;
} // first if
// If addressed to talk, send the response "I am a talker"
if ((ibsta&TACS)&&(!(ibsta&ATN)))
{
// Send data across the bus.
ibwrt (noncontroller, "I am a talker", strlen("I am a talker"));
return 0;
} // second if
} // while
} // main
註記: 在執行此程式碼之前,請先確定下列事項已完成:
- 包含
windows.h
標頭檔。
- 包含
ni488.h
標頭檔。
- 將你的應用程式和適合的語言介面檔案做連結。
- 對於 Microsoft C/C++, 連結
gpib-32.obj。
- 對於 Borland C/C++,連結
borlandc_gpib-32.obj。
建議您要允許 readdressing 以確保 Non-Controller 可以每一次都被設定為 listen 。否則您會接收到一個 ENOL error (沒有 listener 被找到的錯誤)。要允許 readressing ,可以使用 ibconfig ibcreaddr 1 指令。(對 controller 您只需進行上面這些動作) 若你想要修改上面的程式放在迴圈內執行,這個動作就是必須的。
想要更了解這些 GPIB 的功能和 Status Word ,請參考下列的 Related Links 網址連結。
Product Manuals: NI-488.2 User Manual for Windows
Product Manuals: NI-488.2 Function Reference Manual for Windows
KnowledgeBase 2I4E7MXH: Disabling a GPIB Interface as System Controller (SC)
Developer Zone Example: Serial Polling Between Two Computers (Controller and Non-controller)
|