flat assembler
Message board for the users of flat assembler.

Index > Windows > Reading from com port

Author
Thread Post new topic Reply to topic
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
I'm trying to read from the com port. Writing works just fine, but when trying to read, ReadFile hangs and does not care about my sent data.

Code:
        invoke  CreateFile,_com,GENERIC_READ or GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
        mov     [hcom],eax

        invoke  GetCommState,eax,dcb

        mov     [dcb.BaudRate],38400
        mov     [dcb.ByteSize],8
        mov     [dcb.Parity],0
        mov     [dcb.StopBits],0
        mov     [dcb.fBinary],1

        invoke  SetCommState,[hcom],dcb

        invoke  SetCommTimeouts,[hcom],timeout

        invoke  ReadFile,[hcom],buffert,1,tid,NULL
    


Is there some fundamental difference between writing to and reading from the serial port that I'm missing?

Edit: The sending side is not configured with any flow control, and after my program fails to read them, a terminal emulator will spit out the characters written during the stuck ReadFile

_________________
This is a block of text that can be added to posts you make.
Post 12 Mar 2010, 23:33
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
I think your code is OK, but check this one in case something was missing: http://board.flatassembler.net/topic.php?t=6459

What device do you have at the other end? Are you sure it is working in the same mode and that it is actually sending something? Maybe your timeouts are set to wait forever and that is the reason for the block?
Post 13 Mar 2010, 00:09
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Well, it was supposed to block until something got through. With a timeout it just went through with a read count of 0.

It turns out my DCB was wrong.

Code:
  DWORD fBinary  :1;
  DWORD fParity  :1;
  DWORD fOutxCtsFlow  :1;
  DWORD fOutxDsrFlow  :1;
  DWORD fDtrControl  :2;
  DWORD fDsrSensitivity  :1;
  DWORD fTXContinueOnXoff  :1;
  DWORD fOutX  :1;
  DWORD fInX  :1;
  DWORD fErrorChar  :1;
  DWORD fNull  :1;
  DWORD fRtsControl  :2;
  DWORD fAbortOnError  :1;
  DWORD fDummy2  :17;    


Apparently this is only ONE DWORD, and not 13 :S

A fixed DCB and a
Code:
mov     [dcb.flags],$80000000    

and it works.

_________________
This is a block of text that can be added to posts you make.
Post 13 Mar 2010, 16:02
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Here's the entire test program if anyone is interested. It reads a byte at a time and puts it in the window title:

Code:
struct DCB
  DCBlength           dd ?
  BaudRate            dd ?
  flags               dd ?
  wReserved           dw ?
  XonLim              dw ?
  XoffLim             dw ?
  ByteSize            db ?
  Parity              db ?
  StopBits            db ?
  Xondb               db ?
  Xoffdb              db ?
  Errordb             db ?
  Eofdb               db ?
  Evtdb               db ?
  wReserved1          dw ?
ends

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


Code:
include 'win32wxp.inc'
include 'dcb.inc'

.code
  start:
        invoke  GetModuleHandle,0
        mov     [wc.hInstance],eax

        invoke  LoadCursor,0,IDC_ARROW
        mov     [wc.hCursor],eax

        invoke  RegisterClass,wc
        invoke  CreateWindowEx,0,_class,_class,WS_VISIBLE+WS_OVERLAPPEDWINDOW+CS_HREDRAW+CS_VREDRAW,$100,$100,800,600,NULL,NULL,[wc.hInstance],NULL
        mov     [hwin],eax

        invoke  CreateFile,_com,GENERIC_READ or GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
        mov     [hcom],eax

        invoke  GetCommState,eax,dcb

        mov     [dcb.BaudRate],38400
        mov     [dcb.ByteSize],8
        mov     [dcb.Parity],0
        mov     [dcb.StopBits],0
        mov     [dcb.flags],$80000000

        invoke  SetCommState,[hcom],dcb

        mov     [timeout.ReadTotalTimeoutConstant],1000
        invoke  SetCommTimeouts,[hcom],timeout

        invoke  CreateThread,NULL,0,treceive,NULL,0,n

  .msgloop:
        invoke  GetMessage,msg,NULL,0,0
        or      eax,eax
        jz      .exit
        cmp     eax,-1
        je      .exit
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     .msgloop

  .exit:
        ret

treceive:
        invoke  ReadFile,[hcom],buffer,1,n,NULL
        cmp     [n],0
        je      @f
        invoke  SetWindowText,[hwin],buffer
  @@:
        cmp     [term],0
        je      treceive

WindowProc:
 virtual at esp+8
 .wmsg  rd 1
 end virtual

        cmp     [.wmsg],WM_DESTROY
        jne     .default
        invoke  PostQuitMessage,0
        not     [term]
  .exit:
        xor     eax,eax
        ret     $10
  .default:
        jmp    [DefWindowProc]
.end start

.data

_class  TCHAR   'classy',0
_com    TCHAR   'COM1',0

wc      WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,WHITE_PEN,NULL,_class

buffer  db 0
        db 0
        dw 0

msg     MSG
dcb     DCB
timeout COMMTIMEOUTS

n       rd 1
hcom    rd 1
hwin    rd 1
term    rd 1

    

_________________
This is a block of text that can be added to posts you make.
Post 13 Mar 2010, 21:02
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Zetus



Joined: 03 Jun 2004
Posts: 37
Zetus
same problem/tnx a lot
Post 29 Mar 2010, 11:53
View user's profile Send private message ICQ Number 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.

Powered by rwasa.