News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

port i/o under winxp

Started by winter, January 15, 2005, 01:11:31 AM

Previous topic - Next topic

Opcode

Take a look in the nohaven reply to this thread:
http://board.win32asmcommunity.net/viewtopic.php?p=153900#153900
Maybe the function NtSetInformationProcess is the answer to your question
to access I/O ports without a driver.

Regards,
Opcode

Howard

Hi
I played around with comms a lot in XP last year, and have pretty good success at 19.2K, using standard Windows drivers. Originally got the IO permissions map scheme to work OK, but then MS issued a security fix midway thru year, which wiped it. So had to do it the proper way. If you still want I can give details. There is support for watching the handshake lines etc.

Howard

sbrown

Quote from: Howard on January 26, 2005, 12:26:52 AM
Hi
I played around with comms a lot in XP last year, and have pretty good success at 19.2K, using standard Windows drivers. Originally got the IO permissions map scheme to work OK, but then MS issued a security fix midway thru year, which wiped it. So had to do it the proper way. If you still want I can give details. There is support for watching the handshake lines etc.

Howard

Yes! Can you post it?


Scott

Howard

Hi Scott
As farrier has earlier mentioned, this method is based on Createfile,ReadFile & Writefile.

Refer MSDN - Excellent, though a little confusing, Technical Article entitled "Serial Communications in Win32"
and documentaion on CreateFile, SetUpComm, SetCommState, SetCommTimeouts, WriteFile & ReadFile.

I found the Timeouts (for retries) to be the critical area. It seems that because Windows is Pre-emptive Multi, it can
occasionally miss a received chtr or 2 in a good sized block (often up to 7 or 8 in a 6000h length block), and then will
of course just sit there waiting for the missed chtrs to turn up (since a fixed length RX). My selection works well for
my app. under Win98, XP Home, & XP Prof on a range of different speed machines.



;This code was built using Ramon's EasyCode

.Data?
EBuff DB 128 Dup(?)
.Data
ComPort DB 16 Dup(0)
hPS DD 0
cto COMMTIMEOUTS <>

sDCB DCB <>
;brBits BITRECORD <1, 0, 0, 1, DTR_CONTROL_ENABLE, 0, 1, 0, 0, 0, 0, \
; RTS_CONTROL_ENABLE, 0, 0>
brBits DD 1099H ;since structure above doesn't work

.Code

;I use a dialog box to select the comm port

dlgConfigureProcedure Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
.If uMsg == WM_CREATE
Invoke GetWindowItem, hWnd, IDC_DLGCONFIGURE_PORTSELECT
Mov hPS, Eax
fn SendMessage, hPS, CB_ADDSTRING, 0, "COM1"
fn SendMessage, hPS, CB_ADDSTRING, 0, "COM2"
fn SendMessage, hPS, CB_ADDSTRING, 0, "COM3"
fn SendMessage, hPS, CB_ADDSTRING, 0, "COM4"



.ElseIf uMsg == WM_CLOSE
Invoke SendMessage, hPS, WM_GETTEXT, 16, Addr ComPort
.If Eax > 0
Invoke CreateFile, Addr ComPort, GENERIC_READ Or GENERIC_WRITE, \
0, NULL, OPEN_EXISTING, 0, NULL
.If Eax == INVALID_HANDLE_VALUE
Jmp OpenError
.EndIf
Mov hCom, Eax

Invoke SetupComm, hCom, 6010H, 6010H ;Setup Comms buffers - I TX/RX chunks 6000h long in my app.

Mov sDCB.DCBlength, SizeOf sDCB
Mov sDCB.BaudRate, CBR_19200
Mov Eax, brBits
Mov sDCB.fbits, Eax
Mov sDCB.ByteSize, 8
Mov sDCB.Parity, NOPARITY
Mov sDCB.StopBits, ONESTOPBIT

