473,657 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serial communication problem with Framework 2.0

Hi,

I've been able to communicate using HyperTerminal with my device via
serial port COM1. I just send a command and device switches on. I just
need that to start playing with it. This way, I understand device and
serial communication work wihout problems.

Now I want to do the same with C# code. As simple as that.

I open the serial port with the configuration manufacturer gives me,
send a command with Write() method and wait for data received event to
fire. I understand command is sent (no errors), but device is not
switched on and inmediatelly I data received event is fired. Here I
always get a "?" character, which seems to be an error character.
I'm using "Write", because in HyperTerminal I just type "DM" and device
responds. I also have tried with WriteLine() and so on.

This is my code, I would be very grateful if anybody could take a look
at it:
------------------------------------------------------------------------------------------------------------------
private void ReceiveData(obj ect sender, SerialDataRecei vedEventArgs e)
{
try
{
MessageBox.Show (serialPort.Rea dExisting());
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}

private void ErrorInSerialCo mmunication(obj ect sender,
SerialErrorRece ivedEventArgs e)
{
MessageBox.Show ("ERROR!");
}

private void button1_Click(o bject sender, System.EventArg s e)
{
try
{
serialPort.Baud Rate = 9600;
serialPort.Stop Bits = StopBits.One;
serialPort.Pari ty = Parity.None;
serialPort.Data Bits = 8;
serialPort.Port Name = "COM1";
serialPort.Hand shake = Handshake.None;
serialPort.Data Received += new
SerialDataRecei vedEventHandler (ReceiveData);
serialPort.Erro rReceived += new
SerialErrorRece ivedEventHandle r(ErrorInSerial Communication);
serialPort.Open ();
serialPort.RtsE nable = true;
serialPort.DtrE nable = true;
serialPort.Writ e("DM");
}
catch (Exception exc)
{
MessageBox.Show (exc.Message);
}
}

Manufacturer tells me about FlowControl = None, I understand that
matches with Handshake = None.

Thanks very much for all your help. It's really urgent.

Jan 18 '06 #1
4 11043
Try adding a "\r" to the Write command like this:

serialPort.Writ e("DM\r");

We used to have to do with with some serial processing. Perhaps it
will help you.

Also, if you can find out the value of the ? that you receive back.
Perhaps it is some sort of Ack/Nak code. 6 = ack and 21 = nak
(negative ack).

I have always believed that RS232 serial communications was a 'black
art'.

Jan 18 '06 #2
I've read in this previous post
http://groups.google.es/group/micros...aracter&rnum=1
that could be because of the Encoding I'm using. When serial port
returns "?" character seems to mean that the serial port is not able to
decode correctly some character.

The default Encoding of the serial port is "us-ascii", I've changed
this encoding to Encoding.Defaul t, which is in my case Western European
Encoding. Does not work again!

The specifications of the manufacturer are the following:

------------------------------------------------------------------------------------
Serial commands and responses are encoded as ASCII characters. A
checksum is generated for all messages and the
hexadecimal representation of the least significant 16 bits of the
checksum (a blank followed by 4 characters) is
placed at the end of each response message, just before the carriage
return (<CR>), line feed (<LF>) pair.

The following conditions must be met:
1. The computer must assert (apply a positive RS-232 voltage to) RTS
and/or DTR. Either or
both of these signals supply power to the cable circuitry.
2. The computer may leave RTS "open" but may not drive it to a negative
RS-232 level.
3. The computer communications port must be set to 9600 baud, 8 data
bits, no parity, and one
stop bit.

Baud Rate = 9600 bps Data Bits = 8
Stop Bits = 1 Parity = none
Flow Control = None Com Port = port # utilized
----------------------------------------------------------------------------------------------------------

Please could anyone help me? I'm sure it's a very simple thing, and
almost sure it's related to encoding and the way I'm writing in the
port. I would need a clear explanation of the steps I must follow,
always based on the manufacturer's specifications. I want to avoid
speaking with manufacturer at the moment.

With the encoding of the port changed, the character returned was not
"?"; it was a strange character, similar to a "Y". I suppose it means
the same but with different encoding.

Thanks very much.

Jan 19 '06 #3
Hi, it finally was a delay issue.
For a "DM" command, I was trying to send "DM" string with Write() and
WriteLine() method.

I finally achieved success in the serial communication by sending a
"D", sleeping 200 miliseconds and then sending "M" character and
sleeping again for 200 miliseconds.
It seems like the electronical engineering of the device I'm using is
quite slow and the communication must be performed this way.

That' all, just to let you know. Thanks very much.

Jan 30 '06 #4
ZS

Hi Lonifasiko
I'm trying to develop an application similar to yours but it does not
seem to work. I found a code on msdn that uses kernel32.dll etc... is it
necessary?

How did u instantiate your serial port.

Thank you .
-zelma
"Lonifasiko " wrote:
Hi,

I've been able to communicate using HyperTerminal with my device via
serial port COM1. I just send a command and device switches on. I just
need that to start playing with it. This way, I understand device and
serial communication work wihout problems.

Now I want to do the same with C# code. As simple as that.

I open the serial port with the configuration manufacturer gives me,
send a command with Write() method and wait for data received event to
fire. I understand command is sent (no errors), but device is not
switched on and inmediatelly I data received event is fired. Here I
always get a "?" character, which seems to be an error character.
I'm using "Write", because in HyperTerminal I just type "DM" and device
responds. I also have tried with WriteLine() and so on.

This is my code, I would be very grateful if anybody could take a look
at it:
------------------------------------------------------------------------------------------------------------------
private void ReceiveData(obj ect sender, SerialDataRecei vedEventArgs e)
{
try
{
MessageBox.Show (serialPort.Rea dExisting());
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}

private void ErrorInSerialCo mmunication(obj ect sender,
SerialErrorRece ivedEventArgs e)
{
MessageBox.Show ("ERROR!");
}

private void button1_Click(o bject sender, System.EventArg s e)
{
try
{
serialPort.Baud Rate = 9600;
serialPort.Stop Bits = StopBits.One;
serialPort.Pari ty = Parity.None;
serialPort.Data Bits = 8;
serialPort.Port Name = "COM1";
serialPort.Hand shake = Handshake.None;
serialPort.Data Received += new
SerialDataRecei vedEventHandler (ReceiveData);
serialPort.Erro rReceived += new
SerialErrorRece ivedEventHandle r(ErrorInSerial Communication);
serialPort.Open ();
serialPort.RtsE nable = true;
serialPort.DtrE nable = true;
serialPort.Writ e("DM");
}
catch (Exception exc)
{
MessageBox.Show (exc.Message);
}
}

Manufacturer tells me about FlowControl = None, I understand that
matches with Handshake = None.

Thanks very much for all your help. It's really urgent.

Feb 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8827
by: Andreas Horneff | last post by:
Hi @ all, I've got a problem with serial communication in Borland C++ Builder. I've already found a lot of stuff about serial communication in the internet, but it dosen't work. What I want to do: I want to connect only one button to my com port. If the button is pressed,
1
9697
by: Chris | last post by:
Hi, I'm trying to find out if a serial communication dll exists that would allow me to communicate to devices connected on the com port through .net. My implementation language will be C#. I was able to find MSCOMM.OCX but that is an ActiveX control which requires a gui (i believe?). Eventually, the application I will be running will be a Windows Service so I think I need a regular dll (I'm not sure though). Any suggestions?
3
3143
by: carmen | last post by:
I'm working in an aplication for a Smart Device that need to "talk" with a printer continuosly through the serial port. I'm trying to use the John Hint's sample code "Use P/Invoke to develop a .NET Base Class Library for Serial Device Communication" but I'm new with VC# and need some basic rules because I have a lot of compilation errors. Where should I create my inherited class? Thank you
4
4732
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows functions frequently in my C++ code. I came to know how to import the windows functions into Visual C#.NET. But what is the method to import windows structure into Visual C#.NET? It will be very helpful for me if anybody can give me the WebID that...
6
2855
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....
4
11186
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
2
3366
by: Mihai Popescu | last post by:
I wrote a simple application in MFC that uses MSComm ActiveX Control for serial communication. This app works just fine but I have to port it to Pocket PC. So, I wrote another simple application in C# for Pocket PC now using a System.IO.Ports.SerialPort object with the same communication parameters as the former: 19200,n,8,1 and on COM4. The problem is that the latter app generates an IO exception System.IO.FileNotFoundException cause...
4
4804
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.
2
8271
by: Adrian Chen | last post by:
please help me! I come across a problem. Now I develop a finger print management system which is based on B/S.When users click a button in the web pages, a device connected to the COM1 serial port of the client machine starts to work. Now I wrote the code which is in charge of serial communication in the ..aspx.cx pages, but then I found that when uses click the button, it is the server's serial port that starts to work,not the clients!! ...
0
8385
marktang
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...
0
8303
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,...
0
8723
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8602
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
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();...
0
4150
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...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.