最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Win32串口操作的技巧

来源:懂视网 责编:小采 时间:2020-11-09 07:31:29
文档

Win32串口操作的技巧

Win32串口操作的技巧:1.开启一个 Serial Port: 利用一般开启档案的 CreatFile() 即可开启 serial port device用 CreateFile() API. HANDLECreateFile( LPCTSTRlpFileName, // pointertonameofthefile DWORDdwDesiredAccess
推荐度:
导读Win32串口操作的技巧:1.开启一个 Serial Port: 利用一般开启档案的 CreatFile() 即可开启 serial port device用 CreateFile() API. HANDLECreateFile( LPCTSTRlpFileName, // pointertonameofthefile DWORDdwDesiredAccess

1.开启一个 Serial Port: 利用一般开启档案的 CreatFile() 即可开启 serial port device用 CreateFile() API. HANDLECreateFile( LPCTSTRlpFileName, // pointertonameofthefile DWORDdwDesiredAccess, // access(read-write)mode DWORDdwShareMode,

1.开启一个 Serial Port:

  利用一般开启档案的 CreatFile() 即可开启 serial port device用 CreateFile() API.

HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);
lpFileName 为 "COM1" 或是 "COM2"
  dwDersiredAccess 一般为 GENERIC_READ|GENERIC_WRITE
  dwShareMode "必须"为 0, 即不能共享, 但同一个 process 中的不同 thread 在一开启之后就可以共享.
  lpSecurityAttributes 一般为 NULL
  dwCreateionDistributon 在这里"必须"为 OPEN_EXISTING
  dwFlagsAndAttributes 定义了开启的属性, 若是设成 FILE_FLAG_OVERLAPPED 则可使用异步的 I/O.
  hTemplateFile "必须"为 NULL
  传回档案 handle
  设定 Serial Port 传送及接收缓冲区的大小

  在开启完 serial port 之后, 可以藉由呼叫 SetupComm() 来进行配置传送时的缓冲区及接收时的缓冲区. 如果没有呼叫 SetupComm() 的话, Win95 会配置内定的缓冲区

BOOL SetupComm(
HANDLE hFile, // handle of communications device
DWORD dwInQueue, // size of input buffer
DWORD dwOutQueue // size of output buffer
);

  2.关闭 Serial Port file

  利用一般的 CloseHandle() 即可.

BOOL CloseHandle(
HANDLE hObject // handle to object to close
)  3.取得 Seial Port 的信息

  在 Win32 里头, 将一些通讯时会用到的信息用 COMMPROP 这个结构来表示. (当然不仅仅是 Serial Port) 可以用 GetCommProperties() 来取得:

BOOL GetCommProperties(
HANDLE hFile, // handle of communications device
LPCOMMPROP lpCommProp // address of communications properties structure
);
COMMPROP 长的像这个样子:
typedef struct _COMMPROP { // cmmp
WORD wPacketLength; // packet size, in bytes
WORD wPacketVersion; // packet version
DWORD dwServiceMask; // services implemented
DWORD dwReserved1; // reserved
DWORD dwMaxTxQueue; // max Tx bufsize, in bytes
DWORD dwMaxRxQueue; // max Rx bufsize, in bytes
DWORD dwMaxBaud; // max baud rate, in bps
DWORD dwProvSubType; // specific provider type
DWORD dwProvCapabilities; // capabilities supported
DWORD dwSettableParams; // changable parameters
DWORD dwSettableBaud; // allowable baud rates
WORD wSettableData; // allowable byte sizes
WORD wSettableStopParity; // stop bits/parity allowed
DWORD dwCurrentTxQueue; // Tx buffer size, in bytes
DWORD dwCurrentRxQueue; // Rx buffer size, in bytes
DWORD dwProvSpec1; // provider-specific data
DWORD dwProvSpec2; // provider-specific data
WCHAR wcProvChar[1]; // provider-specific data
} COMMPROP;  在这里, lpCommProp 需要 programmer 自行配置空间. 有趣的问题是, 系统在这个结构之后会需要额外的空间. 但是配置者也就是 programmer 却不知道系统会需要多少. 很简单的做法是配置一大块绝对会够的空间. 另一个聪明的做法是执行两次 GetCommProperties() , 第一次只配置 sizeof(COMMPROP) 这么大的空间, 因为还没有开始执行一些动作, 所以系统并不会尝试着在后面填东西, 所以不会出问题. 接着执行第一次的 GetCommProperties(), 得到结果, 取出结构中的 wPacketLength, 这个 member 代表实际上需要的大小, 然后依据这个大小重新配置一个新的. 这样的话 , 就不会有浪费任何空间的问题了.

  至于上述 COMMPROP 结构的成员所代表的意思, on-line help 中应该写的都满清楚的 .

  4.设定及取得通讯状态

  你可以利用 COMMPROP 来取得一些状态, 但是当你想改变目前的设定时你需要两个 API 来完成:

BOOL GetCommState(
HANDLE hFile, // handle of communications device
LPDCB lpDCB // address of device-control block structure
);

BOOL SetCommState(
HANDLE hFile, // handle of communications device
LPDCB lpDCB // address of device-control block structure
);

  你可以用 GetCommState() 来取得目前 Serial Port 的状态, 也可以用 SetCommState() 来设定 Serial Port 的状态.

  DCB 的结构就请自行翻阅 help 啰.

  另外, programmer 最常控制的几个设定就是 baud rate, parity method, data bits, 还有 stop bit. BuildCommDCB() 提供了对于这几个常见设定的控制.

BOOL BuildCommDCB(
LPCTSTR lpDef, // pointer to device-control string
文档

Win32串口操作的技巧

Win32串口操作的技巧:1.开启一个 Serial Port: 利用一般开启档案的 CreatFile() 即可开启 serial port device用 CreateFile() API. HANDLECreateFile( LPCTSTRlpFileName, // pointertonameofthefile DWORDdwDesiredAccess
推荐度:
标签: 一个 开启 操作
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top