9. X-script Command shell

9.1. VMI API

Shell commnands are written in X-script language.

../../_images/vmi_commandshell.png

Command shell VMI documentation

Documentation –> RDE documention –> Command shell –> VMI documention

In this chapter we will see 2 commands that use the BCC communication protocol of Robox, in order to communicate with the controller. The 2 commands can be found in the installation folder of RDE.

../../_images/commandshell.gif

Example of Shell commands implemented in X-script

9.2. ALS command

ALS command source code

; ===========================================================================
; ROBOX SpA
; Via Sempione 82, Castelletto Ticino, ITALY
; +390331922086
; http://www.robox.it
; ---------------------------------------------------------------------------
; Script.......: ALS
; Description..: Display alarms stack content
; ===========================================================================

code help (): bool
	print ("ALS [-E] [pos]", textBold)
	if (languageCode() == "it")
		print ("Visualizza contenuto stack allarmi.", textItalic)
		print ("Parametri:")
		print ("  -E, informazioni estese")
		print ("  pos, indice dello stack (1-N)")
	else
		print ("Display alarms stack content.", textItalic)
		print ("Parameters:")
		print ("  -E, extended information")
		print ("  pos, stack index (1-N)")
	end
	return true
end

; ---------------------------------------------------------------------------

code printStackPosition (bccmsg @asw)
	string buf	
	buf = "als("+  strFormat("%2d",asw.u32(0)) +") "
	buf = buf + " ac=" + strFormat("%-4d",asw.u16(4))
	if (asw.u16(6) != 0)
		buf = buf + " ax=" + strFormat("%-2d",asw.u16(6))
	else
		buf = buf + "      "
	end 
	buf = buf + strFormat(" '%s'", asw.str(40))
	print(buf)
end

; ---------------------------------------------------------------------------

code execute (cmdline @cl): bool
	int pos = 0
	bool extInfo = false
	string alsId
	string alsTitle
	bccmsg cmd, asw, msg
	bccmsglist msgs
	
	; Imposta task veloce
	setFastSchedule(true)
	
	; Titolo
	print (tr ("us=Alarms stack contents^it=Contenuto stack allarmi"), textBold)

	; Verifica opzioni
	while (cl.isOption ())
		if (strLower (cl.asString()) == "e")
			extInfo = true
			cl.next ()
			continue
		end
		
		printError (tr ("us=Wrong option -^it=Opzione errata -") + cl.asString ())
		return false
	end
	
	; Verifica parametro posizione (opZ)
	if (cl.isInteger ())
		pos = cl.asInt ()
		if (pos < 1)
			printError (tr("us=Invalid stack index^it=Indice dello stack non valido"))
			return false
		end
		cl.next ()
	end
	
	; Ignora parametri extra
	cl.ignoreExtra ()
	
	; Richiesta singola posizione
	if (pos)	
			; Compose and send request command
		cmd.msgcode = AS|520
		cmd.msglen = 8
		cmd.u32(0) = 0			; flags
		cmd.u32(4) = pos		; posizione
		if (not command (@cmd, @asw))
			printnack (@asw)
			return false
		end
		printStackPosition(@asw)
		return true
	end
	
	; Richiesta stack completo allarmi
	cmd.msgcode = AS|521
	cmd.msglen = 4
	cmd.u32(0) = 0			; flags
	msgs.clear ()
	if (not downloadSequence (@cmd, @asw, @msgs))
		printnack (@asw)
		return false
	end
	
	; Composizione titolo	
	alsId = strFormat ("0x%08x", asw.u32 (8))
	if (languageCode () == "it")
		alsTitle = "Visualizzazione di " + msgs.count() + " su "+ asw.u32 (4) + " allarmi"
		if (extInfo)
			alsTitle += ", con ID "+ alsId
		end
	else
		alsTitle = "Displaying " + msgs.count () + " of " + asw.u32 (4) + " alarms"
		if (extInfo)
			alsTitle += ", with ID " + alsId
		end
	end
	
	; Stampa contenuto dello stack
	if (msgs.count () > 0)
		print (alsTitle)
		if (msgs.first(@msg))
			do
				printStackPosition(@msg)
			end msgs.next (@msg)
		end
		
	; Stampa stack vuoto
	else
		if (extInfo)
			print (alsTitle)
		end
		print (tr ("us=No alarm in stack^it=Nessun allarme in stack"))
	end
			
	return true
end

9.3. DATE command

Date command cource code

; ===========================================================================
; ROBOX SpA
; Via Sempione 82, Castelletto Ticino, ITALY
; +390331922086
; http://www.robox.it
; ---------------------------------------------------------------------------
; Script.......: date
; Description..: Show (or set) date for connected device
; ===========================================================================

code help (): bool
	print ("DATE [dd mm yy]", textBold);
	print ("DATE -LSET", textBold);
	if (languageCode() == "it")
		print ("Visualizza (o imposta) la data per il dispositivo.", textItalic)
		print ("Parametri:")
		print ("  -LSET, imposta data usando la data locale")
		print ("  dd, giorno (1-31)")
		print ("  mm, mese (1-12)")
		print ("  yy, anno (2003-2100)")
	else
		print ("Display (or set) date for the device.", textItalic)
		print ("Parameters:")
		print ("  -LSET, set date using local date")
		print ("  dd, day (1-31)")
		print ("  mm, month (1-12)")
		print ("  yy, year (2003-2100)")
	end
	print ("")
	print ("DATE -L", textBold);
	if (languageCode() == "it")
		print ("Visualizza data locale.", textItalic)
	else
		print ("Display local date.", textItalic)
	end
	return true
