Specialty serial program

For issues with communication ports, protocols, etc.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Specialty serial program

Post by dasyar »

I started a new mobile robot project, and I need a specialized program to work with it. The program below is kind of an outline of what I have so far, and I will try to figure out how far I can take this.

Basically the robot has an XBee module, so a lot of communication will be through that. The robot also has an IR component, so I will be able to control it via a hand held remote control. The FB program will be able to control the robot movements with specific commands, but I am more interested in being able to create script txt files and then have the FB program feed it to the robot, and I will have a specific command that I can get diagnostics from the robot and have the FB program create and store the data in a log file on the computer that it is running on. These are some general ideas of the direction I am heading.

The task at hand, I need to come with a way to communicate with the robot using specific commands. I think I need something like writeStr() to be able to send some string data to the robot, and of course I will need something like readStr() to capture some string data that the robot sends back. But I think on the robot side I will be using commands like 0100, so I will probably need readDec(), or something along those lines.

The one thing I need to do is make the opening of a COM port an interactive event, and get away from having it hard coded like the way it is now. Back to programming...

Code: Select all

' test.bas
'
' January 9, 2017
'
' Specialty Serial Program
'''''''''''''''''''''''''

Dim As String inBuff

Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 10) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:"}
'''''''''''''''''''''''''

''''' Procedures (subroutines) '''''
''''' serial_open(COM11,9600) '''''
Sub serial_open(getport as string,getbaud as string)
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 10) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:"}

	if getport = "COM11" then
		getport = port(10)
	End if
	if getbaud = "9600" then
		getbaud = baud(3)
	end if
Open Com getport & getbaud & ",n,8,1,cs0,ds0,cd0,rs" As #1
	If Err <> 0 Then
		Print "Error opening", getport
	End If
	Print "COM11 open"
End Sub
'''''''''''''''''''''''''

''''' writeStr(#1,"String") '''''
' Output data to the COM port
Sub writeStr()

End Sub
'''''''''''''''''''''''''

''''' writeDec(#1, Dec) '''''
Sub writeDec()

End Sub
'''''''''''''''''''''''''

''''' readStr(#1, buffer) '''''
' Input data from the COM port
Sub readStr()

End Sub
'''''''''''''''''''''''''

''''' readDec(#1, buffer) '''''
Sub readDec()

End Sub
'''''''''''''''''''''''''

''''' menu '''''
Sub menu
	Print "Menu - Quit or q, help or ?, date, time, "
End Sub
'''''''''''''''''''''''''

''''' Start of main '''''
Print "boterm System Command"
Print "Type help or ? for menu"

' Open a COM port
' Hard coded, will need to make it selectable in live session.
serial_open("COM11","9600")

do
	input "> ",inBuff
	If inBuff = "Quit" or inBuff = "q" Then
		Exit Do
	Elseif inBuff = "date" Then
		print date
	Elseif inBuff = "time" Then
		print time
	Elseif inBuff = "help" or inBuff = "?" Then
		menu
	Elseif inBuff = "setserial" Then
		print "Future open COM port with BAUD rate."
	Else
		Print "Invalid Command"
	End If
	
Sleep 1,0
loop

Print "Program End!"
Close
'''''''''''''''''''''''''
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Specialty serial program

Post by BasicCoder2 »

Hi dasyar,

Looking at your past posts I see you have been interested in this area for about 8 years?

Do you intend using a Raspberry Pi as a robot brain?

On my medium sized robot I communicated commands to the robot via a wireless keyboard to an on board laptop.
.
Last edited by BasicCoder2 on Jan 15, 2017 16:42, edited 1 time in total.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

I did some initial testing using a Raspberry Pi 3, it drains the battery system way to fast, the robot would always be in the charger nest. Besides, the charging nest is for 6V @ 1.5 Amps. When the robot was in the nest, the 1.5 Amps was not enough to charge the battery system and keep the Raspberry Pi 3 alive.

I am using, at the moment, a Parallax Activity Board, it has a socket for an XBee, and it has an ADC component, which I use for checking the actual battery voltage. Those are the components that I am using at the moment. The thing that I will miss with the Raspberry Pi is the possibility of adding a camera, and having a vision component.

