Google Answers Logo
View Question
 
Q: switch statement - GA ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: switch statement - GA
Category: Computers > Programming
Asked by: amy123456-ga
List Price: $150.00
Posted: 20 Jan 2004 06:33 PST
Expires: 19 Feb 2004 06:33 PST
Question ID: 298350
The room number that we extracted, i need it to be used in the switch
statement below to refenrence the array for the correct room #.



public void CcmcRoom()
	{
		int room;
		room = 601;
		//FromFile("c:\\lightsr\\test.txt") (room );

		switch  (room )  //("c:\\lightsr\\test.txt"))
		{
		
			case 601:
				Console.WriteLine("You selected: 601");
				// Perform action 601
				break;
			case 602:
				Console.WriteLine("You selected:602");
				// Perform action 602
				break;
			case 603:
				Console.WriteLine("You selected: 603");
				// Perform action 603
				break;
			case 604:
				Console.WriteLine("You selected: 604");
				// Perform action 604
				break;
			case 605:
				Console.WriteLine("You selected:605");
				// Perform action 605
				break;
			case 606:
				Console.WriteLine("You selected: 606");
				// Perform action 606
				break;
			case 607:
				Console.WriteLine("You selected: 607");
				// Perform action 607
				break;
			case 608:
				Console.WriteLine("You selected:608");
				// Perform action 608
				break;
			case 609:
				Console.WriteLine("You selected: 609");
				// Perform action 609
				break;
			case 610:
				Console.WriteLine("You selected: 610");
				// Perform action 610
				break;
			case 611:
				Console.WriteLine("You selected:611");
				// Perform action 611
				break;
			case 612:
				Console.WriteLine("You selected: 612");
				// Perform action 612
				break;
			case 613:
				Console.WriteLine("You selected: 613");
				// Perform action 613
				break;
			case 614:
				Console.WriteLine("You selected:614");
				// Perform action 614
				break;
			case 615:
				Console.WriteLine("You selected: 615");
				// Perform action 615
				break;
			case 616:
				Console.WriteLine("You selected: 616");
				// Perform action 616
				break;
			case 617:
				Console.WriteLine("You selected:617");
				// Perform action 617
				break;
			case 618:
				Console.WriteLine("You selected: 618");
				// Perform action 618
				break;
			case 619:
				Console.WriteLine("You selected: 619");
				// Perform action 619
				break;
			case 620:
				Console.WriteLine("You selected:620");
				// Perform action 620
				break;
			case 621:
				Console.WriteLine("You selected: 621");
				// Perform action 621
				break;
			case 622:
				Console.WriteLine("You selected:622");
				// Perform action 622
				break;
			case 623:
				Console.WriteLine("You selected: 623");
				// Perform action 623
				break;
			case 624:
				Console.WriteLine("You selected: 624");
				// Perform action 624
				break;
			case 625:
				Console.WriteLine("You selected:625");
				// Perform action 625
				break;
			case 626:
				Console.WriteLine("You selected: 626");
				// Perform action 626
				break;
			case 627:
				Console.WriteLine("You selected: 627");
				// Perform action 627
				break;
			case 628:
				Console.WriteLine("You selected: 628");
				// Perform action 628
				break;
			default:
				Console.WriteLine("Bad selection");
				break;
		}
	}
}

Request for Question Clarification by mathtalk-ga on 20 Jan 2004 07:33 PST
Hi, Amy:

Is the "room number" in this context a combination of the floor and
the room?  I ask because you seem to be working in a high range,
relative to the room numbering in the early problem (which ranged from
1 to 28 or so).

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 20 Jan 2004 07:57 PST
I will change that to 1- 28 and not 601 -628.

Form here i am going to send a command to the com port to turn the room 
light on.

Thank you

Clarification of Question by amy123456-ga on 20 Jan 2004 07:59 PST
case 601:
				Console.WriteLine("You selected: 601");
				// Perform action 601
				break;
			case 02:
				Console.WriteLine("You selected:602");
				// Perform action 602
				break;
			case 03:
				Console.WriteLine("You selected: 603");
				// Perform action 603
				break;
			case 04:
				Console.WriteLine("You selected: 604");
				// Perform action 604
				break;
			case 05:
				Console.WriteLine("You selected:605");
				// Perform action 605
				break;
			case 06:
				Console.WriteLine("You selected: 606");
				// Perform action 606
				break;
			case 07:
				Console.WriteLine("You selected: 607");
				// Perform action 607
				break;
			case 08:
				Console.WriteLine("You selected:608");
				// Perform action 608
				break;
			case 09:
				Console.WriteLine("You selected: 609");
				// Perform action 609
				break;
			case 10:
				Console.WriteLine("You selected: 610");
				// Perform action 610
				break;
			case 11:
				Console.WriteLine("You selected:611");
				// Perform action 611
				break;
			case 12:
				Console.WriteLine("You selected: 612");
				// Perform action 612
				break;
			case 13:
				Console.WriteLine("You selected: 613");
				// Perform action 613
				break;
			case 14:
				Console.WriteLine("You selected:614");
				// Perform action 614
				break;
			case 15:
				Console.WriteLine("You selected: 615");
				// Perform action 615
				break;
			case 16:
				Console.WriteLine("You selected: 616");
				// Perform action 616
				break;
			case 17:
				Console.WriteLine("You selected:617");
				// Perform action 617
				break;
			case 18:
				Console.WriteLine("You selected: 618");
				// Perform action 618
				break;
			case 19:
				Console.WriteLine("You selected: 619");
				// Perform action 619
				break;
			case 20:
				Console.WriteLine("You selected:620");
				// Perform action 620
				break;
			case 21:
				Console.WriteLine("You selected: 621");
				// Perform action 621
				break;
			case 22:
				Console.WriteLine("You selected:622");
				// Perform action 622
				break;
			case 23:
				Console.WriteLine("You selected: 623");
				// Perform action 623
				break;
			case 24:
				Console.WriteLine("You selected: 624");
				// Perform action 624
				break;
			case 25:
				Console.WriteLine("You selected:625");
				// Perform action 625
				break;
			case 26:
				Console.WriteLine("You selected: 626");
				// Perform action 626
				break;
			case 27:
				Console.WriteLine("You selected: 627");
				// Perform action 627
				break;
			case 28:
				Console.WriteLine("You selected: 628");
				// Perform action 628
				break;
			default:
				Console.WriteLine("Bad selection");
				break;
		}
	}
}

