Qbasic reading comport reply without newline - reply

I'm working on reading device reply using QBasic. The problem is the qbasic wait for the newline or CHR$(13) before outputting the data but my device reply don't have CHR$(13) (example: "OK") so qbasic hang waiting for newline.
How can i get the reply or read comport even without newline? is this possible?
[EDIT]
CLS
OPEN "com2:9600,n,8,1,BIN,cs,ds,rs" FOR RANDOM AS #1
param$ ="Some data"
PRINT #1, param$
DO WHILE b$ <> "*CLOSE*"
INPUT #1, b$
PRINT b$
LOOP
That is my code but in that code it can't read *CLOSE* because no newline after *CLOSE*.
And another thing the device delay 5 sec before replying.

Could you give an example of your code? I suspect you are using INPUT#n , but maybe instead you should use INPUT$(x). I found an example here, see code below
a$ = ""
DO
IF LOC(1) THEN a$ = a$ + INPUT$(1, 1)
LOOP UNTIL INSTR(a$, "OK")

This code sample demonstrates accessing modem in Basic.
REM Reset modem source:
CLS
OPEN "COM2:9600,N,8,1,BIN,CS,DS,RS" FOR RANDOM AS #1
Reset$ = "ATZ" + CHR$(13) + CHR$(10)
PRINT #1, Reset$;
Inp$ = ""
DO
IF LOC(1) THEN
Inp$ = Inp$ + INPUT$(1, 1)
IF INSTR(Inp$, "OK") THEN
PRINT "Modem reset."
EXIT DO
END IF
END IF
LOOP
END

Related

how to search for a sub string within a string in QBasic

I am creating a simple chat programme in QBasic that will answer questions based on some specific key words present in the user input.therefore I need a way to search for a sub string (I.e. A specific word)within a string.
So, please help me.
To find out if a string contains a certain (sub-)string, you can do this:
text$ = "nonsense !"
IF INSTR( text$, "sense" ) >= 1 THEN
PRINT "This text makes sense !"
END IF
And no, I was not able to test this, as a no longer have QBasic on my PC ;-)
According to the link from the comment above >= 1 is ok
I think INSTR is usually used as follows:
sent$ = "This is a sentence"
PRINT INSTR(1, sent$, "is")
PRINT INSTR(4, sent$, "is")
PRINT INSTR(1, sent$, "word")
the first PRINT command will print a '3' since the first location of "is" within the sentence is at position 3. (The 'is' in 'This')
the second PRINT command starts searching at position 4 (the 's' in 'This'), and so finds the "is" at position 6. So it will print '6'.
the third PRINT command will print a '0' since there is no instance of "word" in the sentence.
Counts the occurrences of a substring within a string.
T$ = "text to be searched and to be displayed"
S$ = "to"
l = 1
DO
x = INSTR(l, T$, S$)
IF x THEN
n = n + 1
l = x + LEN(S$)
ELSE
EXIT DO
END IF
LOOP
PRINT "text '"; S$; "' matches"; n; "times."

I'm new to QBasic and coding in general and I'm making a guessing game that just wont work

I'm new to QBasic and coding in general and I'm making a guessing game that just won't work.
I have to make a guessing game that does not use GOTO or Do statements, and gives the user 5 chances. Here's the code:
chances%=1
dim guess as integer
dim answer as string
randomize timer
rndnum=INT(RND*100+1)
'makinng a title
color 5
locate 12,32
print "welcome to My guessing game."
Print "think of a number between 1 and 100."
color 12
Input "enter you guess: ",guess
while chances%<4
if guess >rndnum then
print "wrong, too high"
elseif guess <rndnum then
print "wrong, too low"
elseif guess=rndnum then
print "your guessed the number!"
end if
wend
chances%=chances%+1
color 14
Print "you took "; chances%;"to guess the number"
color 3
Input would you like to play again (yes/no)?", answer
wend
if answer = "yes" then
?
else
print "have a good day"
end if
end
You are asking for input one time, then you have a closed loop that checks the answer until attempts are greater than four, but attempts does not ever increment because the Wend command tells it to start the loop again without asking the question again or incrementing the counter at all. This is what is called an "endless loop" because the conditions inside the loop will not change. I'll leave it at that and see if you can figure out how to correct both of these issues - note that fixing only one of them will not stop it from being an "endless loop" you must solve both.
You could use WHILE...WEND to run a loop until chances becomes 0.Here is what I mean:
....(rest of code)
chances = 5
WHILE chances > 0
....
if guess > rndnum then
print "wrong, too high"
chances = chances - 1
elseif guess < rndnum then
print "wrong, too low"
chances = chances -1
....
WEND
Your guess must be inside the while wend loop and when the correct answer is given chances% must be set equal to 4 otherwise you end up with an eternal loop.
It is also necessary to increment chances% directly after the first guess. See the slightly changed code. Please also see guesses and change your line saying you took x guess from chances% to guesses
chances%=0
while chances% < 4
Input "enter your guess: ",guess
chances% = chances% + 1
if guess > rndnum then
print "wrong, too high"
elseif guess < rndnum then
print "wrong, too low"
elseif guess = rndnum then
print "your guessed the number!"
guesses = Chances%
chances% = 4
end if
wend
if you're still having problems I found that here:
Input would you like to play again (yes/no)?", answer
...
if answer = "yes"
...
you would have to change answer to answer$ because you cannot save a string inside a number value.
This snip demonstrates a number guessing game in QB64:
REM guessing game.
Tries = 5
DO
PRINT "Guess a number between 1 and 100 in"; Tries; "guesses."
Number = INT(RND * 100 + 1)
Count = 1
DO
PRINT "Enter guess number"; Count; " ";
INPUT Guess
IF Guess = Number THEN
PRINT "Correct! You guessed it in"; Count; "tries."
EXIT DO
END IF
IF Guess > Number THEN
PRINT "Wrong. Too high."
ELSE
PRINT "Wrong. Too low."
END IF
Count = Count + 1
IF Count > Tries THEN
PRINT "The number was"; Number
PRINT "You didn't guess it in"; Tries; "tries."
EXIT DO
END IF
LOOP
DO
PRINT "Play again(yes/no)";
INPUT Answer$
IF LCASE$(Answer$) = "no" THEN
END
END IF
IF LCASE$(Answer$) = "yes" THEN
EXIT DO
END IF
LOOP
LOOP
END

