473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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(object sender, SerialDataReceivedEventArgs e)
{
try
{
MessageBox.Show(serialPort.ReadExisting());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void ErrorInSerialCommunication(object sender,
SerialErrorReceivedEventArgs e)
{
MessageBox.Show("ERROR!");
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.PortName = "COM1";
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new
SerialDataReceivedEventHandler(ReceiveData);
serialPort.ErrorReceived += new
SerialErrorReceivedEventHandler(ErrorInSerialCommu nication);
serialPort.Open();
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.Write("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 11017
Try adding a "\r" to the Write command like this:

serialPort.Write("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.Default, 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(object sender, SerialDataReceivedEventArgs e)
{
try
{
MessageBox.Show(serialPort.ReadExisting());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void ErrorInSerialCommunication(object sender,
SerialErrorReceivedEventArgs e)
{
MessageBox.Show("ERROR!");
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.PortName = "COM1";
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new
SerialDataReceivedEventHandler(ReceiveData);
serialPort.ErrorReceived += new
SerialErrorReceivedEventHandler(ErrorInSerialCommu nication);
serialPort.Open();
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.Write("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
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...
1
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...
3
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...
4
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...
6
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...
4
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. ...
2
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...
4
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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,...
0
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...

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.