Hi, I wrote a simple code for reading an optical touchscreen that sends coordinates serially in 8 bytes +CRNL via a FTDI chip USB to the PC.
The code is working nicely and drawing on a Screen21 for verification of touchscreen working all over the place.
There are two problems.
First, I want to interrupt (end) the code and return to "Cmd Screen", so I used a "LOOP UNTIL INKEY$=CHR(13)", it works, but only after I touch the screen. This lockup happens because the "LINE INPUT #1, DAT" locks and still there until data +CRNL is pushed into the "windows serial port". The "LINE INPUT" keeps waiting until a carriage return + new line (0x0D, 0x0A) appears on the receive buffer, so it hold the code there. I wish there was a way to just verify if the input buffer (for the LINE INPUT) has data already or none yet. If yes, then I would go and execute the LINE INPUT, if no, then I will do the INKEY$, verify what key and stop the code if requested, or go back to verify if data exist on the LINE INPUT buffer. May be it is easy to verify, I just don't know.
Second, I was unable to make FreeBasic to read data from COM7:, even that the OPEN "COM7:9600,N,8,1" as 1 worked nicely, without errors, but no data can be read using LINE INPUT. The COM7: was allocated by Win10 as the next available port when I inserted the touch screen at an USB port. From COM1 to COM6 windows Device Manager (PORTS) show as "used by other application". And not, BIOS/CMOS setup only shows COM1 as SuperI/O configuration, for some reason Windows blocked from COM2 to COM6. The physical motherboard has only COM1 header. By the way this SSD is a test for several motherboards, may be it locks COM2-COM6 from previous motherboard. Only after I forced this FTDI COM7 to be COM2, windows complains, reboots twice, then COM2 becomes empty (free) and FTDI USB (Serial) appeared as COM2 on Device Manager, then FreeBasic was able to read data via LINE INPUT. So, I don't know if there any limitation on FreeBasic for ports higher than COM2 or was something else. It works nicely (LINE INPUT ok) as COM2, but I want to find out why it didn't work with COM7, next PC may give me trouble. Any hint?
Thank you, enjoy the weekend.
FreeBasic reading COMx: port
FreeBasic reading COMx: port
Last edited by fxm on Feb 04, 2022 16:32, edited 1 time in total.
Reason: Post moved from "General" to "Hardware Interfaces / Communication".
Reason: Post moved from "General" to "Hardware Interfaces / Communication".
Re: FreeBasic reading COMx: port
Hi, reading FreeBasic documentation I found (and remember from old BASIC) the LOF, LOC, EOF from opened files/devices, and I will try, never used for COM ports, only for data files.
Post Edit: Tested all, no good, could not make it work to anticipate if data is there to be read or not.
Post Edit: Tested all, no good, could not make it work to anticipate if data is there to be read or not.
Re: FreeBasic reading COMx: port
I wrote some demos starting at this post here: Arduino Serial Communication Demo #1
It was for talking to an Arduino, but the host side code which should run on windows / linux may help give you some ideas how to poll for data and read the serial port without blocking.
Re: FreeBasic reading COMx: port
The most common confusion comes from the fact that FreeBasic evaluates the levels of the status lines by default.
These must be explicitly disabled if they are not connected accordingly.
Connect Tx with Rx on your FDTI and try this minimalistic code:
Frank
These must be explicitly disabled if they are not connected accordingly.
Connect Tx with Rx on your FDTI and try this minimalistic code:
Code: Select all
Open COM "COM2:9600,N,8,1,BIN,Cs0,Ds0,Cd0,RS" As #9
If Err <> 0 Then Print "Error opening ComPort." : Sleep : End
Dim x As String
Line Input "Enter a string + [Enter]: ", x
Print #9, x;
Sleep 1 'wait for transmission of all chars
Do
Print LOC(9),
Print Input (1, #9)
Loop Until LOC(9) = 0
Close #9
Sleep
Re: FreeBasic reading COMx: port
Very good advice. Your sample works for me tying TX to RX and no control lines (took me a while to find a USB serial adapter in my stash).
Code: Select all
Enter a string + [Enter]: hello
1 h
4 e
3 l
2 l
1 o
Line Input can work but is not a robust solution if it's possible that an end of line is never sent from the device because line input will block.
Just for demonstration purposes of where line input would work, we expect the end-of-line, using a physical device TX tied to RX, no control lines:
Code: Select all
Open COM "COM10:9600,N,8,1,BIN,Cs0,Ds0,Cd0,RS" As #9
If Err <> 0 Then Print "Error opening ComPort." : Sleep : End
dim as string question = "hello there?"
dim as string response = ""
print "Send: " & question
print #9, question
'' wait 1/10th of a second
sleep 100
print "Size: " & loc(9)
line input #9, response
print "Recv: " & response
close #9
/'
OUTPUT:
Send: hello there?
Size: 14
Recv: hello there?
'/