Unable to check received value on serial port in Matlab

I want to check if I receive a dot ('.') on serial port in Matlab. For this I run for loop for 10 times in which I check if I got '.' on serial port then display "A dot is received" otherwise display whatever is receive. But on receiving '.' on serial port it is not displaying "A dot is receibed". Here is my code:-
s=serial('COM5', 'BaudRate',9600);%, 'DataBits',8, 'Terminator','');
fopen(s);
disp('Port succefully Opened');
count=0;
checkdot = '.';
for x = 1:10
recv= fscanf(s);
z = char(recv);
if (z== '.')
disp('A dot is received');
else
disp(z);
end
end
fclose(s);
And here is my output on command window:-
>> Serialcomm
Port succefully Opened
.
.
.
.
.
.
.
.
.
.
So, please tell me where is the mistake.
You can use deblank (removes all whitespace characters) or strtrim (removes only leading and trailing whitespace) to get rid of unwanted characters:
a = sprintf('.\r\n');
disp(a)
.
strcmp(a,'.')
ans =
0
strcmp(strtrim(a),'.')
ans =
1
Also, you're using fscanf to retrieve data from the serial port, try using fgetl instead. fgetl gets one line and discards terminators, so should only retrieve the . (in theory).

redirect input standard MATLAB

From Matlab I call a system command and this command will ask to enter yes or no. How could I redirect to input ?
I've tried:
myCmd = fullfile('control','bin','launch');
cmd = system(myCmd);
=> this will ask following message to enter yes /nos (prompt)
so I've tried
cmd = system([myCmd ,' < ','yes'])
but this is not working.
You can use input function provided in matlab. result = input(prompt) displays the prompt string on the screen, waits for input from the keyboard. More about it here. Hope this helps you.
To request a simple text response that requires no evaluation.
prompt = 'Do you want more? Y/N [Y]: ';
str = input(prompt,'s');
if isempty(str)
str = 'Y';
end

Real-time output from engines in IPython parallel?

I am running a bunch of long-running tasks with IPython's great parallelization functionality.
How can I get real-time output from the ipengines' stdout in my IPython client?
E.g., I'm running dview.map_async(fun, lots_of_args) and fun prints to stdout. I would like to see the outputs as they are happening.
I know about AsyncResult.display_output(), but it's only available after all tasks have finished.
You can see stdout in the meantime by accessing AsyncResult.stdout, which will return a list of strings, which are the stdout from each engine.
The simplest case being:
print ar.stdout
You can wrap this in a simple function that prints stdout while you wait for the AsyncResult to complete:
import sys
import time
from IPython.display import clear_output
def wait_watching_stdout(ar, dt=1, truncate=1000):
while not ar.ready():
stdouts = ar.stdout
if not any(stdouts):
continue
# clear_output doesn't do much in terminal environments
clear_output()
print '-' * 30
print "%.3fs elapsed" % ar.elapsed
print ""
for eid, stdout in zip(ar._targets, ar.stdout):
if stdout:
print "[ stdout %2i ]\n%s" % (eid, stdout[-truncate:])
sys.stdout.flush()
time.sleep(dt)
An example notebook illustrating this function.
Now, if you are using older IPython, you may see an artificial restriction on access of the stdout attribute ('Result not ready' errors).
The information is available in the metadata, so you can still get at it while the task is not done:
rc.spin()
stdout = [ rc.metadata[msg_id]['stdout'] for msg_id in ar.msg_ids ]
Which is essentially the same thing that the ar.stdout attribute access does.
just in case somebody is still struggling with
getting ordinary print-outputs of the individual kernels:
I adapted minrk's answer such that i get the output of each
kernel as if it would have been a local one by constantly checking if the stdout of each kernel changes while the program is running.
asdf = dview.map_async(function, arguments)
# initialize a stdout0 array for comparison
stdout0 = asdf.stdout
while not asdf.ready():
# check if stdout changed for any kernel
if asdf.stdout != stdout0:
for i in range(0,len(asdf.stdout)):
if asdf.stdout[i] != stdout0[i]:
# print only new stdout's without previous message and remove '\n' at the end
print('kernel ' + str(i) + ': ' + asdf.stdout[i][len(stdout0[i]):-1])
# set stdout0 to last output for new comparison
stdout0 = asdf.stdout
else:
continue
asdf.get()
outputs will then be something like:
kernel0: message 1 from kernel 0
kernel1: message 1 from kernel 1
kernel0: message 2 from kernel 0
kernel0: message 3 from kernel 0
kernel1: message 2 from kernel 0
...