flat assembler
Message board for the users of flat assembler.

Index > Windows > serial port

Author
Thread Post new topic Reply to topic
coconut



Joined: 02 Apr 2004
Posts: 326
Location: usa
coconut
does anyone have an example of reading data from a device connected to a serial port (com4)? only when i press a button on the device, does it send the data to the program. thanks for any suggestions
Post 29 Dec 2006, 04:05
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
I worked in a project some months ago which access some devices throught serial COM. I did this prototype first to check how to work on it and using fasm and accessing a Microsoft Mode mouse.
Code:
include 'Win32axp.inc'

SETXOFF                              equ 1 
SETXON                               equ 2 
SETRTS                               equ 3 
CLRRTS                               equ 4 
SETDTR                               equ 5 
CLRDTR                               equ 6 

PURGE_TXCLEAR                        equ 4h 
PURGE_RXCLEAR                        equ 8h 

struct DCB 
  DCBlength             dd ? 
  BaudRate              dd ? 
  fBits                 dd ? ; fBinary:1,fParity:1,fOutxCtsFlow:1,fOutxDsrFlow:1,fDtrControl:2,fDsrSensitivity:1,fTXContinueOnXoff:1,fOutX:1,fInX:1,fErrorChar:1,fNull:1,fRtsControl:2,fAbortOnError:1,fDummy2:17 
  wReserved             dw ? 
  XonLim                dw ? 
  XoffLim               dw ? 
  ByteSize              db ? 
  Parity                db ? 
  StopBits              db ? 
  XonChar               db ? 
  XoffChar              db ? 
  ErrorChar             db ? 
  EofChar               db ? 
  EvtChar               db ? 
  wReserved1            dw ? 
ends 

struct COMMTIMEOUTS 
  ReadIntervalTimeout           dd ? 
  ReadTotalTimeoutMultiplier    dd ? 
  ReadTotalTimeoutConstant      dd ? 
  WriteTotalTimeoutMultiplier   dd ? 
  WriteTotalTimeoutConstant     dd ? 
ends 

.data 
  hCOM                  dd           -1 
  timeouts              COMMTIMEOUTS 0, 1, 2000, 0, 0 
  hWindow               dd           0
  bytesRecieved         dd           0
  configCOM             DCB          sizeof.DCB 
  dummy                 dd           ? 
  buffer                db           ? 

.code 
start: 
; We Detect the mouse on COM1
  invoke  CreateFile, "COM1", GENERIC_READ or GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL 
  mov     [hCOM], eax 
  cmp     eax, -1 
  jne     @f 

  invoke  MessageBox, 0, "Can't open COM port", "Error", 0
  jmp     salir 

@@: 
  invoke  GetCommState, eax, configCOM 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't get actual configuration", "Error", 0
  jmp     salir 

@@: 
  mov     [configCOM.BaudRate], 1200 
  mov     [configCOM.ByteSize], 7 
  and     [configCOM.fBits], 0FFFF8000h 
  or      [configCOM.fBits], 00000001h 
  mov     [configCOM.StopBits], 0 
  mov     [configCOM.Parity], 0 

  invoke  SetCommState, [hCOM], configCOM 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't change actual configuration", "Error", 0
  jmp     salir 

@@: 
  invoke  SetCommTimeouts, [hCOM], timeouts 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't set timeouts", "Error", 0
  jmp     salir 

@@: 
  invoke  EscapeCommFunction, [hCOM], CLRDTR 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't clear DTR signal", "Error", 0
  jmp     salir 

@@: 
  invoke  EscapeCommFunction, [hCOM], CLRRTS 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't clear RTS signal", "Error", 0
  jmp     salir 

@@: 
  invoke  PurgeComm, [hCOM], PURGE_TXCLEAR or PURGE_RXCLEAR 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't purge port", "Error", 0
  jmp     salir 

@@: 
  invoke  Sleep, 1000 
  invoke  EscapeCommFunction, [hCOM], SETRTS 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't set RTS signal", "Error", 0
  jmp     salir 

  invoke  EscapeCommFunction, [hCOM], SETDTR 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't set DTR signal", "Error", 0
  jmp     salir 

; ### Listo, esperamos la M para ver si el mouse está 
@@: 
  invoke  ReadFile, [hCOM], buffer, 1, bytesRecieved, 0
  cmp     [bytesRecieved], 0
  jne     @f 

  invoke  MessageBox, 0, "There was no asnwer to DTR signal", "Error", 0
  jmp     salir 

@@: 
  cmp     [buffer], 'M' 
  je     @f 

  invoke  MessageBox, 0, "The device didn't send the awaited 'M' character", "Error", 0
  jmp     salir 

@@: 
;##### Ready to process events #####
  invoke  InitCommonControls 

  invoke  CreateThread, NULL, 0, ThreadProc, NULL, 0, dummy 
  test    eax, eax 
  jnz     @f 

  invoke  MessageBox, 0, "Can't create Thread", "Error", 0
  jmp     salir 

