Code archives/Networking/BMax Serial COM Port - Linux

This code has been declared by its author to be Public Domain code.

Download source code

BMax Serial COM Port - Linux by Yeshu7772010
Simple way to send and receive strings without additional modules.

Does require some addition to rc.local in order to set port and temp files, but other than that it works and can be expanded upon.

First thing,

Open a terminal and add the following lines to rc.local

eg. $sudo gedit /etc/rc.local

#Set COM Port to 9600 BAUD, Null Modem config
stty -F /dev/ttyS0 9600 raw -echo

#Create a log file
cat /dev/ttyS0 > /var/tmp/ttyS0.log &

#Allow BMax permission to play with it
chmod a+rw /var/tmp/ttyS0.log


This will now automatically log everything recieved by the specified com port to a text file.

!Update! - log_file now declared TStream due to a possible memory leak..
'*************************************************************
'* Description... Simple Serial COM String Handler
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

'*************************************************************
' * Vars
'*************************************************************

Global	TXCom:TStream	  ' Transmit Stream 

Global	bytes_rcvd         ' New Bytes Recieved.
Global	prev_bytes_rcvd	  ' Previous Bytes Recieved

Global   log_file:TStream

'*************************************************************
'* Description... Init The Comms
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

Function	InitSerialComms( )

	TXCom = WriteStream("/dev/ttyS0");
	
	prev_bytes_rcvd = 0;

	log_file = OpenFile("/var/tmp/ttyS0.log")

	bytes_rcvd = StreamSize(log_file)'

	CloseStream(log_file)
	
End Function

'*************************************************************
'* Description... Close The TX Stream
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

Function	EndSerialComms()

	CloseStream(TXCom)'

End Function

'*************************************************************
'* Description... Command Handler (In Main Game Loop)
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

Function	CommandHandler()

	Local	cmd$'
	
	cmd$ = RxCommand$()'
	
	If(Len(cmd$) <> 0) Then
	
		If(Instr(cmd$, "TEST", 0) > 0) Then

                         'Do what you need here.

			DebugLog("TEST Recieved")
			SendCommand("ack")
	
		End If

	End If

End Function

'*************************************************************
'* Description... Send A String
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

Function	SendCommand( buf$ )
	
	WriteString(TXCom, buf$)'
	FlushStream(TXCom)'
	
End Function
	
'*************************************************************
'* Description... Recieve a String via the log file.
'* Date.......... 20.1.10
'* Author........ Yeshu777
'*************************************************************

Function	RxCommand$()

	Local	buf$
	
	log_file = OpenFile("/var/tmp/ttyS0.log")
		
	If(log_file) Then

	        bytes_rcvd = StreamSize(log_file)'

		If(bytes_rcvd > prev_bytes_rcvd) Then
  
                      SeekStream(log_file, prev_bytes_rcvd)

		     buf$ = ReadLine(log_file)

		     prev_bytes_rcvd = bytes_rcvd'

                     CloseStream(log_file)

		    Return(buf$)

              End If
		
              CloseStream(log_file)

	End If
							
End Function

'*************************************************************

Comments

markcw2010
Thanks for the share.


Yeshu7772010
It's not pretty and it's not nice, but works.

EDIT - Updated due to possible memory leak.


Code Archives Forum