Request for Question Clarification by mathtalk-ga on 22 Jan 2004 21:31 PST
Hi, Amy:

I think there's an easy aspect of this "switch statement" question,
and that's how to get the room number passed in so that the
conditional cases can act on it.  But I'm not sure if you want to go
beyond this issue to the more difficult one of how to drive a serial
port from C#.

We should create a class method CcmcRoom which takes an integer "room"
as an argument.  Then, instead of having that local variable involved,
the switch statement can simply use the argument's value.

If we back up a bit from there, the question must arise at some point
as to how we can convert the string roomnumber extracted from the
printer file into an integer value.  Of course I can also answer that.

But what is tricky here is the serial port driver.  The existing .Net
Framework Class Library doesn't have any native support for serial
communications.  What you require, of course, is something much
simpler than a full blown serial communications library package,
because all you want to do (as I understand it) is to turn on (or
possibly off) one pin or another on the serial port (to signal the
hospital staff by illuminating an electric bulb).  Would you like for
me to go ahead and discuss ways to do this?  You may have already
addressed this "hardware" issue, and I don't want to sidetrack the
discussion unnecessarily.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 23 Jan 2004 03:28 PST
Good morning. Yes that is going to be an issue with the com port. some
are advising me to go to VB. But i decide to try C#. Do you think it
is do able in c#. Please help.   With regards to the switch, what ever
you think can work for me to get the room #.  You are right all i need
is to be able to get the room # and then send a command to the com
port to turn on light and then turn off light.  Thank you once again

Request for Question Clarification by mathtalk-ga on 23 Jan 2004 06:07 PST
Hi, Amy:

Please describe the hardware arrangements which exist for controlling
the "order" lights.

One can always use .Net wrappers for ActiveX/COM components, or one
can go to a "platform invoke" layer.  I actually did a bit of research
on the various alternatives for this in .Net with one of the other
Google Answers Researchers.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 23 Jan 2004 07:25 PST
Hi- I am sending you the hardware info  that we are using.
I am also sending the web site. 

http://WWW.CCIelectronics.com


Thank you. 

World 1 is a general I/O device controlled by your PC. The user should
have some knowledge of a programming language such as C, VB or simply
Basic available in most DOS machines. To gain the maximum benefit, the
user should also have some knowledge of general electronics. Although
World 1 is a useful and practical I/O tool, our hope is that you will
enjoy using it.

All that is required to use World 1 is contained here. You'll also
find valuable information, accessories and tips for using World 1 on
the WEB at http://WWW.CCIelectronics.com. Please visit our site often
as we are continually updating user information. Please E-mail us with
your questions and comments regarding World 1.

World 1 was designed as a general I/O device for internal use at CCI
Electronics production facility. It was designed to connect a PC to
the outside world simply and easily. It's unique design concept allows
portability from one host to another by simply connecting to the
hosts' comm port. World 1 finds its' place in applications such as
education, engineering, robotics, remote monitoring, project
development and diagnostics; virtually any application requiring I/O.
World 1 will operate from any host computer with an RS232 port capable
of 19,200 baud. Most of today's computers are equipped with a comm
port suitable for World 1.

World 1 is controlled by sending to it simple commands. Commands are
in the form of 8 bit words. Each "command word" causes a function to
be performed, either read a port, write to a port, or configure a
port.

Device capability:
31 digital I/O lines.
8 analog channels.

Drive capability:
Sink/Source = 25ma/25ma, port A, 4 = open drain.
Analog: 8 analog channels 0-5 volt input. The reference is connected
to the internal 5 volt regulator.

I/O port configuration:
Port a 6 digital I/O lines or 5 analog channels.
Port b 8 digital I/O lines.
Port c 6 digital I/O lines.
Port d 8 digital I/O lines.
port e 3 digital I/O lines or 3 analog channels.

World 1 communicates with the host computer using an RS232 serial
port. The serial port may be any port capable of 19,200 baud, no
parity, 8 data bits, 1 stop bit.Included with this package is a six
foot cable with standard 9 pin connectors on each end. This is not a
null modem cable. Connections are straight through. World 1 does not
use handshaking when communicating with the host. Three pins are used:
DB9 Connector Pin	World 1	PC 
2	tx	rx
3	rx	tx
5	signal gnd 	signal gnd
		

Any programming language capable of reading and writing to a serial
port may be used to control World 1. World 1 does not use handshaking
signals to communicate. Communicating with World 1 is a simple matter
of writing to and reading from the hosts' comm port. Host interrupt
routines are not needed as long as your source code actively looks for
words received from World 1. World 1 does not speak unless asked to.
Therefore the programmer may issue a read command and retrieve the
response at a later time. Programming examples in this manual do not
use any particular programming language. They are for demonstration
purposes only. The words 'comm port' and 'serial port' are used
interchangeably in this manual.

Digital lines can be configured as inputs, as outputs or any
combination of inputs and outputs. Commands for each port begin with
the letter of that port.

Write commands begin with the port name and end with a '1'.
Digital I/O direction commands begin with the port name and end with a '0'.
Digital read commands begin with the port name and end with a '2'.

Example: The command 'a1'(hex) is a write to port a.

Special commands use fa-fe(hex). 

The serial port selected must be set up for 19,200 baud, no parity, 8
data bits, 1 stop bit. Serial port setup can be accomplished using DOS
'mode command' or may be included in your source code.

Comm port setup from DOS 
DOS mode command: <mode comm 2: 19,n,8,1>

