Hello, I have a basic application written which is designed to data over a serial cable and then receive a response back. I am not getting any triggers to my data received event. I have tried connecting the pc I am running this application on to another PC which is also sending hex characters ;however, when I run the application I am getting no trigger of the data received event and am completely clueless as to why. Any help that could be provided would be greatly appreciated.
Below is my code and App.config contents: - using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO.Ports;
-
-
namespace DEXtransactionParserTestApp
-
{
-
public static class SerialPortCommands
-
{
-
public static bool received = false;
-
private static SerialPort dexConnection = new SerialPort();
-
// -- Properties ---------------------------------------------------------
-
-
// -- Public methods -----------------------------------------------------
-
-
#region PortControl
-
-
public static void InitializePort(string PortNumber, Parity par,int dataBits, int baudRate)
-
{
-
dexConnection.PortName = PortNumber;
-
dexConnection.Parity = par;
-
dexConnection.BaudRate = baudRate;
-
dexConnection.DataBits = dataBits;
-
dexConnection.DataReceived += new SerialDataReceivedEventHandler(dexConnection_DataReceived);
-
}
-
-
-
public static void EnablePort()
-
{
-
if (!dexConnection.IsOpen)
-
dexConnection.Open();
-
}
-
-
public static void DisablePort()
-
{
-
if (dexConnection.IsOpen)
-
dexConnection.Close();
-
}
-
#endregion
-
-
#region Communication
-
public static void SendASCIIMessage(string Message)
-
{
-
//dexConnection.Write(Message);
-
int test21 = 0X05;
-
byte[] test = new byte[1];
-
test[0] = (byte)test21;
-
dexConnection.Write(test, 0, 1);
-
//dexConnection.Write(ConvertToBytes(Message),0,ConvertToBytes(Message).Length);
-
}
-
-
-
#endregion
-
-
// -- Helper methods -------------------------------------------------------------
-
private static byte[] ConvertToBytes(string ascii)
-
{
-
byte[] asciiBytes = Encoding.ASCII.GetBytes(ascii);
-
return asciiBytes;
-
}
-
// -- Events ---------------------------------------------------------------------
-
private static void dexConnection_DataReceived(object sender, SerialDataReceivedEventArgs e)
-
{
-
received = true;
-
Console.WriteLine("Response From VMD:");
-
Console.WriteLine(dexConnection.ReadExisting().ToString());
-
Console.ReadLine();
-
//throw new Exception("The method or operation is not implemented.");
-
-
-
//TODO: General code to determine which method to call to process a response
-
-
//TODO: Add code to process "event" messages from the VMD
-
-
//TODO: Add code to process "report" data from VMD
-
-
//TODO: Add code to process "handshake" signals from VMD
-
}
-
-
}
-
}
-
- using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO;
-
using System.Text.RegularExpressions;
-
using System.IO.Ports;
-
using System.Configuration;
-
using System.Threading;
-
namespace DEXtransactionParserTestApp
-
{
-
class Program
-
{
-
public static string[] records;
-
-
static void Main(string[] args)
-
{
-
System.Windows.Forms.Application.DoEvents();
-
#region testVersionStuff1
-
//string viewSplitRecord = "";
-
-
//string ReportLine1 = "DXS*RST7654321*VA*V0/6*1";
-
//string ReportLine2 = "ST*001*0001";
-
//string ReportLine3 = "ID1*112233445566*ABC1234*0101*Building 1**777888999";
-
//string rep1and2 = ReportLine1;
-
//rep1and2 += ReportLine2;
-
////string[] sites = rep1and2.Split('*');
-
////string[] sites = ReportCapture.currRep.Split("crlf");
-
////string[] sites = ReportCapture.currRep.Split('*');
-
//records = Regex.Split(ReportCapture.currRep, "crlf");
-
//processRecord(records[0]);
-
//string[] sites = Regex.Split(ReportCapture.currRep, "crlf");
-
//string[] viewSplitField = sites[0].Split('*');
-
//foreach (string s in sites)
-
//{ /*Console.WriteLine(s)*/
-
// viewSplitRecord += s+"\n"; //;
-
-
-
//}
-
//Console.Write(viewSplitRecord[0]);
-
//Console.WriteLine(viewSplitField[0]);
-
-
//Console.ReadLine();
-
#endregion
-
-
-
Parity spPar;
-
if (ConfigurationManager.AppSettings["parityType"].ToString() == "EVEN")
-
{
-
spPar = Parity.Even;
-
}
-
-
if (ConfigurationManager.AppSettings["parityType"].ToString() == "ODD")
-
spPar = Parity.Odd;
-
-
else
-
spPar = Parity.None;
-
-
SerialPortCommands.InitializePort(ConfigurationManager.AppSettings["portNumber"].ToString(), spPar, Int32.Parse(ConfigurationManager.AppSettings["DataBits"].ToString()), Int32.Parse(ConfigurationManager.AppSettings["BaudRate"].ToString()));
-
SerialPortCommands.EnablePort();
-
Console.WriteLine("Type Message to send to the VMD\nthen press Enter:");
-
string Message = Console.ReadLine();
-
while (!SerialPortCommands.received)
-
{
-
-
SerialPortCommands.SendASCIIMessage(Message);
-
Thread.Sleep(1100);
-
//Console.ReadLine();
-
//Console.WriteLine("Response From VMD");
-
//SerialPortCommands.DisablePort();
-
}
-
-
Console.ReadLine();
-
SerialPortCommands.DisablePort();
-
}
-
#region unUsedReportCode
-
//public static string processRecord(string theRecord)
-
//{
-
// string processedReocrd = "";
-
// Match m = Regex.Match(theRecord, "^[A-Za-z0-9]*\\\\*");
-
// Console.WriteLine(m);
-
// Console.WriteLine("\n" + "\n");
-
-
// switch (m.ToString())
-
// {
-
// case "ID1":
-
// ID1Process(theRecord);
-
// break;
-
-
// case "DXS":
-
// break;
-
-
// case "VA1":
-
// break;
-
-
// case "DXE":
-
// break;
-
-
// case "ST":
-
// break;
-
-
// case "CA2":
-
// break;
-
// case "CA3":
-
// break;
-
-
// case "CA4":
-
// break;
-
-
// case "PA1":
-
// break;
-
-
// case "PA2":
-
// break;
-
-
// case "DA2":
-
// break;
-
-
// default:
-
// break;
-
-
-
// }
-
// return processedReocrd;
-
//}
-
-
//public static string ID1Process(string ID1Record)
-
//{
-
// ID1Record = Regex.Replace(ID1Record, "ID1"+Regex.Escape("*"), "");
-
// Console.WriteLine(ID1Record);
-
-
// //System.Windows.Forms.MessageBox.Show(ID1Record);
-
-
// //SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
-
// return ID1Record;
-
//}
-
-
//public byte[] ConvertToBytes(string ascii)
-
//{
-
// byte[] test = Encoding.ASCII.GetBytes("test");
-
// return test;
-
//}
-
#endregion
-
-
}
-
}
-
- <?xml version="1.0" encoding="utf-8" ?>
-
<configuration>
-
<appSettings>
-
-
<!--Serial Port Object initialization Settings-->
-
<add key="portNumber" value="COM1"/>
-
<add key="parityType" value="NONE"/>
-
<add key="BaudRate" value="9600"/>
-
<add key="DataBits" value="7"/>
-
-
</appSettings>
-
</configuration>
6 11783
Instead of running your program, have you tried running hyperterminal to verify the connection, wiring, port, baud rate, handshaking and so on are all correct? Hyperterminal is available using programs...acce ssories...commu nications.
For me the most common mistake is not using a null modem cable when i should be. followed by the computers port not being enabled at all.
Let me know.
Plater 7,872
Recognized Expert Expert
Pc to PC requires using a special connector (just swap the RX and TX and you have basic functionality)
Do you use the correct Serial Port, sometimes they are not what you would think they are.
Did you make sure to set flow control to none?
Instead of running your program, have you tried running hyperterminal to verify the connection, wiring, port, baud rate, handshaking and so on are all correct? Hyperterminal is available using programs...acce ssories...commu nications.
For me the most common mistake is not using a null modem cable when i should be. followed by the computers port not being enabled at all.
Let me know.
I went out and bought a different null modem cable, once I did that the data_received event fired properly. The problem was with the old cable apparantly. Thanks! Hyper terminal, wouldn't work on the old cable either, but once I bought a new cable everything worked like a charm!
Pc to PC requires using a special connector (just swap the RX and TX and you have basic functionality)
Do you use the correct Serial Port, sometimes they are not what you would think they are.
Did you make sure to set flow control to none?
I don't believe that the flow control was set to none, it would seem to set as "Hardware" can that cause problems later on down the road?
A quick addition to my original question:
The following code will result in a hex byte with the value of 05 to be written to the serial port correct? - int test21 = 0x05;
-
byte[] test = new byte[1];
-
test[0] = (byte)test21;
-
dexConnection.Write(test, 0, test.Length);
Plater 7,872
Recognized Expert Expert
Well unless you actually use flow control, better to set it to none.
Hardware uses extra signal lines to control when data can be sent or not (whereas software uses special hex codes to determine when data can be sent or not)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jacob |
last post by:
Has anyone successfully received serial communications from a COM port/RS232C
device? It's easy to connect and write to a serial device with Framework 2.0,
but when I try to read from the input buffer I get nothing except a timeout
exception. No matter how high you set the SerialPort.ReadTimeout property it
doesn't seem to matter. My code (using C# for Framework 2.0) looks something
like this:
use System.IO.Ports;
public static void...
|
by: Peter Krikelis |
last post by:
Hi All,
I am having a problem setting up input mode for serial
communications.
(Sorry about the long code post).
The following code is what I use to set up my comm port.
|
by: Kevin |
last post by:
Hi all
I am having a problem reading from a serial port, first of all I have now resorted to using the MSComm ActiveX control on my Windows Forms to provide me with the interface to my serial port. is there no other way to do this - it has to be simple or well explained because I'm still new to the .NET world and c#. I did stumble across an article on the MSDN site where I downloaded a file NetSerialComm.exe - I think it was from one of the...
|
by: Leandro Berti via DotNetMonster.com |
last post by:
Hi All,
I wrote a code to do serial communication with an equipament.
When i use the code outside of threaded class it seens work properly,
but when i put inside a class and execute a thread in the first seconds the
communication is ok, later i receive read/write error.
I?ve been in MSDN site and there i discover that the read/write error is a
INVALID_HANDLE problem. But why??? I just create the serial communication
file and use it....
|
by: Tom Brown |
last post by:
Hey people,
I've written a python app that r/w eight serial ports to control eight devices
using eight threads. This all works very nicely in Linux. I even put a GUI on
it using PyQt4. Still works nicely.
Then I put the app on on a virtual Windows machine running inside of vmware on
the same Linux box. Vmware only lets me have four serial ports so I run the
app against four serial ports using four threads. The app did not respond...
| |
by: davetelling |
last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things
work they way I want. Using Visual C# Express, I have a form in which I added
a user control to display a graph, based upon data received via the serial
port. If I run the serial port in the main form code, I can get data and,
using public properties of the user control, transfer the data to be shown on
the graph. However, I am trying to add a feature that will...
|
by: laura salhuana |
last post by:
Hi:
I am trying to write a serial communication GUI application. My
application has the following specifications. When I set my board up in
hyperterminal I do the following set up: bits per second = 38400, data
bits = 8, parity = none, stop bits = 1, flow control = none. Then once I
hit OK if I type in a "V" my board responds with 6 bits of data.
xyz I have the following code to set up the com
port and then write a V and I have set...
|
by: Lou |
last post by:
I have a class that creates an instance of the seril Port.
Every thing works fine except whenever I receive data I cannot display the
recieved data. I get no errors but the recived data seems to just go no
where.
I can see the recived data in my serial receive function but when I either
raise an event
with it or try to display it in a text box nothing happens. I do use
beginInvoke on the text box.
If I trace the code through it all...
|
by: max_mont |
last post by:
Hi all,
I'm a newbie in .NET technology.
I've already developed Serial communication applications in C++
(WIN32).
And I wanted to migrate to .NET technology.
There is a serial component in framework to read and write on serial
port.
I would like to make asynchronous reception. I saw that we can pass a
delegate to the serial class which is call when some data is readen on
the port.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |