Read from Serial

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

AFAIK to read from a serial port you need to open it and
read like a file but this simple read i try hangs the program in the
readbyte() command i have try to replace with readdata/string
offcourse the serial port is configured at correct baudrate parity etc


OpenConsole()
a$ = "a"
tra = OpenComPort(0,"COM1:")
PurgeComm_(*tra, #PURGE_RXCLEAR)
UseFile(0)
Repeat
ReadData(@a$, 1)
PrintN(a$)
UseFile(0)
Until a$ = "12"
CloseFile(0)
End

Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
AFAIK to read from a serial port you need to open it and
read like a file but this simple read i try hangs the program in the
readbyte() command i have try to replace with readdata/string
offcourse the serial port is configured at correct baudrate parity etc
Don't you need to open the device as "\\.\COM1" ? I was reading the API docs this morning for this, so it would be nice if you got this working :wink:


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.


Don't you need to open the device as "\\.\COM1" ? I was reading the API docs this morning for this, so it would be nice if you got this working :wink:


Not AFAIK the PureFrog opens it like this but ....

Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by pusztry.

Can somebody put a quick code snippet on how to open the COM Port. I am new to this and I loofed at the Froggy example but I still can't figure it out.

Thanks

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Ryan,

Here's what I use. It's taken directly from the Froggy code, with a little bit of my own stuff.

Regards,
Eric

Code: Select all

; open COMM port
Procedure.l SetupCommPort(Port.b, BaudRate.l, Parity.b, StopBits.b)
  Structure DCB2
    DCBlength.l   ; sizeof(DCB) 
    BaudRate.l    ; current baud rate 
    Flags.l
    wReserved.w   ; not currently used 
    XonLim.w      ; transmit XON threshold 
    XoffLim.w     ; transmit XOFF threshold 
    ByteSize.b    ; number of bits/byte, 4-8 
    Parity.b      ; 0-4=no,odd,even,mark,space 
    StopBits.b    ; 0,1,2 = 1, 1.5, 2 
    XonChar.b     ; Tx and Rx XON character 
    XoffChar.b    ; Tx and Rx XOFF character 
    ErrorChar.b   ; error replacement character 
    EofChar.b     ; end of input character 
    EvtChar.b     ; received event character 
    wReserved1.w  ; reserved, do not use 
  EndStructure

  #CPFile = 999
  CPNum.l = #CPFile + Port
  
  *CPort = OpenComPort(CPNum,"COM" + Str(Port) + ":")
  If *CPort
    If GetCommState_(*CPort, @dcb.DCB2)
      ; Allocation des tampons d'E/S
      SetupComm_(*CPort, 4096, 4096)
      
      ct.COMMTIMEOUTS
      ct\ReadIntervalTimeout         = #MAXDWORD
      ct\ReadTotalTimeoutMultiplier  = 0
      ct\ReadTotalTimeoutConstant    = 0
      ct\WriteTotalTimeoutMultiplier = 0
      ct\WriteTotalTimeoutConstant   = 0
      SetCommTimeouts_(*CPort, ct)
      
      ; construction des donnees d'initialisation du port
      dcb\BaudRate  = BaudRate
      dcb\Parity    = Parity
      dcb\StopBits  = StopBits
      dcb\ByteSize  = 8    
      dcb\Flags     = 4227  ; Combined flags values got from the C.. PureBasic doesn't support flags in structures
      SetCommState_(*CPort, @dcb)

      PurgeComm_(*CPort, #PURGE_TXCLEAR | #PURGE_RXCLEAR)
    Else
      CPNum = -2
    EndIf
  Else
    CPNum = -1
  EndIf
    
  ProcedureReturn CPNum
EndProcedure

; read from COMM port until ,  or number of characters
Procedure.s ReadCommPort(CommPort.l, NumChars.l)
  UseFile(CommPort)
  InpBuffer.s = ""
  char.s = " "
  StopFlag.b = #FALSE
    
  While StopFlag = #FALSE
    If ReadData(@char, 1)
      If char = Chr(#CR) Or char = Chr(#LF)
        StopFlag = #TRUE
      Else
        InpBuffer + char
        If NumChars And Len(InpBuffer) >= NumChars
          StopFlag = #TRUE
        EndIf
      EndIf
    EndIf
  Wend
  
  ProcedureReturn InpBuffer
EndProcedure

; write string to COMM port - no newline
Procedure WriteCommPort(CommPort.l, Message.s)
  UseFile(CommPort)
  WriteString(Message)
EndProcedure

; write string to COMM port with newline
Procedure WriteCommPortN(CommPort.l, Message.s)
  UseFile(CommPort)
  WriteStringN(Message)
EndProcedure

[code]

You use it like this:

[code]
; initialize comm port: 9600,N,8,1
CommPtr = SetupCommPort(CommPort, #CBR_9600, #NOPARITY, #ONESTOPBIT) 
; abort if unable to open comm port
If CommPtr 
WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
[/quote]
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

Ebs

Your code is ok but i only get correct value the first time
after this i receive strange numbers !??!
i.e. not the ones that is send to serial
with a terminal always i got correct numbers .

Christos
Post Reply