
FB II Compiler
PG PRO
Debugging
Memory
System
Mathematics
Resources
Disk I/O
Windows
Controls
Menus
Mouse
Keyboard
Text
Fonts
Drawing
Sound
Clipboard
Printing
Communication
ASM
|
COMMUNICATION
Read from a serial port
I am trying to read/write a small AD/DA converter connected to my Mac's serial port. The device needs a short 5-byte command to output a digital signal, and has a read routine that should return several parameters. Neither is working. The routine provided in the FutureBasic reference manual doesn't help much, and is not documented very well. It loops through <READ #_modemPort,A$;0> with a read byte=0 until <LEN(A$)<>0>. This doesn't hang up, but it isn't reading anything from the port. If I put a byte of 1 or more on the command <READ #_modemPort,A$;1> the program hangs up totally, necessitating a forced restart.
Anyone have any routines for reading & writing data from & to a serial port AD/DA device?
You may wish to go to <http://www.microvention.com> and download the Mac version of the development software. It has some FB examples for serial port interfacing, though maybe not specifically for AD/DA use. These are for talking to their controller, but you might find it helpful.
Thanks for responding. Below is one of the permutations I've tried. It's adapted from the example given in Tech Support Goodies that came with FB. I've tried: deleting HANDSHAKE, changing the baud rate and deleting all the modem arguments (all the parameters for the modem match those in the AD/DA manual), changing the number of bytes suffixed to READ and WRITE (including 0, which the FB Reference suggests), varying the loop in which the READ is imbedded, and not converting the ASCII characters. No matter what, it hangs up if the READ suffix is anything but 0, and doesn't hang up but returns no data if the READ suffix is 0.
Any ideas?
thisPort = _modemPort
_baud = 9600
WINDOW 1, "Receive Data" : TEXT ,,,0
OPEN "C", thisPort, _baud, _noParity, _oneStopBit, _eightBits
HANDSHAKE thisPort, _none
PRINT%(10,20) "Waiting for data";
L$ = ""
out$="!"+CHR$(48)+"RC"
DO
WRITE #thisPort, out$;4
READ #thisPort, rcvd$;0
char = ASC(rcvd$) AND 127
IF char AND char <> 13 THEN L$ = L$ + CHR$(char)
UNTIL char = 13 OR INKEY$ = "."
PRINT:PRINT "Received :";L$;"xx"
INPUT "Return to continue";co$
CLOSE #thisPort
END
Lot's of things can be wrong. Make sure that you're connected with the proper baud rate, parity, etc. like:
OPEN "C",_modemPort,9600,_noParity+_twoStopBits,_sevenBits,256
If that's correct, then check the routine using:
HANDSHAKE _modemPort,_XON
or
HANDSHAKE _modemPort,_none
Sometimes, ports are slow so you have to put in delays, like:
DO
PRINT #_modemPort,CR$;LF$;
DELAY 100
READ #_modemPort,rcvd$;0
INC(counter)
UNTIL (counter > 14 OR rcvd$ = CQ$ ) 'time out or get acknowledgement
But, generally, I use something like:
LOCAL FN checkPort
READ #_modemPort,a$;0 'grab one byte
SELECT a$
CASE ":" 'start of record
PRINT ": start of record";
CASE ELSE
READ #_modemPort,b$;0 'grab second byte
d$ = a$ + b$
LONG IF d$ <> ""
PRINT d$;" ";
END IF
SELECT d$
CASE "00" 'nothing
CASE "0D" 'return
CASE "80" 'text field
CASE "0A" 'line feed
PRINT #_modemPort,"#";
c$ = ""
CASE "FC" 'ASC(252)
CASE "FF" 'end of record
PRINT #_modemPort,"#";
CASE ELSE
b$ = "&H" + a$ + b$
test = VAL(b$)
c$ = c$ + CHR$(test)
END SELECT
END SELECT
END FN
DO
HANDLEEVENTS
fn checkPort
UNTIL gFinished
---
This may be your problem here. You write only 4 characters to the device but not a carriage return. Try adding a CHR$(13) to the end of out$ and sendingout 5 characters.
--
1) Try using a program like ZTerm to access the device to determine that it is indeed functioning the way you think it is; where your program sends "!"+CHR$(48)+"RC" you can simply type "!0RC" where that second character is azero.
2) Try this modified program:
thisPort = _modemPort
WINDOW 1, "Receive Data" : TEXT ,,,0
OPEN "C", thisPort,9600, _noParity, _oneStopBit, _eightBits
'I hope the device really wants those settings - namely 9600, _noParity,
_oneStopBit, _eightBits
HANDSHAKE thisPort, _none
PRINT%(10,20) "Waiting for data ";
L$ = ""
out$="!"+CHR$(48)+"RC"
WRITE #thisPort, out$;4
DO
READ #thisPort, rcvd$;0
LONG IF LEN(rcvd$)
char = ASC(rcvd$) AND 127
IF char <> 13 THEN L$ = L$ + CHR$(char)
PRINT rcvd$;
END IF
UNTIL char = 13 OR INKEY$ = "."
PRINT:PRINT "Received :";L$;"xx"
INPUT "Return to continue";co$
CLOSE #thisPort
END
|