Reading from a serial port

Blitz3D Forums/Blitz3D Programming/Reading from a serial port

Anyone know a nice stable way of reading and writing to a serial port?

I seem to remember someone doing a dll. Can't remember who it was though... might have been IPete2?

I am wonderink if anyone could wrap this:

http://www.zealsoftstudio.com/ntport/

VERY useful in VB :D

I hope this will work out for you..I use it once and its working..you can open,read, write, close port..

http://www.uploadraid.com/uploads/55a357e1ed.zip

http://www.uploadraid.com/uploads/5663ac281b.zip

D,

Cygnus was half right - I have done quite a lot of work in this area for the SSGB project (but in truth the code I used came from Nigel Brown - who is a bit of a whizzzzard when it comes to all this stuff).

There is a dll somewhere... but contrary to opinion and belief, you do not actually need a dll! Nigel gave me some code which worked just dandy for what I wanted.

Let me look up the code and I'll email it to you.

IPete2.

Thqnks ill take a looky.

I am doing a project for someone where i have to control a moving 'ride console' Ive managed to get around reading the controls in (can be done by simulating scancodes from another app) but i need to tell the mechanics when to start, what to do and when to stop.

Should be an interesting one.

http://www.nigelibrown.pwp.blueyonder.co.uk/blitz/userlibs/index.htm

Joe,

Thanks for providing the link

IPete2.

Yeah, nigels serialio.bb code looks exactly like the code I successfully used:
Const	NOPARITY			= 0
Const	ODDPARITY			= 1
Const	EVENPARITY			= 2
Const	MARKPARITY			= 3
Const	SPACEPARITY			= 4

Const	ONESTOPBIT			= 0
Const	ONE5STOPBITS		= 1
Const	TWOSTOPBITS			= 2

Const	DTR_CONTROL			= 1
Const	RTS_CONTROL			= 2
Const	NO_CONTROL			= 3

Const	parityEnable		= False 			; flag if parity is enabled
Const	protocol			= NO_CONTROL		; stores protocol type
Const	dataBits			= 8     			; stores number if data bits
Const	parityBit			= NOPARITY			; stores type of parity bit
Const	stopBits			= ONESTOPBIT     	; stores number of stop bits
Global	timeouts			= CreateBank(20)

Const INVALID_HANDLE_VALUE = -1

Const  GENERIC_READ        = $80000000
Const  GENERIC_WRITE       = $40000000
Const  OPEN_EXISTING       = 3

; Baud rates at which the communication device operates

Const  CBR_110             = 110
Const  CBR_300             = 300
Const  CBR_600             = 600
Const  CBR_1200            = 1200
Const  CBR_2400            = 2400
Const  CBR_4800            = 4800
Const  CBR_9600            = 9600
Const  CBR_14400           = 14400
Const  CBR_19200           = 19200
Const  CBR_38400           = 38400
Const  CBR_56000           = 56000
Const  CBR_57600           = 57600
Const  CBR_115200          = 115200


Const  EV_TXEMPTY          = 4

Const  SETRTS              = 3
Const  CLRRTS              = 4

