Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI serialport
I have written code to read strings via the serial port, But now i need to alter it to read binary inputs.
 
Here is what I have at the moment
 
private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
             if (_continue == true)
             {
 
                    String^ data = serialPort1->ReadExisting();             //Reading all exsisting data in serialport buffer
                    StreamWriter^ sw= File::AppendText(fileloc->Text);      //Seting stream wirter up and pointing to selected file
                    sw->Write(data);                                        //write data to same line
                    this->databox->AppendText(data);                        //write data to rich text box

                    sw->Flush();                                            
                    sw->Close();
             }
         }
 
This is some of the changes that I have made
private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
             if (_continue == true)
             {
 
                    int data= serialPort1->ReadByte();
                    BinaryWriter^ bw = gcnew BinaryWriter( File::Open(fileloc->Text,FileMode::Append));
                    bw->Write(data);
                    //this->databox->AppendText(data);       Not started on writing binary to the Rich text box

                    bw->Flush();
                    bw->Close();
             }
         }
 
Thanks for any help!
Posted 21-Apr-11 11:24am
Edited 21-Apr-11 11:41am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to call ReadByte in a loop until it returns -1. And then you can probably use File::WriteAllBytes to write it to a file. If you want to append to the file, you'd need to write more code (something similar to what you already have now.
 
And I don't think it makes sense to display binary data in a rich text box.
 
[Update]
--------
 
Example Read usage:
 
// 100 and 75 are arbitrary values for the sake of the example. 
// Do not hardcode this in your code.
array<byte>^ bytes = gcnew array<byte>(100);
serialPort->Read(bytes, 0, 75);
  Permalink  
v3
Comments
SAKryukov at 21-Apr-11 16:46pm
   
It's a solution, my 5.
--SA
Nishant Sivakumar at 21-Apr-11 16:49pm
   
Thanks SA!
Rick Shaub at 21-Apr-11 16:47pm
   
You can also call Read() into a byte buffer of arbitrary size. It won't block because we already know that there's data available.
Nishant Sivakumar at 21-Apr-11 16:49pm
   
Thanks Rick, I trust the OP will take your comment into consideration.
Member 7796364 at 21-Apr-11 17:03pm
   
Sorry I am pretty new at programing. Looking at MSDN it shows Read(array, Int32, Int32)
Not sure how to populate Read() for my example.
 
Nishant Sivakumar at 21-Apr-11 17:07pm
   
That assumes you know the count of bytes that are coming in. Or at least the max count. So for example, you could pass 256 for count when you know that the max will be less than that. Makes sense?
Member 7796364 at 22-Apr-11 10:09am
   
Yes the array needs to be longer enough to hold the data.
How would the code look for the line? what i have tried has not worked.
 
tried variations
 
int data = serialPort1->Read(array, int offset, int count);
 
this setup give me error C2275: 'cli::array' : illegal use of this type as an expression
Nishant Sivakumar at 22-Apr-11 10:25am
   
I've updated my answer with an example code snippet.
Member 7796364 at 22-Apr-11 10:34am
   
Thanks, to make sure I have this right. bytes is now an array of 100. then we are reading the serial port filling the bytes array with 75 bytes and 0 offset.
 
thanks for all this help.
Nishant Sivakumar at 22-Apr-11 10:35am
   
Yeah, that's correct.
Member 7796364 at 21-Apr-11 16:55pm
   
Writing to the rich text box is only to see if it is working. Would i need to write a loop? The data ReceivedEvent should run every time data is received(?)
Nishant Sivakumar at 21-Apr-11 17:01pm
   
The loop's needed for multiple bytes. Although you could use the overload Rick mentioned above to do it one go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100