end

; ---------------------------------------------------------------------------

code showDate (): bool
	
	; Titolo
	print (tr ("us=Current date&^it=Data corrente"), textBold)
	
	; Compose command message
	bccmsg cmd, asw
	cmd.msgcode = AS|503
	if (not command (@cmd, @asw))
		printnack (@asw)
		return false
	end
	
	; Show result
	int d, m, y
	d = asw.u8 (3)
	m = asw.u8 (4)
	y = asw.u16 (5)
	print (dayName (d, m, y) + ", " + dateToString (d, m, y))
	return true;	
end

; ---------------------------------------------------------------------------

code showLocalDate (): bool
	int d, m, y
	print (tr ("us=Current date (local)^it=Data corrente (locale)"), textBold)
;	print (dateToString (day (), month (), year ()))
	d = day ()
	m = month ()
	y = year ()
	print (dayName (d, m, y) + ", " + dateToString (d, m, y))
	return true;	
end

; ---------------------------------------------------------------------------

code setDate (cmdline @cl): bool
	int d, m, y
	bccmsg cmd, asw

	; Read and check first parameter (DAY) 
	if (cl.isInteger ())
		d = cl.asInt ()
		if (d < 1 or d > 31)
			printError (tr("us=Invalid day (1-31)^it=Giorno non valido (1-31)"))
			return false
		end
		cl.next ()
	else
		printError (tr("us=Expected a day number^it=Atteso numero giorno "));
		return false
	end

	; Read and check second parameter(MONTH) 
	if (cl.isInteger ())
		m = cl.asInt ()
		if (m < 1 or m > 12)
			printerror (tr("us=Invalid month (1-12)^it=Mese non valido (1-12)"))
			return false
		end
		cl.next ()
	else
		printerror (tr("us=Expected a month number^it=Atteso numero mese"));
		return false
	end

	; Read and check third parameter(YEAR) 
	if (cl.isInteger ())
		y = cl.asInt ()
		if (y < 2003 or y > 2100)
			printerror (tr("us=Invalid year (2003-2100)^it=Anno non valido (2003-2100)"))
			return false
		end
		cl.next ()
	else
		printerror (tr("us=Expected a year number^it=Atteso numero anno"))
		return false
	end
	cl.ignoreExtra ()
	
	; Check date validity
	if (not isDateValid (d, m, y))
		printerror (tr("us=Invalid date^it=Data non valida"))
		return false
	end

	; Prepare and send command
	cmd.msgcode = AS|504
	cmd.msglen = 8
	cmd.u8(0) = 0 
	cmd.u8(1) = 0
	cmd.u8(2) = 0
	cmd.u8(3) = byte(d)
	cmd.u8(4) = byte(m)
	cmd.u16(5)= word(y)
	cmd.u8(7) = byte(0x0038)

	; Send message
	if (not command (@cmd, @asw))
		printnack (@asw)
		return false;
	end
	return true
end

; ---------------------------------------------------------------------------

code setDateFromLocal (): bool
	int d, m, y
	bccmsg cmd, asw
	
	; Init variables
	d = day ();
	m = month ();
	y = year ();
	
	; Prepare and send command
	cmd.msgcode = AS|504
	cmd.msglen = 8
	cmd.u8(0) = 0 
	cmd.u8(1) = 0
	cmd.u8(2) = 0
	cmd.u8(3) = byte(d)
	cmd.u8(4) = byte(m)
	cmd.u16(5)= word(y)
	cmd.u8(7) = byte(0x0038)

	; Send message
	if (not command (@cmd, @asw))
		printnack (@asw)
		return false;
	end
	return true
end

; ---------------------------------------------------------------------------

code execute (cmdline @cl): bool
	bool result
	bool showLocal = false
	bool setFromLocal = false
	
	; Imposta task veloce
	setFastSchedule(true)
	
	; Check options
	while (cl.isOption ())
		if (strLower (cl.asString()) == "l")
			showLocal = true
			cl.next ()
			continue
		end
		if (strLower (cl.asString()) == "lset")
			setFromLocal = true
			cl.next ()
			continue
		end
		printError (tr("us=Wrong option -^it=Opzione errata -") + cl.asString())
		return false
	end
	
	; Check and launch operation
	if (showLocal)
		cl.ignoreExtra ()
		result = showLocalDate ()
	else 
		if (setFromLocal)
			cl.ignoreExtra ()
			result = setDateFromLocal ()
		else
			if (cl.isEol ())
				result = showDate ()
			else
				result = setDate (cl)
			end
		end
	end
	
	return result;
end

9.4. Using XForm and Qt designer

The User interface is designed in Qt designer. The extension of the file is .ui and it is an xml file.

../../_images/qtdesigner.png

Qt designer

This script use the Class XForm in order to handle the Qt user interface and send configuration parameters to a third party drive.