Function openComport(comport, baudRate)

	Local dcbBaudRate, driverHandle, dcb
   
	If comport > 255 Or comport < 0 Then
		Return INVALID_HANDLE_VALUE
	EndIf
   
	Select baudRate
		Case 110
			dcbBaudRate = CBR_110
		Case 300
			dcbBaudRate = CBR_300
		Case 600
			dcbBaudRate = CBR_600
		Case 1200
			dcbBaudRate = CBR_1200
		Case 2400
			dcbBaudRate = CBR_2400
		Case 4800
			dcbBaudRate = CBR_4800
		Case 9600
			dcbBaudRate = CBR_9600
		Case 14400
			dcbBaudRate = CBR_14400
		Case 19200
			dcbBaudRate = CBR_19200
		Case 38400
			dcbBaudRate = CBR_38400
		Case 56000
			dcbBaudRate = CBR_56000
		Case 57600
			dcbBaudRate = CBR_57600
		Case 115200
			dcbBaudRate = CBR_115200
		Default
			Return INVALID_HANDLE_VALUE
	End Select
	
	driverHandle = apiCreateFile("COM"+comport, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
	If driverHandle = INVALID_HANDLE_VALUE Then
		Return INVALID_HANDLE_VALUE
	Else
		apiSetupComm(driverHandle, 1024, 1024)

		dcb = CreateBank(28)
		
		PokeInt dcb, 00, 28                         ; sizeof(DCB)
		PokeInt dcb, 04, dcbBaudRate                ; current baud rate

		val	= 1										; Binary Mode (skip Eof check)    
		If parityEnable	= True Then val = val Or 2	; Enable parity checking
		If protocol		= DTR_CONTROL Then val = val Or 48	; DTR Flow control on
		PokeByte dcb, 08, val
		
		val = 0
		If protocol = RTS_CONTROL Then val = val Or 48
		PokeByte dcb, 09, val

		PokeByte dcb, 18, dataBits					; number of bits/byte, 4-8
		PokeByte dcb, 19, parityBit					; 0-4=no, odd, even, mark, space
		PokeByte dcb, 20, stopBits					; 0,1,2 = 1, 1.5, 2

		apiSetCommState(driverHandle, dcb)
		apiSetCommMask(driverHandle, EV_TXEMPTY)

	EndIf
   
	FreeBank dcb
	Return driverHandle
	
End Function



Function closeComport(driverHandle)
   If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then
      Return False
   Else
      Return apiCloseHandle(driverHandle)
   EndIf
End Function



Function writeComport(driverHandle, numBytes, buffer)
   Local state, temp

   If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then
      Return False
   EndIf
   
   apiEscapeCommFunction(driverHandle, SETRTS)
   
   temp = CreateBank(4)
   state = apiWriteFile(driverHandle, buffer, numBytes, temp, 0)
  
;	evtmask = CreateBank(4)
;	apiWaitCommEvent(driverHandle, evtmask )
;	FreeBank evtmask
 
   apiEscapeCommFunction(driverHandle, CLRRTS)
   
   FreeBank temp
   Return state
End Function



Function readComport(driverHandle, bytesRead, bufferSize, buffer, tOut)
	Local comErrors, comStat
	
   If driverHandle = INVALID_HANDLE_VALUE Or driverHandle = 0 Then
      Return False
   EndIf
	
	PokeInt timeouts, 00, 0
	PokeInt timeouts, 04, 0
	PokeInt timeouts, 08, tOut
	PokeInt timeouts, 12, 0
	apiSetCommTimeouts(driverHandle, timeouts)
	
	comErrors = CreateBank(4)
	comStat   = CreateBank(10)
	apiClearCommError(driverHandle, comErrors, comStat)
	
	apiReadFile(driverHandle, buffer, bufferSize, bytesRead, 0)
	
	FreeBank comErrors
	FreeBank comStat
	Return True
	
End Function


And here is some actual code that a program I wrote used to use the serialio code:

Note that some of this code is specific to my application and wouldn't necessarily be used in yours...
Function SendProgram()
	SetStatusText window, "Stepping through comm. output.  Press spacebar or enter to step."
	If StepComm = True Then WaitForKey()

	; Open COM port with selected baudrate
	hCom = openComport(port, baudrate)
	If hCom <> 0 And hCom <> INVALID_HANDLE_VALUE
		SetStatusText window, "Com port opened."
		Delay 500
		If StepComm = True Then WaitForKey()
	Else
		If hCom = 0
			Return 2 ;tried to use invalid com port or baudrate
		Else
			Return 3 ;Error occured trying to open com port,
		EndIf
	EndIf
	
	;initiate transmission
	buffer = CreateBank(255)
	PokeByte buffer, 0, Asc(initialbyte$)
	If Not writeComPort(hCom, 1, buffer)
		closeComport(hCom)
		Return 3
	EndIf
	SetStatusText window, "Initiated communication."
	If StepComm = True Then WaitForKey()
	
	For iter = 1 To N8
		;fill send buffer with block of code
		totalbytes = 0
		For fill = 1 To Len(block$(iter))
			PokeByte buffer, fill - 1, Asc(Mid$(block$(iter), fill, 1))
			totalbytes = totalbytes + 1
		Next
		
		;send data through com port
		If Not writeComPort(hCom, totalbytes, buffer)
			closeComport(hCom)
			Return 3
		EndIf
		SetStatusText window, "Sent line " + iter +" through com port..."
		If StepComm = True Then WaitForKey()
	Next
	
	;terminate transmission
	PokeByte buffer, 0, Asc(terminatingbyte$)
	PokeByte buffer, 1, 10	;LineFeed character
	If Not writeComPort(hCom, 2, buffer)
		closeComport(hCom)
		Return 3
	EndIf
	SetStatusText window, "Terminated communication."
	If StepComm = True Then WaitForKey()

	FreeBank buffer

	; Close and end
	result = closeComport(hCom)
	If result = False
		Return 20 ;Error closing com port
	EndIf
	SetStatusText window, "Closed com port."
	If StepComm = True Then WaitForKey()
	
	SetStatusText window, "Successfully sent " + N8 + " lines of data."
	Delay 2000

	Return 1 ;everything went OK
End Function