Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

C#
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
C#
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
Updated 21-Apr-11 10:41am
v2

1 solution

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:

C++
// 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);
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Apr-11 16:46pm    
It's a solution, my 5.
--SA
Nish Nishant 21-Apr-11 16:49pm    
Thanks SA!
Rick Shaub 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.
Nish Nishant 21-Apr-11 16:49pm    
Thanks Rick, I trust the OP will take your comment into consideration.
Member 7796364 21-Apr-11 17:03pm    
Sorry I am pretty new at programing. Looking at MSDN it shows Read(array<byte>, Int32, Int32)
Not sure how to populate Read() for my example.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900