By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

Send Output to a serial port in C# program

P: 1

I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question.

The program opens a file and reads the contents that are seperated by spaces, not tabs - will never be seperated by tabs as it is output from another program.
The contents of the file are essiantlly 3 fields/values
# # Text
Field 1 will be a number from 1 to 26, Field 2 will always be a 1 and can be ignored, Filed 3 will either be ON or OFF. Unfortuantely I can not rearrange the order of the fileds either. I have limited access to the commercial program that is creating the output.

Once the File is read, the program deletes the file and continues with the program.

Then we need to determine the current State On or Off.

All of this up to this point is working as needed and works great.

Here is a snippet from the documentation on the controller I am working with.

Development of control software is a relatively simple task. To energize or de-energize a relay, a single byte code (0
thru 255 decimal or 00000000 thru 11111111 binary) is transmitted to the RS-232 port. The formula on the following
page is used to control additional relays up to relay #128 (including those in the chart on the following page).
Transmitting a (255) will energize relay #128. EXAMPLE: 255 = (128 x 2) - 1 Use this formula in your software to
compute relay code as a function of relay number and status.

RELAY CODE = (RELAY # times 2) - 1 ...To energize relay
RELAY CODE = (RELAY # times 2) - 2 ...To de-energize relay


So here is the code I have from the progammer I have been working with:

// Read the contents of the PRN file into a string array,
// separating each line into its own array element.
StreamReader reader = new StreamReader(e.FullPath);
string[] fileLines = reader.ReadToEnd().Split('\n');


string[] rawInfo = fileLines[this.watcherFileRow - 1].Split(' ');
string[] info = new string[3];

// The info we need is separated by spaces, not tabs, so it's difficult
// to parse. So, we're going to go through the whole string and eliminate
// the parts that aren't important.

int placeHolder = 0;
foreach (string element in rawInfo)
if (element.Trim() != string.Empty)
info[placeHolder++] = element;

// The first value indicates the relay number
int relayCode = Int32.Parse(info[0]);

//Skip second value as it is not needed
//Convert the third value to a boolean for easier handling.
bool state = (info[2].ToLower() == "on");

relayCode = (state)
? (relayCode * 2) - 1
: (relayCode * 2) - 2;

// TODO: There might be some changes that need to be made to the
// COM port setup. The port name and the baud rate are stored in the
// configuration file - the rest is hard coded below.
SerialPort port = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);


When I run my port monitor software I am getting two values written out the com port.

>>>---lights.prn 15 1 off
Port opened by process "LightsMonitor.exe" (PID: 3992)
Request: 9/25/2006 9:59:37 AM.02464 (+169.6039 seconds)
1C .
Port closed>>>--- lights.prn 15 1 on
Port opened by process "LightsMonitor.exe" (PID: 3992)
1D .
Port closed>>>--- lights.prn 26 1 off
Port opened by process "LightsMonitor.exe" (PID: 3992)
32 2
Port closed

I do not have the knowledge to troubleshoot this myself - any help is greatly appreaciated. I think the incorrect data is being written to the com port.

Sep 25 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.