@@: 
  invoke  GetModuleHandle, 0 
  invoke  DialogBoxParam, eax, IDR_DIALOG, HWND_DESKTOP, DialogProc, 0 

salir: 
  invoke  CloseHandle, [hCOM] 
  invoke  ExitProcess, 0 

proc  ThreadProc lpParameter 

.loop: 
  invoke  ReadFile, [hCOM], buffer, 1, bytesRecieved, 0
  cmp     [bytesRecieved], 0
  je      .loop 

  mov     al, [buffer] 
  test    al, 40h 
  jz      .loop 

  mov     al, [buffer] 
  invoke  SendMessage, [hWindow], WM_USER, eax, 0
  jmp     .loop 

endp 

proc  DialogProc hwnddlg,msg,wparam,lparam 

  xor     eax,eax 
  cmp     [msg], WM_INITDIALOG 
  je      .WM_INITDIALOG 

  cmp     [msg], WM_USER 
  je      .WM_USER 

  cmp     [msg], WM_CLOSE 
  je      .WM_CLOSE 

  ret 

.WM_INITDIALOG: 
  mov     edx, [hwnddlg] 
  mov     [hWindow], edx
  inc     eax 

  ret 

.WM_USER: 
  test    [wparam], 20h 
  jnz     @f 

  invoke  CheckRadioButton,[hwnddlg], 3, 4, 4 
  jmp     .derecho 

@@: 
  invoke  CheckRadioButton,[hwnddlg], 3, 4, 3 

.derecho: 
  test    [wparam], 10h 
  jnz     @f 

  invoke  CheckRadioButton,[hwnddlg], 1, 2, 2 
  ret 

@@: 
  invoke  CheckRadioButton,[hwnddlg], 1, 2, 1 
  ret 

.WM_CLOSE: 
  invoke  EndDialog, [hwnddlg], 0 
  inc     eax 

  ret 
endp 

section '.rsrc' resource data readable 

  IDR_DIALOG = 37 

  directory RT_DIALOG,dialogs 

  resource dialogs,\ 
           IDR_DIALOG,LANG_ENGLISH+SUBLANG_DEFAULT,main 

  dialog main,'Microsoft mode mouse buttons status', 0, 0, 236, 40, DS_CENTER or DS_MODALFRAME or DS_FIXEDSYS or WS_POPUP or WS_CAPTION or WS_SYSMENU

    dialogitem 'STATIC', 'Right Button', -1, 8, 8, 62, 8, WS_VISIBLE
    dialogitem 'BUTTON', 'pressed', 1, 112, 8, 60, 10, BS_RADIOBUTTON or WS_VISIBLE or WS_GROUP
    dialogitem 'BUTTON', 'released', 2, 172, 8, 60, 10, BS_RADIOBUTTON or WS_VISIBLE

    dialogitem 'STATIC', 'Left Button', -1, 8, 24, 62, 8, WS_VISIBLE
    dialogitem 'BUTTON', 'pressed', 3, 112, 24, 60, 10, BS_RADIOBUTTON or WS_VISIBLE or WS_GROUP
    dialogitem 'BUTTON', 'released', 4, 172, 24, 60, 10, BS_RADIOBUTTON or WS_VISIBLE
  enddialog 

.end start 
    


Sorry, in spanish but I think you'll be able to understand it but ask if you can't.

I also did (for free...) the access to those devices but in Delphi, if you need it tell me (again in spanish).

Cheers

[edit] Translated to english[/edit]
Post 29 Dec 2006, 05:09
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Translated to english. If you want to test it you have to plug an old serial COM mouse just before executing the program, once the dialog appears you will see the status of the buttons and if you press the mouse buttons well, the status will be shown.

[edit] Don't forget to read documentation, maybe WaitCommEvent will be useful for you (not for me since my project was to handle poll based devices which shares the serial COM as it were a bus which doesn't reply anything if there is no device at the polling "address" and hence the read timeouts)[/edit]
Post 29 Dec 2006, 15:09
View user's profile Send private message Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: usa
coconut
thank you, ill try it out. i speak/read spanish btw, but thanks for the translation anyways hehe
Post 29 Dec 2006, 17:17
View user's profile Send private message Reply with quote
pool



Joined: 08 Jan 2007
Posts: 97
Location: Philippines
pool
..


Last edited by pool on 17 Mar 2013, 11:56; edited 1 time in total
Post 08 Jul 2008, 11:19
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:
A process uses the CreateFile function to open a handle to a communications resource. For example, specifying COM1 opens a handle to a serial port, and LPT1 opens a handle to a parallel port.


I tried hard to find a REALLY old program I made using LPT port but I have no trace of it Sad (Well, it was programed in MASM anyway)

What I can remember is that I've used TransmitCommChar and I had to connect to ground one of the LPT port pins (Strobe?) to make it work (I was doing the typical lights control through LPT port project using LEDs to test).
Post 08 Jul 2008, 15:26
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.