If you like to program in VB, the following information is not
necessary as the use of controls takes care of all of this for you.
However, it is interesting to know what is going on with the serial
port.

Comm port setup from source code. 
These numbers are selected using a host with a serial port clock
frequency of 1.8432 MHz (standard). The following examples are for
configuring comm port 2. Although the host's RS232 port may require
different setup parameters, most PC's with a standard RS232 port will
configure correctly using the following method. Refer to the hosts
operating manual on how to configure the comm port.

Step 1:
out(0x2fb,0x80) // Select baud rate divisor latch, <bit 7 set.>

Setting bit 7 of the Data Format Register causes the receiver/
transmitter address of the serial port in use to become the Baud Rate
Divisor Latch Register. Set bit 7 and store the baud rate code in the
Receiver/ transmitter address as shown below for comm port 2.


Step 2: 
out(0x2f8,0x06) // Set baud rate of 19,200 . 
The address 2f8 is the base address for comm port 2.
06 hex is stored at address 2f8. With bit 7 of the Data Format
Register set, this address, 2f8 hex, becomes the Baud Rate Divisor
Latch Register as previously mentioned.

Step 3:
out(0x2fb,0x03) // Set 8 data bits, 1 stop bit, no parity. 
The address 2fb is the base address + 3.
Write 03 hex to the Data Format Register. Bit 7 is cleared and the
word format is set at : no parity, 8 data bits, 1 stop bit .

out(0x2f9,0x00) // Set interrupts off. (optional)

In the above example, the first word is the address of the desired
register. The second 'data word' is the bit pattern to be written to
that register. The 'data word' for other comm. ports is the same, only
the register address will change. Register addresses are listed in
back of this manual for your convenience.

The above is all that is needed to set up the host serial port.
For further information on the host's serial port, reference the 8250 UART.

Writing a Data Word to a Port 
World 1 does not return a word when a write command is issued. Writing
to an I/O port requires two words to be sent to World 1. The first
word tells World 1 which I/O port to write to. The second word is the
data to be written to the appropriate I/O port. After a write command
is issued, World 1 will wait for the next word to be received and then
place it in the output buffer of the appropriate port. A write command
will not effect a port configured as an input.

When sending a word to the host's comm port, be sure to check that the
host's transmitter buffer is clear to avoid overwriting the previous
word. This condition is called transmitter overrun and must be
avoided. Serial port status may be tested by reading a register
associated with the serial port in use to prevent transmitter overrun.
Again, if you're programming in VB, the above is taken care of for
you.
When programming a port for digital output, caution should be taken
not to connect the outputs of World 1 to outputs of your application.
To do so may cause damage to World 1 and the devices connected to it.

Reading a Data Word from a Port 
Reading a port requires two steps; sending the appropriate read
command and reading the data returned from World 1. World 1 will
return one 8 bit word for each 'read command' sent. After a read
command is sent, World 1 transmits the word read from the I/O port
requested. The host must wait for the word to be assembled before
reading the host's comm port. For each read command sent to World 1, a
subsequent host read must follow to clear the word in the host's
receiver buffer. Otherwise the word in the host's Uart receiver buffer
will be overwritten by the next read command. This condition is called
receiver overrun and must be avoided.

Any port may be read at any time. Data lines set as outputs may be
read as validation of the individual output states. Data lines
programmed as outputs and held by an external device can be detected
by reading the I/O port and comparing the received word with the
desired output data.
Digital I/O direction commands 
Command (hex)
Setup Ports	I/O direction 0-ff (hex)
1 = input. 0 = output	Port effected
a0	Direction word	A
b0	Direction word	B
c0	Direction word	C
d0	Direction word	D
e0	Direction word	E


All I/O lines may be configured as either digital input or as digital
output. Reset and power up condition = digital input (default). Setup
commands may be issued at any time and begin with the letter of the
port requested and end with a zero. The ability to change the I/O
direction 'on the fly ' allows World 1 to interact with a data bus in
a microprocessor environment much the same way as the processor would,
although much slower.

Example: write_port (0xb0, 0x0f) .

The 'b0' is the configure command. It tells World 1 to accept the next
word as I/O direction for each bit of Port B. The second word is the
I/O direction of Port B. 1 = input - 0 = output. In the example above
Port B will be configured as bits 0-3 = input, bits 4-7 as outputs.
(bin = 00001111)