Once I get the FB program doing some actual communication with the robot, I may test a separate power bank to run the Raspberry Pi. Their is a power bank that puts out 5V @2.1Amps, and the power bank circuitry is designed to be able to be charged at the same time it has a device, like a Raspberry Pi, that is connected and is running. In the big picture I will still need the Activity Board, the Raspberry Pi does not do very good job with the components on my robot.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

In the code below, I have added two new commands, setSerial() and showSerial(). This now allows you, from an interactive session, to setup a serial port and then show some results.

I am still trying to figure out some code that would show the results of more than one active com port. With the setSerial() command, I can create two or more open ports, but how do I show them with the showSerial() command?

Now I have to work on the writeXXX and readXXX commands. Need to test out whether the open COM port is really functional, or just a figment of my imagination.

What I would really like to do is instead of having a file# designation, maybe something a little more descriptive, like in this case, using xbee. So, a command would look like writeStr(xbee,"Did you get this?"), instead of writeStr(#1,"Did you get this?"). The file# designations could get a little tricky if you start to have something like #1,#2,#3,#4,#5 that are open. This could be COM files and FILE files that are in use, you could lose track very easily if you are now able to create them in an interactive style. Just a few things to think about, yes I am open to suggestions.

Code: Select all

' test.bas
'
' January 9, 2017
'
' Specialty Serial Program
'''''''''''''''''''''''''

Dim As String inBuff
Dim Shared as String Gport,Gbaud
Dim Shared as Integer Gfile

Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}
'''''''''''''''''''''''''

''''' Procedures (subroutines) '''''
''''' serial_open(COM11,9600) '''''
Sub serial_open(getport as string,getbaud as string, getfile as Integer)
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}

	if getport = "COM11" then
		getport = port(10)
	End if
	if getbaud = "9600" then
		getbaud = baud(3)
	end if
Open Com getport & getbaud & ",n,8,1,cs0,ds0,cd0,rs" As #getfile  '1
	If Err <> 0 Then
		Print "Error opening", getport
	Else
		Print getport
		Print " open"
	End If
	
End Sub
'''''''''''''''''''''''''

''''' writeStr(#1,"String") '''''
' Output string to the COM port
Sub writeStr()

End Sub
'''''''''''''''''''''''''

''''' writeDec(#1, Dec) '''''
' Output number to the COM port
Sub writeDec()

End Sub
'''''''''''''''''''''''''

''''' readStr(#1, buffer) '''''
' Input string from the COM port
Sub readStr()

End Sub
'''''''''''''''''''''''''

''''' readDec(#1, buffer) '''''
' Input number from the COM port
Sub readDec()

End Sub
'''''''''''''''''''''''''

Sub setSerial()
	Dim as String newPort, newBaud
	Dim as Integer newFile
	
	input "Port:",newPort  'Example-"COM11"
	Gport = newPort
	input "Baud:",newBaud   ' Example - "9600"
	Gbaud = newBaud
	input "File #:",newFile  ' Example - 5
	Gfile = newFile
	
	serial_open(newPort,newBaud,newFile)
	
End Sub

Sub showSerial()
	Print "Open Port",Gport
	Print "BAUD",Gbaud
	print "File",Gfile
End Sub

''''' menu '''''
Sub menu
	Print "Menu - QUIT or q or quit, help or ?, date, time, "
End Sub
'''''''''''''''''''''''''

''''' Start of main '''''
Print "boterm System Command"
Print "Type help or ? for menu"

' Open a COM port
' Hard coded, will need to make it selectable in live session.
'serial_open("COM11","9600")

do
	input "> ",inBuff
	If inBuff = "QUIT" or inBuff = "q" or inBuff = "quit" Then
		Exit Do
	Elseif inBuff = "date" Then
		print date
	Elseif inBuff = "time" Then
		print time
	Elseif inBuff = "help" or inBuff = "?" Then
		menu
	Elseif inBuff = "setserial" Then
		'print "Future open COM port with BAUD rate."
		setSerial
	Elseif inBuff = "showserial" Then
		showSerial
	Else
		Print "Invalid Command"
	End If
	
