5.串口操作之API篇 SetupComm GetCommState SetCommState

it2022-05-23  71

SetupComm 设置串口的缓冲区

1 function SetupComm(hFile: THandle; dwInQueue, dwOutQueue: DWORD): BOOL; stdcall;

hFile:之前用CreateFile打开的串口句柄.dwInQueue,dwOutQueue:接收缓冲区和发送缓冲区.返回如果是0,表示操作失败.非0表示操作成功.GetCommState 读取串口设置(波特率,校验,停止位,数据位等).

1 function GetCommState(hFile: THandle; var lpDCB: TDCB): BOOL; stdcall; 2 function SetCommState(hFile: THandle; const lpDCB: TDCB): BOOL; stdcall; 3 type 4 _DCB = packed record 5 DCBlength: DWORD; 6 //波特率 7 BaudRate: DWORD; 8 Flags: Longint; 9 wReserved: Word; 10 XonLim: Word; 11 XoffLim: Word; 12 //数据位 13 ByteSize: Byte; 14 //校验 15 Parity: Byte; 16 //停止位 17 StopBits: Byte; 18 XonChar: CHAR; 19 XoffChar: CHAR; 20 ErrorChar: CHAR; 21 EofChar: CHAR; 22 EvtChar: CHAR; 23 wReserved1: Word; 24 end; 25 TDCB = _DCB; 26 DCB = _DCB; 27 PDCB = ^TDCB; 28 const 29 //校验 30 NOPARITY = 0; 31 ODDPARITY = 1; 32 EVENPARITY = 2; 33 MARKPARITY = 3; 34 SPACEPARITY = 4; 35 //停止位 36 ONESTOPBIT = 0; 37 ONE5STOPBITS = 1; 38 TWOSTOPBITS = 2; 39 //波特率 40 CBR_110 = 110; 41 CBR_300 = 300; 42 CBR_600 = 600; 43 CBR_1200 = 1200; 44 CBR_2400 = 2400; 45 CBR_4800 = 4800; 46 CBR_9600 = 9600; 47 CBR_14400 = 14400; 48 CBR_19200 = 19200; 49 CBR_38400 = 38400; 50 CBR_56000 = 56000; 51 CBR_57600 = 57600; 52 CBR_115200 = $1C200; 53 CBR_128000 = $1F400; 54 CBR_256000 = $3E800; 1 GetCommState(FCommHandle, FDCB); 2 FDCB.BaudRate := CBR_9600; 3 FDCB.Parity := EVENPARITY; 4 FDCB.Stopbits := ONE5STOPBITS; 5 FDCB.Bytesize := 8; 6 FDCB.Flags := 0; 7 SetCommState(FCommHandle, FDCB);

转载于:https://www.cnblogs.com/solokey/archive/2011/08/03/2126557.html


最新回复(0)