• Hello MLAers! We've re-enabled auto-approval for accounts. If you are still waiting on account approval, please check this thread for more information.

Serial Port Programming

Hi!

I was wondering how to program the serial port, either one, without using a driver? I've looked at the serial manager as much as I can, but I can only find information that talks about using a driver, no direct control. How can I control it directly? (Of course, I'd accept a driver that allows direct control :) !)

Could someone provide a code snippet, preferably in C, that I could use?

Thanks

onlyonemac

 
Here is the user manual for the SCC.

There are many ways to use it, so providing code to "control it directly" probably isn't terribly useful without knowing what you're trying to do. There's also differences in how it can be accessed across machine models like the IIfx and the Q9x0 machines.

Then, depending on what has been initialized, you may need to initialize some ancillary machine specific hardware.

I used the serial port for some ROM debugging, which just initialized the SCC hardware (and VIA1 since I was running before much was initialized), didn't use interrupts, and write out debugging information a character at a time.

Code:
  ; This code initializes the serial access on a IIsi 
 ; It should work for II, IIx, SE/30, IIci, IIcx, and the Iisi, but so far
 ; testing is limited to the IIsi.

 ; The VIA1 initialization values are taken from the ProductInfo table

 ; Some minimal VIA1 initialization is required for the SCC to work.
 move.l #$50F00600, A0 ; vDirA
 move.b #$2F, (A0)
 move.l #$50F00200, A0 ; vBufA
 move.b #$26, (A0)

 ; SCC port A is the "Modem" port.
 move.l #$50F04002, A0 ; SCC aCtl

 move.b #9, (A0)
 move.b #$C0, (A0) ; resets SCC (both ports)

 move.b #1, (A0)
 move.b #0, (A0)   ; disable rx/tx

 move.b #4, (A0)
 move.b #$44, (A0) ; x16 clock, 8 bit sync char, 1 stop bit/char, no parity

 move.b #11, (A0)
 move.b #$50, (A0) ; BR generated timing

 move.b #12, (A0)
 move.b #10, (A0)  ; lower byte of time constant (9600 baud)

 move.b #13, (A0)
 move.b #0, (A0)   ; upper byte of time constant (9600 baud)

 move.b #14, (A0)
 move.b #1, (A0)   ; BR generator enable

 move.b #15, (A0)
 move.b #0, (A0)   ; No interrupts

 move.b #9, (A0)
 move.b #0, (A0)   ; No interrupts

 move.b #3, (A0)
 move.b #$C1, (A0) ; Rx 8 bits/char, Rx enable

 move.b #5, (A0)
 move.b #$6A, (A0) ; Tx 8 bits/char, Tx enable, RTS (RTS enables the line driver)
This makes sure the hardware is ready to transmit, sends the character, and polls to make sure it has been sent:

Code:
  ; This code sends a byte from D0 and returns through A6

 move.l #$50F04002, A0 ; SCC aCtl
checkstatus:
 move.b #0, (A0)
 btst #3, (A0)         ; check for Tx Ready
 beq checkstatus

 move.l #$50F04006, A0 ; SCC aData
 move.b D0, (A0)       ; transmit

 move.l #40000, D1
Loop:
 dbra D1, Loop         ; Wait for send

 jmp (a6)
 
Essentially what I'm wanting is code that will allow me to set a protocol and baud rate, particularly 2400 8N1, and then send a specified byte from the port, or receive a byte. What I'm doing is some electronics experimentation, and I've got what I need for the other side, so I really just need to send and receive stuff to and from my microcontroller.

P.S. I don't need any circuit diagrams!

 
It sounds like it would be easier, faster, and more reliable to just use the Mac OS serial driver.

But if you really want to bang on the hardware directly, the linked user manual includes the register descriptions necessary to do what you're wanting to do. You'll need to compute the proper clock multipliers for the 2400 baud rate, and the rest of the initialization should be similar to what I've pasted.