Analog Input Channels
A total of eight channels may be configured as analog input channels.
Port 'A' bits 0-3, 5 and Port 'E ' bits 0-2. Reading an analog channel
returns analog data from a 'special register' in World 1, not the port
assigned to the channel. When reading a port register, all pins
configured as analog input channels will read as cleared (0). Pins
configured as digital inputs will convert an analog input. All analog
channels are referenced to the internal 5 volt regulator. Accuracy may
vary slightly with temperature. To calibrate analog data through
software, make a voltage measurement and divide it by the 8 bit word
read. (Voltage read / number read in dec. = mv per count (max. 255
counts) Most of the units will calibrate to about 19.63 mv per count
so that a number of 123 received from World 1 = about 2.41 volts. (123
* .01963 = 2.41) Once you have the calibration number you'll be able
to convert received 8 bit words from World 1 into voltage by simple
arithmetic.

Setup Commands for Analog modes 
Command (hex)	Port 'A' bits 0-3,5	Port 'E' bits 0-2
fa	Analog	Digital
fb	Digital	Digital
fc	Analog	Analog



Special commands 
Command (hex)	Function	Number of words 
fd	Reset all ports 
Default = digital inputs	Send one
fe	Comm. test 
Receive aa (hex)	Send one 
Receive one


It's a good idea to reset all ports to their default state after a
function or routine has completed. The command fe(hex) can be used to
test communications from the host to World 1. Issue a command fe(hex)
and World 1 will respond with aa(hex).
Total command set for World 1 
Command (hex)	Number of words.	Function	Port effected
a0	Send two	I/O Direction	A
a1	Send two	Write port	A
a2	Send one
Receive one	Read port	A
a3	Send one
Receive one	Read analog
channel 1	A
a4	Send one
Receive one	Read analog 
channel 2	A
a5	Send one
Receive one	Read analog 
channel 3	A
a6	Send one
Receive one	Read analog
channel 4	A
a7 	Send one
Receive one	Read analog 
channel 5	A
b0 	Send two	I/O Direction	B
b1	Send two	Write port	B
b2	Send one
Receive one	Read Port	B
c0	Send two	I/O direction	C
c1	Send two	Write port	C
c2	Send one
Receive one	Read port	C

Total command set (continued)
Command (hex)	Number of words.	Function	Port effected
d0	Send two	I/O Direction	D
d1	Send two	Write port	D
d2	Send one
Receive one	Read port	D
e0	Send two	I/O direction	E
e1	Send two	Write port	E
e2	Send one
Receive one	Read port	E
e3	Send one
Receive one	Read analog
channel 6	E
e4 	Send one
Receive one	Read analog 
channel 7	E
e5 	Send one
Receive one	Read analog 
channel 8	E
fa	Send one	Setup
Port A analog
Port E digital	A,E
fb	Send one	Setup
Port A digital
Port E digital	A,E
fc	Send one	Setup
Port A analog
Port E analog	A,E
fd	Send one	Reset all to ports digital inputs.	A,B,C,D,E 
fe	Send one
Receive one	comm test
aa = ok	none

Request for Question Clarification by mathtalk-ga on 24 Jan 2004 12:41 PST
Hi, Amy:

Just to help me form a little better picture, do you have one of these
"World I" boxes attached to your development machine, in addition to a
separate one which is intended (or perhaps installed) in "production"?

I often find that in a "systems integration" exercise of this kind it
is important to have a separate development environment; after the
development part of the software lifecycle is over, the extra hardware
is often useful as a "backup" device.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 24 Jan 2004 16:10 PST
Yes i do HAVE THE WORLD II, and it is connected and working. i created 
a box with the 28 rooms and lights all wired and connected to the world II.
world II has a test environment. All i need is the command to be send
to the com port, and the lights should turn on.

Thank you once again.


Just to help me form a little better picture, do you have one of these
"World I" boxes attached to your development machine, in addition to a
separate one which is intended (or perhaps installed) in "production"?

I often find that in a "systems integration" exercise of this kind it
is important to have a separate development environment; after the
development part of the software lifecycle is over, the extra hardware
is often useful as a "backup" device.

Request for Question Clarification by mathtalk-ga on 24 Jan 2004 17:51 PST
Okay, great!  I'll assume that we want to build the application to use
only World 1 functionality, even though you will be developing and
testing with the World 2 device.  I notice that the difference seems
to be the World 2 device can be requested to send "unsolicited"
messages, which would require (perhaps) an interrupt driven
communications library on the PC side.  It'll be simplest if we avoid
such a requirement.

--mathtalk-ga

Clarification of Question by amy123456-ga on 24 Jan 2004 20:31 PST
Hi mathtalk. That is correct.

Thank you

Request for Question Clarification by mathtalk-ga on 26 Jan 2004 11:04 PST
Hi, Amy:

I will go ahead and post some code that addresses the original
question here (about using the room number in a switch or any other
flow control mechanism that you might want).  However let's begin to
discuss the business logic of the light switches a bit.

I understand that as a message (Order Session file) comes in, a
specific light should be turned on.  But what if the light is already
on?  In fact, what are the circumstances by which a light gets turned
off?

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 26 Jan 2004 11:35 PST
Hi. To answer question one. 

1-I understand that as a message (Order Session file) comes in, a
specific light should be turned on.  But what if the light is already
on? It will not effect the process, because the intent is to let staff
know that this patient has active orders.

2- To turn the lights off the staff would go to a separate C# windows
application that would promtp them for a password, and the order for
that patient would show up. ( The orders that we saved to log would
now show up ). once they view the order they would have to acknowlodge
the order and a command to the com port would be sent to shut the
lights off .
 
Thank you

Request for Question Clarification by mathtalk-ga on 27 Jan 2004 08:07 PST
Hi, Amy:

Given that at least two applications are involved, one that we are
coding now that turns on the lights and "a separate C# windows
application" that turns off the lights when staff log in a response,
we need to have a way for the World 1/2 serial port to be shared by
both applications.

The normal Windows management of a serial port treats this device as a
file which is opened exclusively by an application, and therefore a
serial port cannot be simultaneously shared between two applications
at a "low level".

What one might do is create a service in C# that "owns" the serial
port and receives high level Windows custom messages from the other
applications.  This service would "mediate" the serial port
transmissions (and could also respond to Windows messages inquiring
about the World "state").

Before we get too distracted by possible solutions, can you advise me
what operating system is being used on the PC that the World 2 box is
attached to?  To develop a Windows service that machine should be
running an NT-class operating system, ie. Win2K or XP.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 27 Jan 2004 09:21 PST
win 2000

Clarification of Question by amy123456-ga on 27 Jan 2004 09:24 PST
Hi. If a separate application will cause a problem with the port,
should we just think on using the same application to also turn off
the lights.

PS. I will make sure that your time in this project is compensated. 

Thank you very much

Clarification of Question by amy123456-ga on 30 Jan 2004 04:25 PST
Good morning. How are things going? Thank you very much for the effort
that you are putting into this project.

Thank you

Request for Question Clarification by mathtalk-ga on 30 Jan 2004 07:36 PST
Good morning, Amy:

Thanks, I'm looking forward to the weekend and being able to do some
.Net development for you.  Remembering how well you ran past me with
the code I gave on an earlier question, I think I should have taken
that approach and posted some draft code earlier this week for you to
look at.  However the serial port stuff in .Net is still relatively
untamed, and I feared I'd be sending you off on too much of a chase.

However I'll be coding mode tonight and post some of these pieces for you then.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 30 Jan 2004 08:16 PST
Thank you very much

Clarification of Question by amy123456-ga on 01 Feb 2004 07:53 PST
Good morning. Thank you for the information, i went to the site and
found it quite interesting. it seems that com port communiction in the
.NET world is not very simple like VB6.

PS - What ever you find that can work is ok with me.

Thank you once again for your help and efforts

Request for Question Clarification by mathtalk-ga on 01 Feb 2004 10:08 PST
Hi, Amy:

My suggestion is to get the first application running, managing the
turning on of lights, with the ultimate intention of having this same
program act as the intermediary for other programs where users log in
that they've fulfilled orders (and implicitly that the lights should
correspondingly be turned off).

