473,395 Members | 1,863 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,395 software developers and data experts.

COM Port data write/read

I have a device connected to a COM port. I need to send it a couple of bytes of data and then it returns me a couple of bytes of data - basically I need to poll it for data readings. I just need a basic console application to do this job. I am new to this area and not sure where to start - any pointers would be much appreciated.

Thanks.
Sep 6 '10 #1

✓ answered by Alex Papadimoulis

Difficult to say why "sensible" data isn't coming back without knowing anything about the devices, etc. However, if it's simply a 2-byte request followed by a 2-byte response, I would write code like this:

Expand|Select|Wrap|Line Numbers
  1. mySerialPort.Write(new byte[] { 0x01, 0x05 }, 0, 2);
  2.  
  3. while (mySerialPort.BytesToRead < 2) Thread.Sleep(100);
  4.  
  5. byte[] response = new byte[2];
  6. mySerialPort.Read(response, 0, 2);

4 10589
Alex Papadimoulis
26 Expert
The System.IO.Ports will be able to help you here. The main class in this namespace is SerialPort.

Opening, reading/writing, and closing is pretty easy.

Expand|Select|Wrap|Line Numbers
  1. bool continue = true;
  2. SerialPort mySerialPort = new SerialPort();
  3.  
  4. mySerialPort.PortName = "COM1";
  5. mySerialPort.BaudRate = 9800;
  6.  
  7. // ...
  8.  
  9. mySerialPort.Open();
  10. while (continue) {
  11.   string msg = mySerialPort.ReadLine();  
  12.   ProcessMessage(msg);
  13.   Thread.Sleep(100);
  14. }
  15.  
  16. mySerialPort.Close();
Sep 7 '10 #2
Alex,

Thank you for the reply. The data I need to send and receive is hex data so I have tried to adapt your suggestion as follows.

mySerialPort.Open();

// read the port
while (readPort)
{
mySerialPort.Write(new byte[] { 0x01, 0x05 }, 0, 2);
// read the data
List<byte> myData= new List<byte>();
while (mySerialPort.BytesToRead > 0)
myData.Add((byte)mySerialPort.ReadByte());

ProcessData(myData);

// clear the data
myData.Clear();

Thread.Sleep(100);
}

mySerialPort.Close();



I know I have to send a 2 byte request and then receive two bytes of data back. However, my code isn't reading any sensible data back. Any thoughts?

Many thanks.
Sep 8 '10 #3
Alex Papadimoulis
26 Expert
Difficult to say why "sensible" data isn't coming back without knowing anything about the devices, etc. However, if it's simply a 2-byte request followed by a 2-byte response, I would write code like this:

Expand|Select|Wrap|Line Numbers
  1. mySerialPort.Write(new byte[] { 0x01, 0x05 }, 0, 2);
  2.  
  3. while (mySerialPort.BytesToRead < 2) Thread.Sleep(100);
  4.  
  5. byte[] response = new byte[2];
  6. mySerialPort.Read(response, 0, 2);
Sep 8 '10 #4
Alex,

Your suggestion works perfectly - I am now getting excactly what I expect from my device.

Many thanks for your input.
Sep 9 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: SzokU | last post by:
Hi All! I have problem... I want to write/read value from param tag in applet, but I don't know how... This is exaple code:...
1
by: Voronkov Konstantin | last post by:
Hello All! I want to ask you whether there is any way to check asynchronously whether stdin contain data to read. I tried all operations to read stdin, but it looks all that operations are...
2
by: Wanjun Yu | last post by:
What is the class name for open/write/read a text file? I can't find it in the reference book. Thanks! WJ
4
by: Petr Jakes | last post by:
I am trying to save data it is comming from the serial port continually for some period. (expect reading from serial port is 100% not a problem) Following is an example of the code I am trying to...
1
by: yipple | last post by:
Hello, I was trying to access my com port rs232 via C language in Linux. I hav a piece of hardware(thumbprint reader)connected to the port. I need to write a program which get to read data from...
4
bluegreenisland
by: bluegreenisland | last post by:
Hi everyone, I am new to this, so am bound to make blunders ~just let me know & I'll correct them. I am hoping to learn how to write to a file, make changes to the data, and read. I have a...
1
by: Narjis | last post by:
Hi everybody? hope y'r all fine I need a small help.. can anyone give me a c++ program that reads the serial port data? (i.e. that data are burned in a microcontroller chip and i want to connect...
2
by: crampio | last post by:
Hello everyone, I generally look at Google and other websites before I post a question, but trust me I still cannot find and answer to this problem. I'm using VB.net. My problem being is that I...
3
by: Ajinkya | last post by:
How can I poll for serial port data using javascript ?
5
by: Gilles Ganault | last post by:
Hello Out of curiosity, I was wondering what happens with session data: I read that session data are saved on disk as eg. /var/sess_ID (which is already a performance issue since accessing a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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...

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.