473,566 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Send Output to a serial port in C# program

1 New Member
Hi,

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

ODD CODES WILL ENERGIZE RELAYS EVEN CODES WILL DE-ENERGIZE RELAYS


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.ReadToEn d().Split('\n') ;
reader.Close();

File.Delete(e.F ullPath);

string[] rawInfo = fileLines[this.watcherFil eRow - 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(inf o[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(port Name, baudRate, Parity.None, 8, StopBits.One);

port.Open();
port.Write(((ch ar)relayCode).T oString());
port.Close();

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.

TIA
Sep 25 '06 #1
0 5523

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

Similar topics

2
13161
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. However, I'm unsure of how exactly I should be designing the program, I thought I could use threading to start class: class scanner(Thread): def...
1
2544
by: kiran | last post by:
Hi all, I have a problem to communicate with serial port(COM3:). I am able to open the handle but cannot send any data. 1. I connected my motoroala handset to PC through datacable. 2. I installed valid driver to detec the motorla(V400) handset 3. I tested the handset through Hypertermal and succssfully sending any data. 4. I opened the...
3
9094
by: Sara | last post by:
HI, I want to code a program to detect GSM mobile (any kind) which connected through serial port to computer and then be able to send SMS through this mobile phone to other mobile phones, could anyone help me and guide me, I wrote a program which could open COM port but still couldn't detect mobile phone and send SMS through it. I searched...
13
4800
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is...
0
2625
by: Sara | last post by:
HI, I want to code a program to detect GSM mobile (any kind) which connected through serial port to computer and then be able to send SMS through this mobile phone to other mobile phones, could anyone help me and guide me, I wrote a program which could open COM port but still couldn't detect mobile phone and send SMS through it. I searched...
1
2625
by: rsaikamesh | last post by:
Hi, I have connected RFID printer(Zebra R2844-Z) to the serial port(/dev/ttyS0).My OS is linux(ubuntu). The following is a program which is written in ZPL: ^XA ^RI0,,5^FS ^FO20,120^A0N,60^FN0^FS ^HV0,,Tag ID:^FS
3
11557
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import...
2
1631
by: barantamer | last post by:
hello i am trying to make a real-time clock with a microprocessor , and i want to display the output on my pc with c# . my code is below #region Namespace Inclusions using System; using System.IO.Ports; using System.Windows.Forms; #endregion
6
6637
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.