The "log" of extracted text should probably become a database at that
point, also, which would allow for reports to be created based on the
posting and fulfilling of orders.

My current task is convert the World 1/2 documentation into a
convenient class for managing the lights.  Here's a question for you. 
In managing the lights, how much allowance do we need to make for
multiple locations?  In other words, if one instance of the
application will manage the lights for all locations, then it makes
sense to have a mapping based on room number + location.

On the other hand if there will be a separate instance for each
location, then we would only need a mapping based on room number, but
we would also need to initialize application instances with a notion
of which location they are responsible for, so that they (in effect)
ignore the orders for all but that location.

This last is probably trickier (reading a file and putting it "back")
than the first, but I'd guess the choice of one instance vs. several
is dictated by business logic outside the design of the application
itself.

Of course the simplest case (only one location to be responsible for)
can be used for an initial implementation; we just don't want to paint
ourselves into a corner.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 01 Feb 2004 10:57 PST
Hi. Your first question.
1-how much allowance do we need to make for
multiple locations?  none  - we are going to have each floor have
there own application running. when a physicians enters an order into
SCM, he can be any where in the hospital, but the order will only
print and show up in the unit. where the patient is. all the units are
separate.

Clarification of Question by amy123456-ga on 02 Feb 2004 18:40 PST
Hi. I am under some pressure at work because of this project, i have
been asked to show something on the project weather it is a viable
solution or not.
I think it is. Do you think we can have something soon?

I do appreciate your help.  thank you very much

Request for Question Clarification by mathtalk-ga on 02 Feb 2004 19:02 PST
Hi, Amy:

I'm getting an inferiority complex here because when you raise your
offered price, it makes me try to think what more I can do add value
to my Answer, and then... Let's go back on the dollars, so I can feel
okay about posting an Answer that corresponds to the fairly narrow
"dispatching" function.  I realize I opened up a lot of issues, but
what I plan to post is the "switch box" code to map from room numbers
to the World 1/2 port commands.  It's not the whole application, but
it's a nice testable chunk, and I don't think we're painting ourselves
into a corner at this point.

Okay? 

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 02 Feb 2004 19:27 PST
Your a person of honor and great character.

Thank you once again.

Clarification of Question by amy123456-ga on 06 Feb 2004 10:59 PST
Hi. I am just checking in to see if you are still breathing, Do i need
to be worried or concerned?  I do not mean to be a pest, really.

Thank you

Request for Question Clarification by mathtalk-ga on 06 Feb 2004 12:34 PST
Hi, Amy:

Feel free to pester away, it only adds to my delusions of importance!

I've been working from the World 1 documentation to write an interface
class that provides your application with a clearly "intended" set of
methods.

One basic method is to initialize the World 1 box, and seamlessly the
serial port from which we control it.  The baud rate and parity
settings on the serial port itself are pretty standard stuff, but I
not sure how much "state" we should assume the World 1 box itself will
hold (between invocations of the application).  As a first assumption
I would reset all the lights to "off" on the World 1 box as part of
the initialization.  If we need to hold state information, this would
probably be easier to do on the PC side.

