HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite, "How To Use It" main page

Pascalite hardware: Using the RS-232 port: Receiving data


Using the RS-232 port to receive data



The following assumes you've got your Pascalite set up and working. If it gives you what you want, great. Be advised, however, that this page is subordinate to others. Visit my Pascalite, "How To Use It" main page if you came drectly here and there are things "missing".




All along, we've been using the Pascalite's RS-232 port for communication with the development PC. Fine. But it can be used for other things, too... just not at the same time!

If you have not already completed the simpler RS-232 tutorial, which you can access here, you might want to complete that first. It sets the Pascalite up to transmit some RS-232 data to another computer. This tutorial builds on that, but uses RS-232 data arriving from the other computer to trigger the output from the Pascalite. As it stands, the tutorial's program is "pointless", but it gives you the tools for useful programs.

For this tutorial, we need a Pascalite II Plus is attached to a "big" PC via the same RS-232 cable as we use for programming the Pascalite. The big PC will be running Hyperterminal. The Pascalite will watch for a request from the big PC. That request will be generated by a user pressing the "s" key on the big computer. The Pascalite will then send to the big PC the current state of the Pascalite's data line d7, which will have been set up as an input, and wired to a switch, as explained in an earlier beginner's tutorial.

Start by making sure your d7 programming and switch are okay. Use something along the following lines....
program TestD7In;
{Assumes a switch wired to D7 which
 results in that line being high if
 switch NOT pressed, low if pressed.

 The two "if" structures are alternatives.
 You can have the switch status reported
 via the LCD and/or an LED on D0

 DO NOT include the def_out line if your
 Pascalite's d0 line is connected to
 anything that will drive it high or low.}

var bTmp:byte;
begin
def_out(d0);{If you have an LED on d0}
repeat
read(portd,bTmp);
bTmp:=bTmp and 128;
if bTmp=128 then write(LCD,48) {no ; here}
       else write(LCD,49);
if bTmp=128 then reset(d0) {no ; here}
       else set(d0);
until 4=5;
end.
Now check that your RS-232 / Hyperterminal connection is working. See my previous RS-232 tutorial on this.

Get the following running. Rather like TestD7In, if you change the state of D7, you should see something happen on a LED on D0, on the LCD screen, an now, in addition, in your Hypwerterminal window. (The full, final program is listed at the end of the tutorial.)

Remember: You have to keep "connecting" and "disconnecting" your Hyperterminal "Call". If, when you try to download the laterst version of your Pascalite program you get "Can't Open Device COM", then you probably forgot to disconnect Hyperterminal. (The connection state is shown in the lower left of the Hyperterminal window.) If you are seeing nothing in the Hyperterminal window when you expect to: be sure Hyperterminal is connected to the Pascalite.
program DemoRS232;
var bTmp:byte;

begin
def_out(d0);
write(LCD,255); {clears LCD}
repeat
set(d0);{turns LED on}
read(portd,bTmp);
bTmp:=bTmp and 128;
if bTmp=0 then begin
  write(LCD,"hi");
  write(RS232,"From Pascalite");
  write(RS232,13,10);
  reset(d0);
  delay(500);
  end;
until 4=5;
end.
Now make the following additional setting to your Hyperterminal's Pascalite "Connection", i.e. the config file that you use when connecting to the Pascalite via Hyperterminal. In Hyperterminal, invoke...

File|Properties
Settings tab
Ascii Setup button
Echo Typed Characters Locally

(Then re-save the "Connection")

Make sure DemoRS232 as developed so far still works!

Now, just before the "until 4=5" just before the final "end." of the program, add....
if RS232_New_Byte then begin
  read(RS232,bTmp);
  write(LCD," Saw byte FROM RS232 ");
  reset(d0);delay(100);{For LCD-less people}
  set(d0);delay(100);
  reset(d0);delay(100);
  set(d0);delay(100);
  reset(d0);delay(100);
  set(d0);delay(100);
end;
When you run the program now, if you press a key on the PC running Hyperterminal (if you remembered to reconnect!), you should see "Saw byte from RS232 on the LCD, and some flashing on the LED.

What is this new word "RS232_New_Byte"?

The Pascalite II Plus has a 1 byte RS232 receive buffer. (As does the Piccolo. The Pro has a 32 byte buffer.) Because the RS232 channel is interrrupt driven, even if your program is off doing something else (even "repeat;until 4=5"!), if something is sent to the Pascalite via the RS232, it is held in the buffer. (If two or more "somethings" are sent, only the most recently sent something will be in the buffer.)

Why not just do "Read(RS232,bTmp)"? Because if you did, the program would sit at that line until something arrived; it would wait for something to be sent to the Pascalite.

Once you have the simple program, above, showing that everything is working, transform it into the following to do what we set out to do:
program Demo2RS232;
var bTmp,bD7,bFrmRS232:byte;

procedure RS232CRLF;
{Do Carriage Return / Line Feed (start new line}
begin
write(RS232,10);
write(RS232,13);
end;

procedure ReportState;
{Purists would object to my access
 of a global variable within this procedure}
begin
RS232CRLF;
if bD7=0 then write(RS232,"D7 is low"){no ; here}
       else write(RS232,"D7 is high");
RS232CRLF;
end; {procedure}

begin {main}
def_out(d0);
RS232CRLF;
repeat
set(d0);{turns LED on}
write(LCD,255); {clears LCD}
read(portd,bTmp);
bD7:=bTmp and 128;
if bD7=0 then begin
  write(LCD,"D7 is low");
  reset(d0);
  end {no ; here}
  else write(LCD,"D7 is high");
if RS232_New_Byte then begin
  read(RS232,bFrmRS232);
  bTmp:=0;{Will flag if s or S sent from RS232.
           0 means it was not}
  if bFrmRS232=83 then bTmp:=1;{Code for "S"}
  if bFrmRS232=115 then bTmp:=1;{Code for "s"}
  if bTmp=1 then ReportState;
  write(LCD," Saw byte from RS232 ");
  reset(d0);delay(100);{For LCD-less people}
  set(d0);delay(100);
  reset(d0);delay(100);
  set(d0);delay(100);
  reset(d0);delay(100);
  set(d0);delay(100);
  end;
delay(300);
until 4=5;
end.


That's all there is to it!

Of course, you're going to want to do something more interesting in the other computer than run Hyperterminal. If your other computer is running Windows... have fun! The Pascalite end of dumb, handshake-less RS-232 communication is pretty easy. For information of programming a Windows machine to talk with your Pascalite by something of your own creation in Delphi (as opposed to just using Hyperterminal) see my Delphi tutorial on the subject of serial communication.

Eventually I hope to produce is a program for the Windows computer which will periodically poll a Pascalite for some "pre-digested" weather data, and then add that data to a log file, and display the weather history. Wish me luck!
To search THIS site.... (Go to the site's above, and use their search buttons if you want to seach them.)... Way to search this site without using forms
Search this site or the web powered by FreeFind

Site search Web search

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to editor's (Arunet) homepage
How to email or write this page's editor, Tom Boyd