I'm trying to control COM-port with WinAPI functions. After WriteFile function call it return error code 5 (access denied). Need your help!
Port opened successfuly.
Then I define (with MemoryEx) and fill structures DCB and COMMTIMEOUTS.
After structures filled I call API-functions SetCommState, SetCommTimeouts. Both functions returns TRUE.
Now I want to write in port some data like this:
WriteFile returns error code 5 (access denied). Please, tell what I missed in my codes?
Thanks!
Port opened successfuly.
Code:
handle = kernel32.CreateFileA("\\\\.\\COM" .. n_, GENERIC_READ+GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
Code:
DCB.BaudRate = CBR_9600; DCB.ByteSize = 8; DCB.Parity = NOPARITY; DCB.StopBits = ONESTOPBIT; DCB.fBinary = 1; DCB.fOutxCtsFlow = 0; DCB.fOutxDsrFlow = 0; DCB.fDtrControl = DTR_CONTROL_DISABLE; DCB.fDsrSensitivity = 0; DCB.fNull = 0; DCB.fRtsControl = RTS_CONTROL_DISABLE; DCB.fAbortOnError = 0;
Code:
COMMTIMEOUTS.ReadIntervalTimeout = 10; COMMTIMEOUTS.ReadTotalTimeoutMultiplier = 1; COMMTIMEOUTS.ReadTotalTimeoutConstant = 100; COMMTIMEOUTS.WriteTotalTimeoutMultiplier = 0; COMMTIMEOUTS.WriteTotalTimeoutConstant = 0;
Now I want to write in port some data like this:
Code:
local n = Table.Count(data); local b = MemoryEx.Allocate(4); local t = MemoryEx.Allocate(n); if (b and t) then -- clearing byte-counter and buffer MemoryEx.Fill(b, 4, 0, MEMEX_BYTE); MemoryEx.Fill(t, n, 0, MEMEX_BYTE); -- fill buffer with data local c = 1; repeat MemoryEx.Byte(t + c - 1, data[c]); c = c + 1; until c > n; -- write data to port if (kernel32.WriteFile(handle, t, n, b, 0) == 0) then local e = kernel32.GetLastError(); Dialog.Message("error", e); end end
Thanks!
Comment