One quick clarification I need from you is the mapping of room numbers
to ports.  Based on the numbers, I assume you plan on using the
digital lines (as there aren't enough analog channels to go around). 
I'm proceeding on the basis of using all the digital lines numbered
consecutively on the ports:
Port:
  a : 1-6    (six lines)
  b : 7-14   (eight)
  c : 15-20  (six)
  d : 21-28  (eight)
  e : 29-31  (three)

We would apparently want to configure these lines as outputs, but that
doesn't prevent us from reading them (ie. to confirm their states). 
The power-on default is for digital lines to be inputs.

At the serial port level, the lines that make up a "logical" port are
addressed by individual bits within a command argument byte.  Thus,
when setting the I/O direction for one of the digital lines on port b,
we are actually setting all eight of these.

Given this complexity it will be nice to hide all those details behind
an interface class which allows us the luxury of addressing each line
by number.  This is I think the essence of the "switch statement"
task, although I'm very interested to hear from you about concerns and
suggestions.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 06 Feb 2004 13:58 PST
Hi. we can not use port 5 on the world1, i have wired as such.

The Mapping to rooms - Port:
  a : 1-6    (six lines)  
  b : 7-14   (eight)
  c : 15-20  (six)
  d : 21-28  (eight)
  e : 29-31  (three)


room #         A:
1               1
2               2
3               3
4               4
5               6

Room            B
6               7
7               8
8               9
9               10
10              11
11              12
12              13
13              14

room            C
14              15
15              16
16              17
17              18
18              19
19              20

room            D
20              21
21              22
22              23
23              24
24              25
25              26
26              27
27              28

room            E
28              29

Thank you very much

Clarification of Question by amy123456-ga on 10 Feb 2004 09:42 PST
Hi. Do you have anything on the switch procedure that i can try?

Thank you

Request for Question Clarification by mathtalk-ga on 10 Feb 2004 09:49 PST
Hi, Amy:

I'll give you my stub class tonight.  Are you using a "namespace" to
qualify your classes?  Microsoft more or less recommends that
developers create a "custom" namespace for application classes, to
avoid name collisions.  I was thinking about calling the class
SwitchBox or LightBoard, but prefacing it with a "corporate" namespace
like "Hospital" for uniqueness.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 10 Feb 2004 10:07 PST
SwitchBox Is a good idea.   

prefacing it with a "corporate" namespace
like "Hospital" for uniqueness. I salso a good idea.

Thank you.

By the way, have you ever heard of sax communication. Take a look at it
www.sax.net

Request for Question Clarification by mathtalk-ga on 11 Feb 2004 05:51 PST
Sorry, Amy, I didn't get off from work until 10pm last night, which
meant I was too late for my Starbucks pick me up!  I'll leave work
around 5pm today and get you the promised code.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 11 Feb 2004 19:59 PST
Starbucks pick me up is a good idea. Thank you once again for your efforts

Clarification of Question by amy123456-ga on 15 Feb 2004 13:07 PST
Hi. What do you think of the www.sax.net ?
Is it something that can help with the comp port?

Request for Question Clarification by mathtalk-ga on 16 Feb 2004 08:25 PST
Hi, Amy:

It definitely has to do with the (serial) communications port.  Their
site offers additional products (and services), but the one most
directly related to the application you are building is called Sax.net
Communications (for Windows Forms).  I've downloaded the free trial
version.  The price is $499 for a "single developer" license, $999 for
a "team developer".

My impression is this is a little steep.  Microsoft will eventually
have to support a "canonical" serial port solution, as they already do
in the Compact Edition (CE) version of the .Net Framework.  Having
vendor support is a significant "risk mitigation" in a project, so
buying a component is a reasonable option.  You will need to assess
the benefits vs. budget for your project.  Like the John Hind project
discussed earlier, the Sax.net component uses managed code in C# to
make calls into the Windows API.

I can do a more in-depth discussion after I've looked at the trial download.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 16 Feb 2004 11:48 PST
Hi. What are your thought. Can it be done without it.
What are you thinking of using as a toll to communicate to the comp port. 

PS. Do you think this project can work?  Ple
Thank you

Clarification of Question by amy123456-ga on 16 Feb 2004 12:21 PST
Hi. Do i need to worry that maybe this can not be done?

Do you by any chance have the switch part working?, If so can 
i try it? Please

Thank you very much

Request for Question Clarification by mathtalk-ga on 17 Feb 2004 11:00 PST
Hi, Amy:

I'll definitely get the Switchbox code working for you tonight,
stubbing out the serial port calls with the sort of writeLine commands
we used earlier to test the parsing routine.

I have the map built, I just need to integrate it with the earlier
project for the purpose of testing.  I'll post as an Answer, and
you'll have 30 days to evaluate it (and ask for Clarifications) to
determine if it's suitable for your needs.

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 17 Feb 2004 13:07 PST
Hi. Thank you very much
Answer  
Subject: Re: switch statement - GA
Answered By: mathtalk-ga on 18 Feb 2004 06:01 PST
Rated:5 out of 5 stars
 
Hi, Amy:

I created a new C# source file orders.cs in the project we did
earlier, contents are given below.  As we discussed, there is a
namespace Hospitals.Orders defined in this assembly, and the class
Switchbox with its data members and methods is a part of that.

To call this new class for testing purposes, I added a couple of lines
to the textextract.cs file from the earlier project.  One is the
"initialization" code for making the World 1/2 box configured to write
digital lines as needed.  We should clear up how line 5 is supposed to
be managed; for the present I've tried to treat it as read digital.

I put this line in the Main method of the TxtXtrct class, just ahead
of the call to TxtXtrct.ListItems:

        Hospital.Orders.Switchbox.MyInit();

The exact location is not critical, so long as it is called before then.

Then, inside the ListItems method I inserted this line right after the
room number is parsed out:

        Hospital.Orders.Switchbox.Lampset( int.Parse(roomnumber), true);

This uses the method for converting the string roomnumber to an
integer that we discussed earlier.

The MyInit and Lampset methods of class Switchbox are currently built
on "stubs" for the serial port communication called:

SerialWrite( byte c)

SerialWrite( byte c, byte i )

in the same class Switchbox.  It is these which we would proceed to
replace with code for the actual serial port communications. 
Currently these stubs write to the Console window, similar to how we
treated output in the earlier project.

Testing so far has been limited to running the program against the
original test file, which "turns on the lamp" for room 11.

Let me know your questions once you've had a chance to compile and
look over the new code.

regards, mathtalk-ga

* * * * * * * < orders.cs > * * * * * * * * *
using System;
using System.IO;

namespace Hospital.Orders 
{
  public class Switchbox 
  {
    private static int[]  port = new int[31];
    private static byte[] line = new byte[31];
    private static byte[] pimg = new byte[5];
    private static byte[] pcmd = new byte[5];

    static Switchbox() 
    { 
      // port A and five lines
      port[0] = 0; line[0] = 1;
      port[1] = 0; line[1] = 2;
      port[2] = 0; line[2] = 4;
      port[3] = 0; line[3] = 8;
      port[4] = 0; line[4] = 32;

      // port B and eight lines
      port[5] = 1; line[5] = 1;
      port[6] = 1; line[6] = 2;
      port[7] = 1; line[7] = 4;
      port[8] = 1; line[8] = 8;
      port[9] = 1; line[9] = 16;
      port[10] = 1; line[10] = 32;
      port[11] = 1; line[11] = 64;
      port[12] = 1; line[12] = 128;

      // port C and six lines
      port[13] = 2; line[13] = 1;
      port[14] = 2; line[14] = 2;
      port[15] = 2; line[15] = 4;
      port[16] = 2; line[16] = 8;
      port[17] = 2; line[17] = 16;
      port[18] = 2; line[18] = 32;

      // port D and eight lines
      port[19] = 3; line[19] = 1;
      port[20] = 3; line[20] = 2;
      port[21] = 3; line[21] = 4;
      port[22] = 3; line[22] = 8;
      port[23] = 3; line[23] = 16;
      port[24] = 3; line[24] = 32;
      port[25] = 3; line[25] = 64;
      port[26] = 3; line[26] = 128;

      // port E and one line
      port[27] = 4; line[27] = 1;

      // set image bits to zero
      for (int i = 0; i < 5; ++i)
        pimg[i] = 0;

      // set commands to write ports
      for (int i = 0; i < 5; ++i)
        pcmd[i] = Convert.ToByte(16 * (i + 10) + 1);

    }

    public static void MyInit()
    {
      // set all ports & lines to write digital except 5,28,29
      SerialWrite( Convert.ToByte( 0xfb ) );
      SerialWrite( (byte) 0xa0, (byte) 0x2f );
      SerialWrite( (byte) 0xb0, (byte) 0xff );
      SerialWrite( (byte) 0xc0, (byte) 0x3f );
      SerialWrite( (byte) 0xd0, (byte) 0xff );
      SerialWrite( (byte) 0xe0, (byte) 0x01 );
    }

    public static void Lampset(int rn, bool ls)
    {
      // turn lamp for room # rn on or off
      int p = port[rn - 1];
      byte b = line[rn - 1];

      // set respective bit of port image
      pimg[p] |= b;

      // if not ls, then unset that bit
      if (!ls)
        pimg[p] ^= b;

      // write the affected port's image
      SerialWrite(pcmd[p],pimg[p]);
    }

    static void SerialWrite(byte c) 
    {
      // write byte to serial comm port
      Console.WriteLine( "Command: {0}", Convert.ToInt16(c) );

    }

    static void SerialWrite(byte c, byte i) 
    {
      // write byte to serial comm port
      Console.WriteLine( "Port Cmd: {0} Bit Image: {1}",
                         Convert.ToInt16(c),
                         Convert.ToInt16(i)
                       );

    }
  }
}

* * * * * * * * < end of orders.cs > * * * * * * * * * *

regards, mathtalk-ga

Request for Answer Clarification by amy123456-ga on 18 Feb 2004 09:07 PST
Hi. Thank you once again. I apoligize for being a pain.

Would it be possible for you to put the codes together as one projects, Please.

I am lost to where this part fits in. I am very sorry for any problems
or headaches i may of caused you.  sorry.....

Request for Answer Clarification by amy123456-ga on 18 Feb 2004 10:03 PST
Hi Again. I got it to work. I would like to test sending a command to
the comport to test a light going on.

Thank you very much

Request for Answer Clarification by amy123456-ga on 18 Feb 2004 10:08 PST
When you make reference to STUBS what do you mean.

Request for Answer Clarification by amy123456-ga on 18 Feb 2004 11:02 PST
Hi. the code work nicely.

Thank you

Clarification of Answer by mathtalk-ga on 18 Feb 2004 11:41 PST
Hi, Amy:

The use of the word "stubs" is meant to convey that the method calls
that would actually interact with the serial port still need to be
"fleshed out".

Instead of sending bytes to the serial port, the information is
displayed on the Console (screen).

My thought for filling in this functionality is to use a component
(class) which is already written, and specifically to revise the
Switchbox class to inherit from that serial communications class.

If you are seriously thinking about purchasing the Sax.net serial
communications component for this purpose, I will prioritize
investigating that angle.  Otherwise my first thrust would be to take
the John Hind source code and rework it into the extra class that we
would place in the Hospital.Orders namespace.

All the data members and methods in class Switchbox are static,
meaning that no instance of Switchbox ever needs to be created.  I'm
not "wedded" to that design decision; it was just the simplest way to
get started.  With a little tweaking the display of bytes could be
changed to hexidecimal format, and that might make verification of all
the "codes" needed for the World 1 interface go faster.  But basically
with our source code + the John Hind source code, I think we have the
freedom to make things work smoothly together (aka the freedom to
shoot yourself in the foot!).

regards, mathtalk-ga

Clarification of Answer by mathtalk-ga on 18 Feb 2004 11:52 PST
One other quick note, just so I don't lose this thought in the rush to
do new things, we should verify the range of the room number submitted
someplace along this chain of control.  It would probably be most
logical to test parameter rn within Switchbox.Lampset.  Perhaps the
best technique would tie the test to the "mapping" aspect of
Switchbox.

regards, mathtalk-ga

Request for Answer Clarification by amy123456-ga on 18 Feb 2004 12:47 PST
Hi. No I am not tied to Sax.net serial communications component, we
can do as you suggested John Hind source code and rework it into the
extra class that we
would place in the Hospital.Orders namespace.

PS - Here i go again, let me apologize right of the bat. (Sorry) Do
you have  an aproximate time line for John Hind source code to work
with our code.

you do not know how helpful you have been!!!!  Thank you

Thank you very much

Clarification of Answer by mathtalk-ga on 18 Feb 2004 20:28 PST
As an initial plan I'd say 4 to 7 days.  We've reduced the serial port
requirements to a bare outline, involving no "interrupt handlers" or
event driven logic.  We need to be able to "open" a serial port
(setting the baud rate and parity to match the requirements of the
World 1 device, nothing fancy there) and to able to write bytes to it.

I've seen other .Net compatible communications libraries priced
similarly to the Sax.net component ($499), but I also see other
software packages that seem to do the same thing for $29 (per
application, rather than per developer).

Last August another Google Answers Researcher, seizer-ga, and I wrote
up a two pager on Serial Port Communications and the .Net
Architecture, which I later expanded into a 90 minute PowerPoint
presentation for a .Net developers meeting.  So I really think we can
do the serial port interfacing without buying anything. You may need
Administrator rights on the platform where we install the software,
but that's not usually a huge obstacle.

regards, mathtalk-ga

Request for Answer Clarification by amy123456-ga on 19 Feb 2004 03:43 PST
Hi. The PC's will have full administrative rights.

Thank you

Clarification of Answer by mathtalk-ga on 19 Feb 2004 05:34 PST
Great.  I suggest that you continue to use this thread for issues
relating to the mapping/switchbox function, and open a new Question
for development of the serial port base class in the context of your
project.  My guess is that discussion will be fairly self-contained
because of the limited serial port functionality identified by this
part of the project.

I also recommend that you try your hand at reworking the
(console-based) user-interface to allow testing the mapping functions
by entering a room number at the keyboard (and then seeing the World 1
commands displayed).  Such a test harness will be useful in testing
the hardware connections as well once we get the serial port interface
in place.

regards, mathtalk-ga

Request for Answer Clarification by amy123456-ga on 19 Feb 2004 11:46 PST
Hi. I have fully tested the world1 with the lights, and all works very
well, and it matches the mapping.

I will open another question, but are you still going to continue with the project.

Thank you

Clarification of Answer by mathtalk-ga on 19 Feb 2004 12:57 PST
Hi, Amy:

Absolutely, I'd be thrilled to continue working with you.  I may not
have explained well before how you can request that a Question be
addressed by a particular researcher (not that you are obliged to do
this!).

If you add to the Subject line of a Question a qualifier like 

"(for so_and_so-ga)"

then all of us will know that you'd prefer to have the Question
addressed by so_and_so-ga.  In the event that a researcher feels he or
she cannot provide a good Answer in a timely way when a particular
request is directed to them, then they quickly try to leave a
Clarification or Comment to that effect.  The Customer can decide to
close the Question or open it (by way of Clarification) to researcher
community as a whole.

In most cases Customers will get the best results by inviting a
response from any researcher able to provide a good Answer, but
obviously in a case like this where consistency and background
information are important, the option to request a particular
researcher makes sense.

thanks, mathtalk-ga

Clarification of Answer by mathtalk-ga on 19 Feb 2004 13:01 PST
One last point to mention... keep in mind that the code initializes
"line 5" as read digital.  I didn't quite know the purpose of
reserving this line from the pool of lamp configurations, but that
seemed more or less the safest choice.

I don't think there's any way to avoid configuring that line one way
or the other (read or write) since the "direction" commands work on a
port by port basis.

regards, mathtalk-ga
amy123456-ga rated this answer:5 out of 5 stars
I have nothing but excellent Recommendations.

Comments  
Subject: Re: switch statement - GA
From: mathtalk-ga on 31 Jan 2004 12:53 PST
 
Hi, Amy:

An easy piece of the puzzle is how to convert a string to an integer.

In the .Net Framework Class Library this would probably be best
handled by the Parse method of the Integer Class:

int n = int.Parse(s)

where string s is a valid representation of an integer.

Then, if we make your function CcmcRoom a method of our "main" class,
we can add the integer n as an argument, calling it from within the
method where the input text files have the room numbers extracted.

Since there are several locations, even if you plan to limit the use
of these lights to one location, we need to check that data item as
well.

To develop the serial port controls we will need to rely on something
outside of the .Net FCL, either using a wrapper for a COM serial port
component or using the Platform/Invoke interface to call on the Win32
serial port support.

The last is perhaps more of a "pure C#" solution, and a working
program with discussion is found here, in an article by John Hind:

[Serial Comm: Use P/Invoke... for Serial Devices]
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/

I'm heading down this path because it has the prospect of minimal
software dependence.  However we may run into issues with permissions,
etc. running under Win2K.  I don't think it will be a major headache.

regards, mathtalk-ga
Subject: Re: switch statement - GA
From: mathtalk-ga on 12 Feb 2004 06:37 PST
 
The MS recommended practice is to build a two part namespace into
applications, the first part to distinguish your "company"
applications from other companies, and the second to distinguish this
"internal" application or development project from another.

We're going to focus on the code for a class Switchbox that interfaces
between the hospital floor orders management application and the
hardware (serial port and World 1 combination):

namespace Hospital.Orders {
    public class Switchbox {
    // full type name is Hospital.Orders.Switchbox
    }
}

A couple of things will need to be initialized, either by the class
(type) itself or by an "instance" of the class.  These requirements
can be probably be handled equally well in either fashion because we
only anticipate having one instance of the class per running program.

The serial port has to be initialized, and the World 1 box has to be initialized.

We'll assume the details of the serial port intialization can be
deferred to a later point, probably by the component we select to
directly interact with the serial port.  But the initialization of the
World 1 box has implications for how we will interact with it
throughout the running program's lifetime.

In C# the methods associated with the class itself are called
"static", in distinction from methods associated with the instances of
the class.  I'm going to proceed on the lines of having an instance of
the class, even though we only foresee having one, or perhaps one at a
time.

As we discussed previously the digital lines in the World 1
configuration are grouped as "bit positions" within ports labelled
A,B,C,D,E.  The number of digital lines varies somewhat from port to
port, and you have "reserved" one of these lines for another purpose,
but in essence the Switchbox class will need to manage the I/O
direction and settings of all of them.

Such at any rate is my reading of the CCI Electronics documentation. 
Assuming we wanted to set the direction of the third digital line on
port A to "out" (which we probably do), the command to do this also
sets the direction for all the other lines on port A.  It appears that
writing and reading of lines will also work in a "bundled" manner,
e.g. writing or reading all the lines on a given port with a single
command.

From a data perspective I think we need to manage this by modelling:

1) a mapping from room numbers to World 1 box port/bit combinations, and

2) an image of how the bits in each port should be set (possibly
augmented by a "dirty flag" that indicates the port needs to be
refreshed).

Thus the initialization phase will begin by populating the mapping
(1).  While this could be hardcoded for a quick implementation, it may
turn out to be very nice to be able to "rewire" the map without
recompiling the application.

Then we will set the I/O directions for all the digital lines to
output (with the possible exception of line 5 on port A), and
initialize the image and refresh all the ports to "lights out".

Those data structures (1) and (2) would probably be private data
members of the class, because the calling application only needs to
access the Switchbox instance through a method that requests a light
to be turned on or off.

That method we can define as something like this:

public LampSet( int rn, Boolean ls ) { ... }

The implementation of this method, of course, will require access to
the private data (1) and (2) managed by the class.

regards, mathtalk-ga

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy