Serial Com readline

New to FreeBASIC? Post your questions here.
Post Reply
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Serial Com readline

Post by dasyar »

Not sure how to ask this question, but I am trying to convert a Python program to freeBasic. In the Python serial comms, it has a ser.readline() command, which reads a line of incoming data up to a CR. Now I know that freeBasic does not have anything like that, has anybody created something like this.

For my project I have a device that uses a serial connection to stream data, a line at a time that is CR(\n) terminated. I am not even sure as to how I would approach doing this.

I have tried something like this, but it is not working as expected.

Code: Select all

		While LOC(1) > 0
			
			buffer = Input(LOC(1),#1)			

			If buffer = Chr(13) Then   
				Put #2, ,buffer
			'End If					
		Wend
I am trying to capture one line of data, prefix with date and time, and then have it stored in a xxx.csv file. I tried just a regular read and store of the data, but I cannot attach a date and time to each line of data. I need some help with this one.

Thanks
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Serial Com readline

Post by grindstone »

Maybe this way?

Code: Select all

Do
	Line Input #1, buffer 
	buffer = Date + " " + Time + " " + buffer 
	Print #2, buffer
Loop
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Serial Com readline

Post by dasyar »

I gave the small code snippet a try, all that I was getting was the date and time in the xxx.csv file. I get the feeling that the 'Line input #x, xxx' command only works with an opened non serial file. But I am not sure about that.

The 'Line input' command is probably what I am looking for, in concept, but I cannot get it to work as expected, with a serial Com port.

I double checked my device output, and it has a "\n" fixed as the end of the line char. Now I am wondering if the 'Line input' command recognizes that as a CR.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Serial Com readline

Post by grindstone »

Now I am wondering if the 'Line input' command recognizes that as a CR.
Most likely not.

What about this?

Code: Select all

Do
	Do
		buffer += Input(1, #1)
	Loop Until Right(buffer, 2) = "\n"
	Print #2, Date;" ";Time;" ";Left(buffer, Len(buffer) - 2)
	buffer = ""
Loop
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Serial Com readline

Post by sancho3 »

@dasyar
With regards to your original code, remember that chr(10) is a line feed character that doesn't always pair with the carriage return char(13).
So if your original code reads past what you see as a new line, it could be that you need to check for chr(10) orelse chr(13).

Also, if your data stream doesn't pair the chr(13) with a lf chr(10), line input will mess up. In the following code line input only picks up the last part of the string "test".

Code: Select all

dim as string s = "hello help" + chr(13) + "hell" + chr(13) + "test"
open "mooose.txt" for output as #1
	put #1,,s
close #1
s =""
open "mooose.txt" for input as #1
	line input #1, s
close #1
print s

sleep
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Serial Com readline

Post by dasyar »

Also, if your data stream doesn't pair the chr(13) with a lf chr(10), line input will mess up.
Since I have already stated that my data stream line uses a "\n", I guess I am out of luck with this. I do not want to change the data stream code to use "LFCR", because it would be a PIA, and it seems most other popular languages seem to know how to handle "\n".

Thanks
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Serial Com readline

Post by dodicat »

Remember the escape character (!) when using "\n" in FreeBASIC.

Code: Select all

dim as string s=!"Hello\nWorld!"
print s
sleep
 
See !escaped string literal in the help section.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Serial Com readline

Post by grindstone »

The exclamation mark as operator only works with quoted strings, not with variables.

@dasyar:
Do you read at all something from the "#1"-input? Does this

Code: Select all

Do
  buffer += Input(1, #1)
  Print Len(buffer)
Loop
print out something else but 0 ?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Serial Com readline

Post by dodicat »

You could work around variable string/literal string by a Find and replace method if "\n" is embedded in the variable string.
Maybe:

Code: Select all

Function FindAndReplace(Text As String,Find As String,Replacement As String) As String
    Dim s As String=Text
    var position=Instr(s,Find)
    While position>0
        s=Mid(s,1,position-1) & Replacement & Mid(s,position+Len(Find))
        position=Instr(position+Len(Replacement),s,Find)
    Wend
    FindAndReplace=s
End Function


dim as string s="Hello\nWorld"

print s

'replace "\n"    with    "" + !"\n" + ""

s=FindAndReplace(s,"\n","" + !"\n" + "")



print s
sleep
 
If that is any help.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Serial Com readline

Post by dasyar »

@grindstone
Do you read at all something from the "#1"-input?
Yes, I get a number 40, I assume it means 40 chars in the buffer.

Below is a streaming data slice:

Code: Select all

S1,4.18;
B1,12.61;
C1,0;
B2,12.47;
I counted the chars in that sample and I count 31 chars, not sure where the other 9 chars are hidden. I guess you could count the four "\n", that do not appear in the sample, which leaves five chars to account for.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Serial Com readline

Post by dodicat »

"\n", if operating correctly is chr(10).
"\r\n" if operating correctly is chr(13,10)
Perhaps there is a char(13,10) at each line end.

Code: Select all



dim as  string buffer

buffer ="S1,4.18;"+chr(13,10)
buffer+="B1,12.61;" + chr(13,10)
buffer+="C1,0;" + chr(13,10)
buffer +="B2,12.47;" + chr(13,10)

print buffer
print len(buffer) 
sleep

 
Which leaves one out.

if you use line input
...
line input #1, s
you might need to add a chr(10)
i.e.
s+=chr(10)
You should add this chr(10) in normal file loading from a text file.
But I have no experience with serial ports.
Grindstone and Sancho3 will have a better knowledge than me.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Serial Com readline

Post by dasyar »

This seems to be giving what I want, but it has created a new problem that I have to work around. I added the sleep 5,1 so it would not mess up my foreground UI in my program.

Code: Select all

		do
			buffer2 += Input(1,#1)
			sleep 5,1
		loop until Right(buffer2,2) = Chr(13,10)
		Print #2,date;",";time;",";buffer2;		
		buffer2 = ""
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Serial Com readline

Post by grindstone »

dasyar wrote:This seems to be giving what I want
Otherwise I had recommended to list up the Asc values of the received data:

Code: Select all

Do
  buffer = Input(1, #1)
  Print Asc(buffer);" ";
Loop
Post Reply