473,473 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Serial Communication in C# Express

I had an inquiry about the use of MSComm OCX in a C# application awhile
ago and I really appreciated Nicholas Paldino's help, which is listed
below...
My code worked well in VS2003. I am now transfering the code into C#
Express, which has a new SerialPort class in it. I came across another
problem this time. My code is also listed below. I would appreciate your
comments.

/******* Your previous answer to my inquiry ********/
Haluk,

When you set the output like this:

com.Output = ((char) 34) + ((char) 14);

You are actually sending a (char) type, not a string, which is what
you really want to send. To do this, do the following:

com.Output = new string(new char[]{(char) 34, (char) 14});

This will create a new string composed of the two characters that
will be marshaled correctly to the COM object.

Also, I suspect that the output stops when it hits the null
character. To get around this, you will have to send the byte manually.

Hope this helps.

--
- Nicholas Paldino [.NET MVP]
- ni**************@xxxxxxxxxx.com
"Haluk" <xxxxxxxxxxxxxx> wrote in message
news:b03001c20e13$606eb230$37ef2ecf@TKMSFTNGXA13.. .
I am using mscommlib.ocx in my C# application to
communicate via the serial port.
After all the initialization code which I had already used
in my previous VB6 applications successfully, I am using
the following code to transmit data:

com.Output = ((char) 34)+ ((char) 14);

I am getting the following error code when I run this
program:

An unhandled exception of
type 'System.Runtime.InteropServices.COMException'
occurred in axinterop.mscommlib.dll
Additional information: Exception from HRESULT: 0x800A017C
(CTL_E_INVALIDPROPERTYVALUE).

But also if I write the same code as follows:

com.Output = "" + ((char) 34)+ ((char) 14);

It runs successfully.
This is my first problem...

The second one is: instead of '34' or '14', for example if
I decide to send '0' (zero), this character is not transmitted at all!
com.Output = "" + ((char) 34)+ ((char) 0);

I can only achieve to transmit '34', but not '0' when I
run this code..

Any explanation will be deeply appreciated..

Regards,
Haluk

/******* My new code in C# (Express) ******************/

After the usual port initialization,

if (com.IsOpen) com.Close();
com.Open();

// send char 34,14,192,51,0,0
com.Write(new string(new char[] { (char)34, (char)14, (char)192,
(char)51, (char)0, (char)0 }, 0, 6));

// for testing purposes I connected TX and RX pins of the port...

// receive routine:
private void com_DataReceived(object sender, SerialDataReceivedEventArgs
e)
{

// This method will be called when there is data waiting in the port's
buffer

// Obtain the number of bytes waiting in the port's buffer
bytes = com.BytesToRead;

// Create a byte array buffer to hold the incoming data
buffer = new char[bytes];

// Read the data from the port and store it in our buffer
com.Read(buffer, 0, bytes);

}
/****** My question about this new code *******/
After I activate the send routine, 6 bytes of data is received by pc but
with some difference to the originally sent data.

I receive : 34,14,63,51,0,0 everytime instead of 34,14,192,51,0,0

I tried to transmit 34,14,127,51,0,0 and received the same data
sequence...

As I try to tranmit anything bigger than 127 I receive 63 instead of the
original data...

Do you have any idea why this happens?
Best Regards,
Haluk
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
0 1774

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...
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...
6
by: Jeff | last post by:
Hello, Someone can help me about serial port with C++ .NET (Windows forms application). Like setting port, opening it, transmiting, receiving, closing, etc. Thanks very much (for me and my...
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. ...
0
by: nmsreddi | last post by:
hello any one using c#2005 express edition i want to know how to use the system.io.ports namespace for serial communication ,for interacting with the hard ware connected to my pc through rs232...
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
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
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...
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,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.