The receive code will be similar to the transmit code I pasted, you'll just be polling on the receive status register until a byte is ready, then read it.

 
There's a plug-in for Hypercard called Serial Port OSAX which might be useful.

 
In the Dartmouth collection of XFNC / XCMD code also some similar thing is available: Look for "SerialHandler". However, some system software extension might need to be removed. As far as I remember it is called "Serial (Port) Arbitrator" which causes SerialHandler not to work. Unfortunately I have no running system for comparison at the moment, as my appropriately configured PB 180s refuse to boot :-(

Probably a simple terminal application like ZTerm (scriptable!) would be sufficient for experimental transmission of a few bytes. You might transcode dual to decimal numbers with regard to the bit order to input and send the byte as an ASCII number. I successfully used a simple HyperCard stack with SerialHandler XFCN in the stack ressources to set the parameters of a stepper motor controller. If you have difficulties to set up communiation, check port parameters, cable and correct transmission. You may loop back the serial port to the second serial port using a printer cable to directly check for proper operation. For debugging of serial comunication I used to use an additional computer in the middle, running a little program to display any input from one port and send it without modification through the other port and vice versa.

 
Could I have a special driver which just mirrors the data sent to it (not like a printer driver, which does a lot else as well)? I would also need example code to use the driver.

I'm really not into assembly, registers, or baud rate calculations.

Essentially what I want is:

Code:
/*Open serial port.*/
OpenPort(handle *port, int portID);

/*Set baud rate and parity.*/
SetParity(handle port, int parityconstant);
SetBaud(handle port, int baudrate);

/*Send some data.*/
Send(byte data, handle port);

/*Receive some data.*/
data = Receive(handle port);
Note: All commands are imaginary only!!!

 
What you ask for is exactly what is implemented in the OSAX (AppleScript extension) suggested by Bunsen or in the XFCN from the Dartmouth HyperCard stack. The source code is written in C or Pascal but might be difficult to obtain. Consider to get a copy of the Mac Programmer's Cookbook. This book helped me to gather valuable information I needed to set up my first programme to remotely control an electronic balance via RS422 <-> RS232 communication.

Have a terminal programme available to independently check for proper wiring of your setup, before trying to debug your own code.

 
I've just got some help from the Inside Macintosh:Devices book. Apparently the system allows you to open the port and read/write data directly, using a built-in driver in the System itself. I'll try it later...

 
For future reference, that's "the Mac OS serial driver".

When you say you want to access something "directly", that typically means you want to bypass the driver and talk to the hardware directly.

In your original post, you explicitly said you didn't want to use the driver mentioned in all the documentation.

 
Sorry for the confusion, I'm new to this. What I mean is to not have protocols and printer page setup boxes in my way, basically I want to open a port and off we go! Like I said, I'm happy to use a "direct" driver-that is, not one with something else "in front of it".

When I said "information that talks about a driver", I was not looking in the Inside Macintosh series (hadn't dowloaded it yet); instead, I was looking in the THINK programming referance and in the header files.

Anyway, I've had bad luck setting stuff up-I can't get the initialization to work!

 
If someone only needs quick and dirty solutions for some tasks to automate: AppleScript, KeyQuencer, Folder Watcher and Okey Dokey make an excellent combo to let the Mac do some stuff unattended that usually needs user interaction. KeyQuencer can wait for some specific window to come to front, it can also execute AppleScript code AND it comes with an OSAX to let you implement KeyQuencer capabilities into AppleScript commands. Folder Watcher just sits in the background an looks what happens to the contents of a directory. Under some preset trigger condition FolderWatcher may just execute some AppleScript program. Want to have some folder actions like printing stuff that was dropped into a folder? With the appropriate setup it works like a charm, but with 7.1 (or above), yet! Many programs are more or less scriptable, with their own simple scripting language (for example little ZTerm or big Excel) or they just support AppleScript.

P.S.: for HyperTalk language there are two different kinds of additional code snippets to extend the ressource fork of a HyperCard stack: external command "XCMD" and external function "XFCN". For AppleScript there is a folder inside the System Folder to hold the OSAX scripting additions for AppleScript. Plenty of useful and funny extensions are available, even in well sorted collections. I would encourage anyone who is setting first steps into Mac programming to explore such sources for well tested and fully operational code. To get a thing like SerialHandler to work as expected under any circumstance might turn out to be quite difficult if you are new to an IDE like Think Pascal.

 
Back
Top