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

how to completely send a string through serial port

Hi

I am doing a messaging application using Visual C++ window form application.

Sometimes I can't receive a whole message from the sender. If i type short sentence like "123456", it can be shown properly. But, if i type a quite long sentence like "123456789123456789", it can't shown a correct message, can't show exactly the whole message.

here is my code:

Expand|Select|Wrap|Line Numbers
  1. //transmission:
  2. String^ message;
  3. String^ message1;
  4. String^ name = this->serialPort1->PortName;
  5. // grab text and store in send buffer
  6. message = this->richTextBox2->Text;
  7. // write to serial
  8. if(this->serialPort1->IsOpen)
  9. this->serialPort1->WriteLine(message);
  10. this->serialPort1->DiscardOutBuffer();
  11. this -> serialPort1 -> DiscardInBuffer();
  12. }
  13.  
  14. //Receving code:
  15. demessage = " ";
  16. ciphertext = " ";
  17. try
  18. {
  19. ciphertext = this->serialPort1->ReadExisting();
  20. this -> Invoke (gcnew System::EventHandler(this, &Form1::DisplayText));
  21. }
  22. catch(TimeoutException^)
  23. {
  24. MessageBox::Show("Timeout Exception");
  25. }
  26. private: System::Void DisplayText (System::Object^ s, System::EventArgs^ e)
  27. {
  28. this -> richTextBox1 -> Text = String::Empty;
  29. this->richTextBox1->AppendText(ciphertext);
  30. }
May 9 '13 #1
1 2704
Banfa
9,065 Expert Mod 8TB
A serial port provides a byte stream, it is not message based. There is no such thing as a whole sentence as far as a serial port is concerned.

That means you need to be able to tell from the data transmitted where the end of the sentence is, for example you transmit a sentinel character (say a full stop) which you can look for when receiving to indicate the end of data.

Then you read the serial port in a loop and you keep reading it until you have received the end of data marker, and then you output the data (sentence) you have received.


Also I don't know how .NET handles the serial port but in your writer it seems a little strange to discard the output buffer immediately after having written data to the port. How do you know that all the data was actually sent? It takes time to send data on a serial port, a lot more time than it takes the processor to process commands. I would have thought a command to flush the output buffer (if one exists) would be a better thing to call.
May 9 '13 #2

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

Similar topics

1
by: kiran | last post by:
Hi all, I have a problem to communicate with serial port(COM3:). I am able to open the handle but cannot send any data. 1. I connected my motoroala handset to PC through datacable. 2. I...
1
by: henrycortezwu | last post by:
Hi All, I'm trying to connect to a virtual port (COM19, OUTGOING, "Bluetooth Serial Port") using VS2005 System.IO.Ports. When I ran the ff code below here's what happens. 1) VS2005 Compiles w/o...
4
by: H J van Rooyen | last post by:
Hi All, I am writing a polling controller for an RS-485 line that has several addressable devices connected. It is a small access control system. All is well- the code runs for anything from...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
3
by: michael1987 | last post by:
I was writing a program calculating angles. There is a horizontal and a vertical angle and when I want the serial port to read both, it can give the horizontal digit by digit and the vertical all at...
2
by: Lou | last post by:
I have a class that creates an instance of the seril Port. Every thing works fine except whenever I receive data I cannot display the recieved data. I get no errors but the recived data seems to...
6
by: cnixuser | last post by:
Hello, I have a basic application written which is designed to data over a serial cable and then receive a response back. I am not getting any triggers to my data received event. I have tried...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
0
by: Charles Crawford | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: =?Utf-8?B?Q2hhcmxpZQ==?= | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.