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
Read from Serial
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
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 workingAFAIK 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
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
-
BackupUser
- PureBasic Guru

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

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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
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
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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm