Win32中簡單實現串列埠通訊

2021-06-21 02:41:04 字數 1317 閱讀 9453

宣告:本文的串列埠通訊是主機向從機傳送資料。

step 1:

handle m_hcomm= createfile("com3",

//串列埠名,根據實際情況而定

generic_read | generic_write,

0, null,

open_existing,0);

if ( invalid_handle_value == m_hcomm )

{........

return ;

step 2:

dcb   m_dcb;

getcommstate(m_hcomm, &m_dcb);

m_dcb.baudrate  = cbr_115200;

m_dcb.bytesize     = 8;

m_dcb.parity   = 0;

m_dcb.stopbits  = onestopbit;

setcommstate(m_hcomm, &m_dcb);

ov_write.hevent = createevent(null, true, false, null);

ov_write.offset = 0;

ov_write.offsethigh = 0;

step 3:

commtimeouts m_commtimeouts;

uint8 utxdata255], ulen, bytesend;

purgecomm(m_hcomm, purge_rxclear | purge_txclear | purge_rxabort | purge_txabort);

m_commtimeouts.readintervaltimeout = 50;

m_commtimeouts.readtotaltimeoutmultiplier = 50;

m_commtimeouts.readtotaltimeoutconstant = 50;

m_commtimeouts.writetotaltimeoutmultiplier = 50;

m_commtimeouts.writetotaltimeoutconstant = 50;

setcommtimeouts(m_hcomm, &m_commtimeouts);

//utxdata為要傳送的資料,ulen為資料長度,bytesend為實際傳送的長度。

writefile(m_hcomm, utxdata, ulen, &bytesend, &ov_write);

// 假如   ulen == bytesend ,表示傳送成功

對串列埠通訊函式有詳細的介紹,請看這篇文章

在WIN32中的串列埠通訊(Delphi)

在win32中的串列埠通訊 delphi 由在win32作業系統中禁止應用程式象dos中那樣直接訪問計算機硬體,因此,無法象以前那樣採用中斷讀寫串列埠。但是在win32中我們可發採用兩種方法訪問串列埠 1 使用vb中的mscomm串列埠控制項 2 採用api函式,本文主要介紹採用api函式實現串列埠...

win32 串列埠 通訊 非同步 執行緒方式

該例子是 visual c 實踐與提高 串列埠通訊與工程應用篇 第2版 書上給出的部分原始碼,整理修改之後的演示 借助 tc35模組可以,正常執行 如下 seritaltk.cpp 定義控制台應用程式的入口點。include stdafx.h include handle hcom dword th...

Win32串列埠程式設計

win32串列埠程式設計 金貝貝 一 基本知識 win32下串列埠通訊與16位串列埠通訊有很大的區別。在win32下,可以使用兩種程式設計方式實現串列埠通訊,其一是呼叫的windows的api函式,其二是使用activex控制項。使用api 呼叫,可以清楚地掌握串列埠通訊的機制,熟悉各種配置和自由靈...