Invoke SetCommState, hCom, Addr sDCB
.If !Eax
Jmp DCBError
.EndIf

Mov cto.ReadIntervalTimeout, 10000
Mov cto.ReadTotalTimeoutMultiplier, 1
Mov cto.ReadTotalTimeoutConstant, 2000
Mov cto.WriteTotalTimeoutMultiplier, 10
Mov cto.WriteTotalTimeoutConstant, 10000
Invoke SetCommTimeouts, hCom, Addr cto
.If !Eax
Jmp DCBError
.EndIf

Jmp CloseOut
.Else
Jmp CloseOut
.EndIf
OpenError:
fn MessageBox, NULL, "Comms Port Not Available", "Open Comms Port",
MB_OK Or MB_ICONWARNING ;Normally means another app. has grabbed it.
Jmp CloseOut
DCBError:
        Invoke GetLastError
        mov     ecx,eax
        invoke  FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,NULL,ecx,0,ADDR EBuff,128,NULL
        fn MessageBox, NULL, Addr EBuff, "Open Comms Port", MB_OK Or MB_ICONWARNING


CloseOut:
Invoke IsModal, hWnd
.If Eax
Invoke EndModal, hWnd, IDCANCEL
Return TRUE
.EndIf
.Endif
Return FALSE
dlgConfigureProcedure EndP

;***********************
;SENDING MODULE snippets

;***********************
.Const
ACK Equ 06
NAK Equ 15H

.Data?

.Data
hCom DD 0 ;Port handle
HBuff DB 8 Dup(0)
BCount DD 0
Buffer DB 260 Dup(0)
hCount DD 0 ;Static Count Box in Fetch or Send
Buff5 DB 12 Dup(0)
FCommand DB 0F5H, 0FBH, 0F0H

.Code

SndCommand Proc lpCommand:DWord

;BOOL WriteFile(
;  HANDLE hFile,                    // handle to file
;  LPCVOID lpBuffer,                // data buffer
;  DWORD nNumberOfBytesToWrite,     // number of bytes to write
;  LPDWORD lpNumberOfBytesWritten,  // number of bytes written
;  LPOVERLAPPED lpOverlapped        // overlapped buffer (- I haven't tried Overlapped)

Invoke WriteFile, hCom, lpCommand, 3, Addr BCount, NULL
.If !Eax
Call Err1
Return FALSE
.ElseIf BCount != 3
Call Err2
Return FALSE
.EndIf

;My app. expects an immediate reflection of the command
Invoke ReadFile, hCom, Addr Buffer, 3, Addr BCount, NULL
.If !Eax
Call Err1
Return FALSE
.ElseIf BCount != 3
Call Err4
Return FALSE
.EndIf

Lea Esi, Buffer
Mov Edi, lpCommand
Mov Ecx, 3
Repe Cmpsb ;Ensure reflection correct
Jz ReflOK
Call SndNAK
Jmp SndCommand
ReflOK:
Call SndACK
Return Eax
SndCommand EndP



SndNAK:
Mov Al, NAK
Jmp SndSingle
SndACK:
Mov Al, ACK
SndSingle:
Mov [HBuff], Al
Invoke WriteFile, hCom, Addr HBuff, 1, Addr BCount, NULL ;snd C/S
.If !Eax
Jmp Err1
.EndIf
.If !BCount
Jmp Err2
.EndIf
Return TRUE


Err1:
    Invoke GetLastError
    Mov Ecx, Eax
    Invoke  FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM, NULL, Ecx, 0, Addr Buffer, 128, NULL
Return FALSE

Err2:
fn MessageBox, NULL, "Send Timeout", "Comms Error", MB_OK Or MB_ICONWARNING
Return FALSE

Err4:
fn MessageBox, NULL, "Receive Timeout:", "Comms Error", MB_OK Or MB_ICONWARNING
Return FALSE



Howard

sbrown

Howard,

Thanks! I'll look through it later on. :U


Scott