Specialty serial program

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

Re: Specialty serial program

Post by dasyar »

I added another command, writetstr(), so I can associate another variable with a basic command. It seems I eliminated the problem with the multiple error display, now, the error response, shows up at appropriate times.

I am getting close to introducing some kind of scripting function. Not sure if I should create txt files, separately and then have the program feed it down to the robot, or be able to create script files within the program itself. It seems like it would be easier to deal with the script files as something separate, and then just have my program open the file and send the data line by line to the robot. Probably will have to add in some kind of, did you get it code, to make sure that the data got delivered.

I remember I did something like this using Python, which seemed to have the appropriate functions for accomplishing this, I hope it will be just as easy using FB.

Code: Select all

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

Dim As String inBuff,tmpbuf,cmdbuf,timbuf
Dim temp as integer
Dim Shared as String Gport,Gbaud,errbuf
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 Integer)
	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

Sub errchk
	' Check for error
	sleep 50,0
	While( Loc(1) > 0)
		errbuf = Input(Loc(1),#Gfile)
		Print errbuf
	Wend
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 "String: ",tmpbuf
		writeStr(Gfile,tmpbuf)
		errchk()
	Elseif inBuff = "writetstr" then
		input "command and time: ",cmdbuf,timbuf  ' Example 0001,3000
		writeStr(Gfile,cmdbuf)
		sleep 50,0
		writeStr(Gfile,timbuf)
		errchk()
	Elseif inBuff = "setserial" Then
		setSerial
	Elseif inBuff = "showserial" Then
		showSerial
	Elseif inBuff = "test" then
		tmpbuf = "1000"
		'temp = val(tmpbuf)
		'temp = 1000
		'print #Gfile, tmpbuf
	Else
		Print "Invalid Command"
	End If
	
Sleep 1,0
loop

Print "Program End!"
Close   ' Close all files that are open.
'''''''''''''''''''''''''
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

Since the program, so far, is sort of working as I need it, I will be changing it a little. This program is built around a connection to an XBee module that is connected to the PC, In this case a Windows 7 PC.

The XBee setup that I have is a peer to peer on the modules, that means the module that is connected to the PC broadcasts a signal to all other XBee units, which are peer to peer, also. So, when you have more than one module that is receiving, the two receiving modules get the same signal, and the same commands. What I need to do is give the receiving modules there own ID, and have the FB program be able to send the signal with the ID that you want to talk too.

At first when I thought about this it sounded very complicated, which it is, but computers are supposed to make this stuff work in an uncomplicated way. What I will be doing is, on the robot side I will be adding a read loop that looks for the ID variable that the FB program will be sending, and then have an appropriate response. On the FB program, I will have to add a request for an ID variable and then the rest of the command. The new structure of the command should be something like 'writestr(ID,command).
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

Yes, this way it should work.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

Just ran into a small snag, I hope, with updating the program. The XBee Pro 802.15.4 modules that I have, are now legacy items, so do I update to the latest modules. The bigger problem is that in order to use more than two XBee modules at the same time, their has to be some API changes to the modules, I guess give them an identity so it can be seen by more than two modules.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

An update, I just ran into another snag. Last week I got a couple of Maker Focus NodeMCU ESP8266 modules, I think I am in trouble, with this project.

After looking around the WEB, I came across zbasic, and they claim that zbasic can burn the ESP8266 module? So, if zbasic can do it, can freeBASIC do it? I get the impression that the ESP8266 chips are a hot item at the moment, I wonder if it will have the same staying power like the Raspberry Pi. I have to make a decision, XBee+freeBASIC, or ESP8266+freeBASIC?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Specialty serial program

Post by grindstone »

dasyar wrote:So, if zbasic can do it, can freeBASIC do it?
I doubt it. AFAIK there is no option to make the FB - compiler generate ESP8266 object code. But (after having a brief look at the ZBasic manual) I would say that if you're somewhat familiar with FB it wouldn't take more than a few hours to get along with ZBasic.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

I spent the last couple of months working with WiFi, now that that is out of the way I decided to go back to doing some more stuff with the XBee.

The program outline below is for an XBee that is connected to the desktop computer, and it is now set for Broadcast mode. This way I can talk to multiple XBee modules that are turned on. I used the Digi XCTU program to change the DL to Broadcast mode.

I am testing out a method of using the Function key to setup a dedicated session with a particular XBee module, of course I will have to be sending out an ID string for that particular XBee module, in order to communicate.

Does anybody see any major flaws in this concept?

Code: Select all

' xb_base.bas
'
' April 23, 2017
'
'

Dim Shared As String Key,buffer
Dim Shared 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:"}

Open Com port(10) & baud(3) & ",n,8,1,cs0,ds0,cd0,rs" As #1
If Err <> 0 Then
	Print "Error opening",port(10);
End If

''''' Procedures (subroutines) '''''
Sub rootmenu()
	Print "Menu - q, date, time, help "
End Sub

Sub f1key()
	Print "F1 - root"
	Print "q to quit F1 root session"
	Do
		'Key = Inkey$
		Input "# ", inBuff
		If inBuff = "q" Then
			Print "Ending F1 root session"
			Exit Do
		Elseif inBuff = "help" Then
			rootmenu()
		Elseif inBuff = "time" Then
			Print time
		Elseif inBuff = "date" Then
			Print date
		End If	
	Loop
End Sub

Sub f2key()
	Print "F2 - Robot"
	Print "q to quit F2 Robot session"
	Do
		Key = Inkey$
		If Key = "q" Then
			Print "Ending F2 session"
			Exit Do
		End If
		If Key <> "" Then
			If Key = Chr(13) Then  'This handles CR/LF
				Print #1, Chr(13);
				Print Chr$(10);
			Else
				Print #1,Key;        'Send the char
				'Print Key;           'This is the echo
			End If
		End If
		
		'Check for input
		While Loc(1) > 0
			buffer = Input$(Loc(1),#1) 'Grab a char
			Print buffer;              'Print the char
		Wend

	Loop
End Sub

Sub f3key()
	Print "F3 - SHT11"
	Print "q to quit F3 SHT11 session"
	Do
		Key = Inkey$
		If Key = "q" Then
			Print "Ending F3 session"
			Exit Do
		End If
		If Key <> "" Then
			If Key = Chr(13) Then  'This handles CR/LF
				Print #1, Chr(13);
				Print Chr$(10);
			Else
				Print #1,Key;        'Send the char
				'Print Key;           'This is the echo
			End If
		End If
		While Loc(1) > 0
			buffer = Input$(Loc(1),#1) 'Grab a char
			Print buffer;              'Print the char
		Wend
	Loop
End Sub

'''''''''''''''''''''''''

Print "xb_base, a simple specific use terminal program."
Print "Esc or 'q' to exit program."
Print "F1-root, F2-RB5x, F3-temp1, F10-Comms "
locate 4,0

'''''' main '''''
Do 'main
	
	Key = Inkey$

'Check the keyboard input
    If Key = Chr(27) Then      'Esc key
      Exit Do                  'End the program
    Elseif Key = "q" Then       'q = quit
	  Exit Do                  'End the program
    Elseif Key = Chr(255)+Chr(59) Then   ' F1 key
       f1key()
	Elseif Key = Chr(255)+Chr(60) Then   ' F2 key
		f2key()
	Elseif Key = Chr(255)+Chr(61) Then   ' F3 key
		f3key()
	Elseif Key = Chr(255)+Chr(68) Then   ' F10 key
		print Chr(13)
		Print "F10 - Comms"
		Print "Port - ";
		Print LTrim(port(10));
		Print "  ";
		Print baud(3);
		Print " BAUD"	
	End If
	



Sleep 1,0

Loop 'main

'Close all port(s)
Close
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Specialty serial program

Post by dasyar »

So, I have come up against a problem, with the XBee, that I cannot seem to get resolved. I need to set up my three XBee S1 modules in a network, and that is not working for me. I tried the Broadcast and Coordinator modes that just do not want to do what I need it to do. Not sure what the next step will be, do I buy the new XBee modules, I guess they are already setup to work in a networking mode, or do I go some other route, but what route?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Specialty serial program

Post by MrSwiss »

@dasyar, are you still trying to trick others, to do your "coding" for you?

Hopefully, nobody (in future) will fall into that "trap" of yours, again, ever ...
(I did, until realizing the mistake, I've made!)

THIS IS A WARNING TO ALL!
nastasa eodor
Posts: 182
Joined: Dec 18, 2018 16:37
Location: Germany, Hessdorf
Contact:

Re: Specialty serial program

Post by nastasa eodor »

grindstone wrote:
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.
use "put" not cr lf
Post Reply