473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ 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.co m
"Haluk" <xxxxxxxxxxxxxx > wrote in message
news:b03001c20e 13$606eb230$37e f2ecf@TKMSFTNGX A13...
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 .InteropService s.COMException'
occurred in axinterop.mscom mlib.dll
Additional information: Exception from HRESULT: 0x800A017C
(CTL_E_INVALIDP ROPERTYVALUE).

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_DataReceive d(object sender, SerialDataRecei vedEventArgs
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 1783

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

Similar topics

1
8816
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,
3
3141
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....
6
14012
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 students)
4
4729
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...
6
2851
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...
4
11181
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...
0
1044
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 . i want to read the existing data in the rs232 buffer and write to the comport and want some data regarding serailport control in vs .net 2.0
4
4800
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...
2
8265
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...
0
7894
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...
0
8176
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
8321
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
7931
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...
1
5699
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
5370
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...
0
3816
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...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.