Reading from the 128 Serial Port

edited October 2010 in Sinclair Basic
Don't shout at me if this is a silly question. I have Googled extensively, and searched the forums, but I can't find anything discussing what seems to me to be an obvious question. Even the Manual doesn't discuss it!

How do you read incoming data from the Serial port on the Spectrum 128?

The Manual only talks about sending data out of the serial port (using LPRINT) but makes no mention of receiving data coming the other way.

Thanks.
Post edited by trellis on
«13

Comments

  • edited June 2009
    trellis wrote: »
    Don't shout at me if this is a silly question. I have Googled extensively, and searched the forums, but I can't find anything discussing what seems to me to be an obvious question. Even the Manual doesn't discuss it!

    How do you read incoming data from the Serial port on the Spectrum 128?

    The Manual only talks about sending data out of the serial port (using LPRINT) but makes no mention of receiving data coming the other way.

    Thanks.

    with great difficulty, I don't think there's a way to do it from basic easily :(

    if you use machine code it is easy I guess but I don't know z80 mc really.

    I think the z88dk compiler has some serial functions in it's spectrum library but I never really spent much time trying to get them to work.
  • LCDLCD
    edited June 2009
    I think, it works by INPUT or INKEY$ from the opened stream, but I do not remember anymore.
  • edited June 2009
    LCD wrote: »
    I think, it works by INPUT or INKEY$ from the opened stream, but I do not remember anymore.
    Give that man a coconut!
    Of course, it's just a stream isn't it. Duh! I know it was a stupid question.
    There's even an example program in the manual to read data from the RS232 port :D
    Thanks folks.
  • edited June 2009
    Interesting bug: if you have a program line containing "FORMAT LINE 9600" or similar to set the RS232 baud rate, and you do a Renumber (from the Edit menu), your baud rate is interpreted as a line number and altered to whatever Renumber changed line number 9600 into.
  • edited June 2009
    trellis wrote: »
    Give that man a coconut!
    Of course, it's just a stream isn't it. Duh! I know it was a stupid question.
    There's even an example program in the manual to read data from the RS232 port :D
    Thanks folks.

    I'm sure there was some stupidly annoying reason that this didn't work properly on the +3, but maybe I'm just mis-remembering it's been a while since I did any serial stuff with the speccy
  • edited June 2009
    guesser wrote: »
    I'm sure there was some stupidly annoying reason that this didn't work properly on the +3, but maybe I'm just mis-remembering it's been a while since I did any serial stuff with the speccy
    You may well be right. I haven't actually tested it, so it's quite possible that it doesn't actually work.
  • edited June 2009
    trellis wrote: »
    You may well be right. I haven't actually tested it, so it's quite possible that it doesn't actually work.

    I remember it took me ages to actually get the cable working.
    cause the pin names in the manual are "wrong" iirc
    the handshaking pins are silly I think
  • edited June 2009
    guesser wrote: »
    I remember it took me ages to actually get the cable working.
    cause the pin names in the manual are "wrong" iirc
    the handshaking pins are silly I think
    Someone gave me an RS232 cable with a DB25 male plug on the end, so I'll have a look at that and see if I can deduce the correct wiring. There's also these of course.
  • edited June 2009
    trellis wrote: »
    Someone gave me an RS232 cable with a DB25 male plug on the end, so I'll have a look at that and see if I can deduce the correct wiring. There's also these of course.

    I did fathom it out in the end, mostly by trial and error, and some googling. I think two of the pins have the wrong names (not swapped, just "wrong" in some way)
  • edited June 2009
    The Amstrad PC1512 technical manual has an appendix which explains, inter alia, why RS232 pin naming is so weird. On a 'terminal'-class device the pins mean one thing, and on a 'modem'-class device they mean the opposite.

    Further, there's the whole question of which lines get used for handshaking; PCs these days tend to use RTS/CTS handshakes, but the 128 uses DTR/CTS, giving more opportunities for fun.
  • edited June 2009
    The Amstrad PC1512 technical manual has an appendix which explains, inter alia, why RS232 pin naming is so weird. On a 'terminal'-class device the pins mean one thing, and on a 'modem'-class device they mean the opposite.

    Further, there's the whole question of which lines get used for handshaking; PCs these days tend to use RTS/CTS handshakes, but the 128 uses DTR/CTS, giving more opportunities for fun.

    that's the one, normally it's normally either RTS/CTS or DTR/DSR
  • edited June 2009
    Reminds me of the days when we used to string up a null modem serial cable to play Doom at University.
  • edited June 2009
    Thanks to the trusty RS232 Breakout Box, I have got the +3 communicating with the PC.
    Using INKEY$ and LPRINT I've got characters typed on the +3 appearing in HyperTerminal on the PC, and vice-versa, but obviously using INKEY$ in a loop is far to slow to do anything useful.
    10 FORMAT LPRINT "R"
    20 FORMAT LINE 9600
    30 OPEN # 4,"P"
    40 LET M$=INKEY$ #4
    50 LET N$=INKEY$
    60 IF LEN M$>0 THEN PRINT M$;:LPRINT M$;
    70 IF LEN N$>0 THEN LPRINT N$;:PRINT N$;
    80 GO TO 40
    

    What is the correct method for using INPUT with streams? The manual seems to gloss over this and doesn't explain it properly at all.

    If I type
    INPUT #4;a$
    
    the +3 seems to start accepting characters from the PC, but as soon as it sees a carriage return, it squirts a stream of garbage out of the RS232 port, and reports E Out of Data. After this, a$ is undefined.

    The stream of garbage it squirts out is always the same:
    :?:??0???????Q ????F#~???Q
    

    Any ideas appreciated!
  • edited June 2009
    While we wait for someone to help you with that issue, here's a copy of an e-mail where Cristian Secară answered a few questions I had:
    On Mon, 25 May 2009 16:43:18 -0700, Bruno Florindo wrote:

    > [...] Can't we explore the serial or aux port on the back of the
    > +2A/+2B/+3 and use it for data transfer?

    I will try to think about that, but as far as I suppose this is not
    possible, or at least possible but not reliable.

    The serial port is made with the help of the AY registry, is very
    simple as hardware and is suitable merely for data transfers from
    Spectrum (like a printer), where no critical data is transferred.

    At least this is what I am thinking right now. Unfortunately I have no
    time to analyze a test case to see what must be done.

    > There would be no need for hardware or internal modding, only a
    > simple cable and software on both the PC and Spectrum. Possible?

    The "simple cable" is the simplest thing. Who will make the rest ?
    Such thing has two parts: the sender (the PC) which must use some
    program to send the required bytes and the receiver (on +3) which must
    know what it receives and put the data properly where it is required.

    So: I doubt that this can be done in a reliable way (i.e. with *really*
    no error during transfer), but even so, I don't know who will do that
    (the coding).

    The CP/M for +3 comes by default with a serial transfer program (source
    code and a compiled .FID (field instalable device) file), designed for
    modem communications, but it requires a special hardware thing, based on
    some old Intel SIO circuit. That device was supposedly available loong
    time ago, but I have never seen it. I can look in the CP/M manual to
    see the reference for it, if you like so.

    Put yourself this question: why did they made a special hardware device
    (and a supporting software) if the AY-based serial port was already
    there ?

    Cristi

    --
    Cristian Secară
    http://www.secarica.ro/

    I didn't want to bother him again because my original intention was to find a way to explore the ports without the need for additional hardware. I don't understand all the technical details, but it seems Amstrad gave us an aux port that's less reliable than the one on the IF1. :o

    Still, the ports are there and the idea of using them has always fascinated me. Simple communication between a Spectrum and a PC without interfaces. Just terminals on each side to send and receive binaries and, who knows, the ability to write EDSK images without having to go through the usual steps.

    If error checking isn't part of the AY's serial data capabilities, can't we send use some type of checksum every n bytes?
  • edited June 2009
    zxbruno wrote: »
    While we wait for someone to help you with that issue, here's a copy of an e-mail where Cristian Secară answered a few questions I had:



    I didn't want to bother him again because my original intention was to find a way to explore the ports without the need for additional hardware. I don't understand all the technical details, but it seems Amstrad gave us an aux port that's less reliable than the one on the IF1. :o

    Still, the ports are there and the idea of using them has always fascinated me. Simple communication between a Spectrum and a PC without interfaces. Just terminals on each side to send and receive binaries and, who knows, the ability to write EDSK images without having to go through the usual steps.

    If error checking isn't part of the AY's serial data capabilities, can't we send some time of checksum every n bytes before anything is done with the binary file/chunk?

    Hmmm, yes, he makes a very persuasive argument doesn't he :-(

    Actually, at the best of times RS232 only offers a very simple parity bit for error checking, and in most cases even that isn't used, so there isn't any kind of error detection. Any error checking and recovery is usually left for higher-layer protocols to deal with, so that's not really a criticism that can be levelled at Amstrad's implementation ;-)

    I was hoping that using INPUT instead of INKEY$ might enable me to pull in a chunk of bytes at wire-rate into a BASIC string variable, and deal with it from there. But my experimentation so far has shown that this may not be possible. Even sending a long string out using LPRINT doesn't go at wire speed, even at 9600.

    It could be that the BASIC ROM routines are very inefficient, but I suspect the problem with the +3's built-in port may simply be one of speed.
  • edited June 2009
    trellis wrote: »
    Hmmm, yes, he makes a very persuasive argument doesn't he :-(

    Actually, at the best of times RS232 only offers a very simple parity bit for error checking, and in most cases even that isn't used, so there isn't any kind of error detection. Any error checking and recovery is usually left for higher-layer protocols to deal with, so that's not really a criticism that can be levelled at Amstrad's implementation ;-)

    I was hoping that using INPUT instead of INKEY$ might enable me to pull in a chunk of bytes at wire-rate into a BASIC string variable, and deal with it from there. But my experimentation so far has shown that this may not be possible. Even sending a long string out using LPRINT doesn't go at wire speed, even at 9600.

    It could be that the BASIC ROM routines are very inefficient, but I suspect the problem with the +3's built-in port may simply be one of speed.

    I gather that the problem is that there is no hardware fifo buffer and the spectrum is effectively emulating the serial port buffers in software.
    This might be nonsense but I'm sure someone has said it doesn't have proper input and output buffers like a proper rs232 controller
  • edited June 2009
    there is a machine code program which provides a serial terminal on the +3, and supposedly does xmodem transfers (I never got that bit to work, but I was probably doing something wrong on the PC side, then my serial cable disintegrated and I never got around to it again)

    it's called zfst and isn't easy to find but there's a copy here http://www.geocities.com/ResearchTriangle/4535/tools.html
  • edited June 2009
    I made a ROM patch to replace TAPE routines with RS232 routines. Practically, ZX Spectrum is sending a TAP file while saving and receiving TAP file when LOADing.

    Working with files cannot be more easy. On PC, I've made simple BAT file with syntax "s filename.tap" to "send" file to ZX. "r filename.tap" to "receive" file from ZX.

    My system is MB02, but with low effort it can be implemented on divIDE, MDOS or other systems allowing to map RAM instead of ROM.

    http://omega.webnode.com/products/rsp/
  • edited June 2009
    guesser wrote: »
    there is a machine code program which provides a serial terminal on the +3, and supposedly does xmodem transfers (I never got that bit to work, but I was probably doing something wrong on the PC side, then my serial cable disintegrated and I never got around to it again)

    it's called zfst and isn't easy to find but there's a copy here http://www.geocities.com/ResearchTriangle/4535/tools.html
    Thanks, I didn't know that utility existed!

    It works very nicely as a terminal, but like you, I can't seem to get the XModem transfer function to work in either direction. HyperTerminal has "XModem" and "1K XModem" options - neither works to ZFST.

    Even with ZFST on the real +3 talking to ZFST running on an Emulated +3 on the PC, nothing happens, yet I see lots of data flying around on the Breakout Box.

    (In all cases, the two machines talk to each other in Terminal mode, so it's not a comms problem).

    However, it looks like it could be a good starting point for further development.
  • edited June 2009
    trellis wrote: »
    Thanks, I didn't know that utility existed!

    It works very nicely as a terminal, but like you, I can't seem to get the XModem transfer function to work in either direction. HyperTerminal has "XModem" and "1K XModem" options - neither works to ZFST.

    yeah the hyperterminal transfer thing looks to be totally incompatible
    but I never got around to trying the utilities that he documents in the manual.
    trellis wrote: »
    Even with ZFST on the real +3 talking to ZFST running on an Emulated +3 on the PC, nothing happens, yet I see lots of data flying around on the Breakout Box.

    (In all cases, the two machines talk to each other in Terminal mode, so it's not a comms problem).

    that's probably a handshaking issue, it does mention that that is the most likely cause in the manual. so either the cable is wrong, or the emulator doesn't fully emulate the serial port/use the same handshaking/allow bit banging the pins.

    it would be helpful if you could use another computer wired onto the data lines to display what data is going back and forth on a terminal.

    though I suspect I already know, it's some sort of ping type thing if I remember correctly. the receiver telling the sender to go ahead and send the next packet
    trellis wrote: »
    However, it looks like it could be a good starting point for further development.

    he obviously got it working, so it must be possible, at least when the stars are aligned, you're wearing the right colour shirt and the null cable is lying along an ancient leyline
  • edited June 2009
    Does anyone remember this user?

    http://www.worldofspectrum.org/forums/showpost.php?p=47311&postcount=6

    It's an old post, but after checking his post history I found he used zfst extensively without any problems and recommended it several times.

    Could it be that the problem is HyperTerminal? It seems he was doing everything with Linux, not Windows.
  • edited June 2009
    zxbruno wrote: »
    Does anyone remember this user?
    Wasn't he Australian?
    I wanna tell you a story 'bout a woman I know...
  • edited June 2009
    I think the problem is definitely with hyperterm. I also tried an old dos terminal utility that wouldn't work either.

    to avoid any ambiguity I think you need to try a) the utility he mentions in the manual, b) a real serial port if you aren't already, and c) a cable wired the way it says in the manual if you aren't already.
  • edited June 2009
    guesser wrote: »
    I think the problem is definitely with hyperterm. I also tried an old dos terminal utility that wouldn't work either.

    to avoid any ambiguity I think you need to try a) the utility he mentions in the manual, b) a real serial port if you aren't already, and c) a cable wired the way it says in the manual if you aren't already.
    Sounds more like the problem is with the XModem protocol, if no two implementations can communicate with one another.
    a) I'll boot Knoppix and give it a try, but even if it works, it'll be pretty useless. I really need it to talk to the emulated +3 to trasfer disk files.
    b) It's a 9-pin D connector on the back of my PC, which Windows refers to as COM1. Is there such a thing as a 'fake' serial port?
    c) Does he mention how to wire the cable in the manual? All I can find is a brief mention of a 'Null Modem' cable, which is effectively what I'm using. I don't think the cable wiring is the issue as I have no problem sending characters in both directions between the machines.
  • edited June 2009
    guesser wrote: »
    I gather that the problem is that there is no hardware fifo buffer and the spectrum is effectively emulating the serial port buffers in software.
    This might be nonsense but I'm sure someone has said it doesn't have proper input and output buffers like a proper rs232 controller

    I suspect the same thing. I successfully connected a Spectrum (clone actually) IF1 serial port with a PC RS232, using hardware handshake. It works very well, even at 19200 baud. The main issue is to send a single byte at a time from the PC side to the Spectrum, otherwise the Spectrum would choke, because it's not fast enough to receive more than one byte at once (I mean, to call the SendBytes function each time with only one byte).
    I tested 2 utilities, that work great with this connection:
    SPXFR
    PCZX.
  • edited June 2009
    trellis wrote: »
    Sounds more like the problem is with the XModem protocol, if no two implementations can communicate with one another.
    a) I'll boot Knoppix and give it a try, but even if it works, it'll be pretty useless. I really need it to talk to the emulated +3 to trasfer disk files.
    b) It's a 9-pin D connector on the back of my PC, which Windows refers to as COM1. Is there such a thing as a 'fake' serial port?
    c) Does he mention how to wire the cable in the manual? All I can find is a brief mention of a 'Null Modem' cable, which is effectively what I'm using. I don't think the cable wiring is the issue as I have no problem sending characters in both directions between the machines.

    I meant like a USB to serial adapter, if you have a proper hardware com port that's fine.

    I think the hyperterm implementation and the spectrum implementation are using different checksums and handshaking protocols. The hyperterm version is a "clever version" and the speccy is using a "simplest version possible" implementation
  • edited June 2009
    I've asked the users of Speccy.org to provide their input. I know of at least one user over there who understands packet data transfer.

    It seems that the xmodem protocol changed a lot over the years (contrary to the author's wishes). Maybe the +3 uses the original one.

    edit: Has anyone tried to emulate a +3 with Spectaculator and transfer data to a real +3 by using Spectaculator's serial port redirection? If Spectaculator's emulating the +3 serial port correctly it should send data that the real +3 can understand.

    edit2: Hyperterminal's xmodem implementation is described here:

    http://msdn.microsoft.com/en-us/library/ms817548.aspx
  • edited June 2009
    I just tried spectaculator to see if two emulated +3s would talk to each other and remembered I'd tried that myself in the past.
    spectaculator, 5.30 at least, only redirects serial *output* it doesn't read input by the look of things, so it will not work for transfers with zfst.
  • edited June 2009
    guesser wrote: »
    I just tried spectaculator to see if two emulated +3s would talk to each other and remembered I'd tried that myself in the past.
    spectaculator, 5.30 at least, only redirects serial *output* it doesn't read input by the look of things, so it will not work for transfers with zfst.
    You're right. That explains why the Emulator-to-real-machine link didn't work.
    I have got two real +3 machines, but only one PSU, so I'm not currently in a position to try linking two real +3s together, which would be an interesting (if rather pointless) exercise :-)
  • edited June 2009
    I would probably be willing to send you an extra PSU just to help with this. I'm really interested in this topic and I hope it doesn't die (like the previous ones) before we find solutions.

    The folks at Speccy.org have replied to my topic. I shall be translating and posting their input here later today.
Sign In or Register to comment.