Sleep 1,0
loop

Print "Program End!"
Close   ' Close all files that are open.
'''''''''''''''''''''''''
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

So, a command would look like writeStr(xbee,"Did you get this?"), instead of writeStr(#1,"Did you get this?")
Simply use variables instead of explicit numbers:

Code: Select all

Dim As Integer xbee
xbee = FreeFile
...
<open the port as #xbee>
...
writeStr(xbee,"Did you get this?")
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Specialty serial program

Post by MrSwiss »

@grindstone,

plse. use Long for a FreeFile return (to stay compatible with both Compilers, 32/64).
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

In principle you are right, but in this case it will be indifferent. In fact, it would be sufficient to dim xbee as UByte, for a file number can't be greater than 255.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

I did a couple of improvements, but since this is sort of just an outline of my program thoughts, I will not be doing to much code commenting. I think the program flow seems to be obvious, I hope.

In my input code, 'input "Port:',newPort', the expected input should be "XXXX", meaning that it is a string, in quotes. Is their a way to take in XXXX and have that converted to "XXXX", a string to be sent. There is more than one place now where I have to keep putting something in quotation marks. So, I guess, what I am doing right now is typing in "COM11", would like to just type in COM11, and have it be a string.

Code: Select all

' test.bas
'
' January 9, 2017
'
' Specialty Serial Program
'''''''''''''''''''''''''

Dim As String inBuff,tmpbuf,errbuf
Dim Shared as String Gport,Gbaud
Dim Shared as Long Gfile

Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}
'''''''''''''''''''''''''

''''' Procedures (subroutines) '''''
''''' serial_open(COM11,9600) '''''
Sub serial_open(getport as string,getbaud as string, getfile as Long)
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}

	if getport = "COM11" then
		getport = port(10)
	End if
	if getbaud = "9600" then
		getbaud = baud(3)
	end if
Open Com getport & getbaud & ",n,8,1,cs0,ds0,cd0,rs" As #getfile  '1
	If Err <> 0 Then
		Print "Error opening", getport
	Else
		Print getport
		Print " open"
	End If
	
End Sub
'''''''''''''''''''''''''

''''' writeStr(#1,"String") '''''
' Output string to the COM port
Sub writeStr(fileno as Long, strbuff as String)
	Print #fileno,strbuff
	Print #fileno,Chr(13)
End Sub
'''''''''''''''''''''''''

''''' writeDec(#1, Dec) '''''
' Output number to the COM port
Sub writeDec(fileno as Long, outdec as Long)
	Print #fileno,outdec
	'Print #fileno, Chr(13)
End Sub
'''''''''''''''''''''''''

''''' readStr(#1, buffer) '''''
' Input string from the COM port
Sub readStr()

End Sub
'''''''''''''''''''''''''

''''' readDec(#1, buffer) '''''
' Input number from the COM port
Sub readDec()

End Sub
'''''''''''''''''''''''''

Sub setSerial()
	Dim as String newPort, newBaud
	Dim as Integer newFile
	
	input "Port:",newPort  'Example-"COM11"
	Gport = newPort
	input "Baud:",newBaud   ' Example - "9600"
	Gbaud = newBaud
	input "File #:",newFile  ' Example - 5
	Gfile = newFile
	
	serial_open(newPort,newBaud,newFile)
	
End Sub

Sub showSerial()
	Print "Open Port",Gport
	Print "BAUD",Gbaud
	print "File",Gfile
End Sub

''''' menu '''''
Sub menu
	Print "Menu - QUIT or q or quit, help or ?, date, time, "
	Print "       writestr,  "
End Sub
'''''''''''''''''''''''''

''''' Start of main '''''
Print "boterm System Command"
Print "Type help or ? for menu"

' Open a COM port
' Hard coded, will need to make it selectable in live session.
'serial_open("COM11","9600")

do
	input "> ",inBuff
	If inBuff = "QUIT" or inBuff = "q" or inBuff = "quit" Then
		Exit Do
	Elseif inBuff = "date" Then
		print date
	Elseif inBuff = "time" Then
		print time
	Elseif inBuff = "help" or inBuff = "?" Then
		menu
	Elseif inBuff = "writestr" then
		input "command: ",tmpbuf  ' Example "0001"
		writeStr(Gfile,tmpbuf)
		' Check for error
		sleep 80,0
		While Loc(1) > 0
			errbuf = Input(Loc(1),#Gfile)
			Print errbuf;
		Wend
	Elseif inBuff = "setserial" Then
		setSerial
	Elseif inBuff = "showserial" Then
		showSerial
	Else
		Print "Invalid Command"
	End If
	
Sleep 1,0
loop

Print "Program End!"
Close   ' Close all files that are open.
'''''''''''''''''''''''''
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

In my input code, 'input "Port:',newPort', the expected input should be "XXXX", meaning that it is a string, in quotes. Is their a way to take in XXXX and have that converted to "XXXX", a string to be sent. There is more than one place now where I have to keep putting something in quotation marks. So, I guess, what I am doing right now is typing in "COM11", would like to just type in COM11, and have it be a string.
I don't exactly understand what's your problem. newPort is DIMmed as a string variable, so you don't have to type quotation marks to get a string (if you do it anyway they will be ignored). For conversion have a look at STR and VAL
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

Not sure I understand what is going on, with the program. Here is a sequence of commands and responses, since I do not have a cut and paste feature in my program.
> writestr
command: 0001
> writestr
command: 0002
0001

Invalid Command

Invalid Command

Invalid Command

Invalid Command
>
Every subsequent writestr command: that I type in, produces the 0001 and four Invalid Command responses. The first writestr seems to work as expected, all the subsequent commands produce the Invalid Command multiple times. It seems that on my program, the second time I invoke writestr, it seems to be sending some invalid code to trigger an Invalid Command four times. Now on the other end, for this particular test, 0001 turns on an LED, and 0002 turns off the LED. Even though I am getting an Invalid Command, the LED is turned off, so it did get a correct 0002.

At this time, I would like to, "Thank You" too everybody that has responded.

Code: Select all

' test.bas
'
' January 9, 2017
'
' Specialty Serial Program
'''''''''''''''''''''''''

Dim As String inBuff,tmpbuf,errbuf
Dim Shared as String Gport,Gbaud
Dim Shared as Long Gfile

Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}
'''''''''''''''''''''''''

''''' Procedures (subroutines) '''''
''''' serial_open(COM11,9600) '''''
Sub serial_open(getport as string,getbaud as string, getfile as Long)
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200",_
"38400","57600","115200"}
Dim As String port(0 To 12) = {"COM1:","COM2:","COM3:","COM4:","COM5:",_
"COM6:","COM7:","COM8:","COM9:","COM10:","COM11:","COM12:","COM13:"}

	if getport = "COM11" then
		getport = port(10)
	End if
	if getbaud = "9600" then
		getbaud = baud(3)
	end if
Open Com getport & getbaud & ",n,8,1,cs0,ds0,cd0,rs" As #getfile  '1
	If Err <> 0 Then
		Print "Error opening", getport
	Else
		Print getport
		Print " open"
	End If
	
End Sub
'''''''''''''''''''''''''

''''' writeStr(#1,"String") '''''
' Output string to the COM port
Sub writeStr(fileno as Long, strbuff as String)
	Print #fileno,strbuff
	Print #fileno,Chr(13)
End Sub
'''''''''''''''''''''''''

''''' writeDec(#1, Dec) '''''
' Output number to the COM port
Sub writeDec(fileno as Long, outdec as Long)
	Print #fileno,outdec
	'Print #fileno, Chr(13)
End Sub
'''''''''''''''''''''''''

''''' readStr(#1, buffer) '''''
' Input string from the COM port
Sub readStr()

End Sub
'''''''''''''''''''''''''

''''' readDec(#1, buffer) '''''
' Input number from the COM port
Sub readDec()

End Sub
'''''''''''''''''''''''''

Sub setSerial()
	Dim as String newPort, newBaud
	Dim as Integer newFile
	
	input "Port:",newPort  'Example-"COM11"
	Gport = newPort
	input "Baud:",newBaud   ' Example - "9600"
	Gbaud = newBaud
	input "File #:",newFile  ' Example - 5
	Gfile = newFile
	
	serial_open(newPort,newBaud,newFile)
	
End Sub

Sub showSerial()
	Print "Open Port",Gport
	Print "BAUD",Gbaud
	print "File",Gfile
End Sub

''''' menu '''''
Sub menu
	Print "Menu - QUIT or q or quit, help or ?, date, time, "
	Print "       writestr,  "
End Sub
'''''''''''''''''''''''''

''''' Start of main '''''
Print "boterm System Command"
Print "Type help or ? for menu"

' Open a COM port
' Hard coded, will need to make it selectable in live session.
'serial_open("COM11","9600")

do
	input "> ",inBuff
	If inBuff = "QUIT" or inBuff = "q" or inBuff = "quit" Then
		Exit Do
	Elseif inBuff = "date" Then
		print date
	Elseif inBuff = "time" Then
		print time
	Elseif inBuff = "help" or inBuff = "?" Then
		menu
	Elseif inBuff = "writestr" then
		input "command: ",tmpbuf  ' Example 0001
		writeStr(Gfile,tmpbuf)
		' Check for error
		sleep 60,0
		While( Loc(1) > 0)
			errbuf = Input(Loc(1),#Gfile)
			Print errbuf;
		Wend
	Elseif inBuff = "setserial" Then
		setSerial
	Elseif inBuff = "showserial" Then
		showSerial
	Else
		Print "Invalid Command"
	End If
	
Sleep 1,0
loop

Print "Program End!"
Close   ' Close all files that are open.
'''''''''''''''''''''''''
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

The "Invalid Command" is most likely the message sent by the device connected to your COM11 - port as response to your "0002".

On my computer, with a bluetooth adaptor connected to COM11, I can't reproduce that behaviour.

boterm System Command
Type help or ? for menu
> setserial
Port:COM11
Baud:9600
File #:5
COM11:
open
> writestr
command: 0001
> writestr
command: 0002
> _
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

Thanks grindstone for trying to reproduce some errors. I am developing the program on the robot side, the difficulty just might be on that side, but I am not sure. Serial communication between two devices is proving to be quite challenging. I have always been told that serial comms between devices was the easiest and simplest form of communication, so I can just imagine what the others forms of comms would entail.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

You can easily determine the origin of the error messages. Just comment out the marked line:

Code: Select all

...
ElseIf inBuff = "writestr" Then
		Input "command: ",tmpbuf  ' Example 0001
		writeStr(Gfile,tmpbuf)
		' Check for error
		Sleep 60,0
		While( Loc(1) > 0)
			errbuf = Input(Loc(1),#Gfile)
			'Print errbuf; '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		Wend
	ElseIf inBuff = "setserial" Then
	...
If the messages disappear, they are produced by the connected device.

I strongly suppose the problem is caused by writeStr. Try to add semicolons at the end of the Print - statements:

Code: Select all

Sub writeStr(fileno As Long, strbuff As String)
	Print #fileno,strbuff;
	Print #fileno,Chr(13);
End Sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

Yes, that is the little subtlety that I keep over looking, the semicolon, no auto CR, after the Print statement. The two lines that you point out, I have essentially at least three CR sent, which on the robot side would be interpreted as "Invalid Command". Now I am wondering if using write statement would be a better option? If I remember correctly I do not think that the write statement has that semicolon/no semicolon business associated with it. This way it would force me to have greater control over the CR that are sent/not sent. Just an idea, not sure if that would gain me anything, except maybe a bigger debugging issue.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

Now I am wondering if using write statement would be a better option?
That won't work. The WRITE- statement also adds a LF/CR that can not be suppressed by a semicolon. In addition to it the string is automatically put in quotation marks.
Post Reply