Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a program to read data from serial port
appsto port
    R L S F 1 CR LF
port to apps
    R L S F 1 CR LF
port to apps
   L D S E 3 CR LF

 READ data 
port to apps
L D E  D 5 CR LF
apps to port
R L E E 3 CR LF
 port to apps
R L E E 3 CR LF

o/p in serial monitor 
 rec:-   R L S F 1 CR LF
 snd:-  R L S F 1 CR LF
 snd:-  L D S E 3 CR LF
data didnt transfer to apps from microcontroller
 snd:-  L D E  D 5 CR LF
 rec:-  R L E E 3 CR LF
 snd:-  R L E E 3 CR LF


What I have tried:

private: System::Void ReadBtn_Click(System::Object^ sender, System::EventArgs^ e)
   {
       if (serialPort1->IsOpen)
       {
           cli::array<System::Byte>^ dateS = gcnew cli::array<System::Byte> {0x52, 0x4C, 0x53, 0x46, 0x31, 0x0D, 0x0A};
           serialPort1->Write(dateS, 0, dateS->Length);
           cli::array<System::Byte>^ byts = gcnew cli::array<System::Byte>(dateS->Length);
           serialPort1->Read(byts, 0, dateS->Length);
           Thread::Sleep(10);
           textBox2->Text = System::Text::UTF8Encoding::UTF8->GetString(byts);
           textBox1->Text = "RLSF1";

           if (textBox1.Text==textBox2.Text)
           {
               serialPort1->DataReceived += gcnew SerialDataReceivedEventHandler(port_OnReceiveData); // Add DataReceived Event Handler
           }
           Console::Read();

       }

   }
   private : static void port_OnReceiveData(Object^ sender,SerialDataReceivedEventArgs^ e)
   {
          SerialPort^ spL = (SerialPort^)sender;
            String^ deta =  spL->ReadExisting();
            MessageBox::Show(deta);
            cli::array<System::Byte>^ en = gcnew cli::array<System::Byte>{0x52, 0x4C, 0x45, 0x45, 0x33, 0x0D, 0x0A};
            spL->Write(en, 0, en->Length);
            cli::array<System::Byte>^ bys = gcnew cli::array<System::Byte>(en->Length);
            spL->Read(bys, 0, en->Length);

            spL->Close();


   }
Posted
Updated 30-Aug-22 22:14pm
v2
Comments
CPallini 31-Aug-22 3:04am    
And... What is the problem?
n.deny 31-Aug-22 3:12am    
i received only commands without data

Quote:
serialPort1->Write(dateS, 0, dateS->Length);
cli::array<system::byte>^ byts = gcnew cli::array<system::byte>(dateS->Length);
serialPort1->Read(byts, 0, dateS->Length);

Just a wild guess (you post omits significant details): you are trying to receive the same amount of bytes that your are sending. This is probably not correct: you should try to receive the number of bytes stated by the protocol.
 
Share this answer
 
Comments
n.deny 31-Aug-22 20:01pm    
@CPallini thanks for the comment.Actually i am trying to read the data from the microcontroller. when i sent the RLSF1CRLF command to microcontroller ,It will send back to pc with LDSE3CRLF to pc and data read will start and send LDED5 command after completing the reading process to pc..
here i received all the command correctly except data ..
Your code assumes that the remote device will send all its data in a single burst according to your read command. However this is not how serial I/O works. It is an asynchronous process and characters need to be captured as they are received in order to build the complete message. I gave you a suggestion at How to received data from microcontroller in C++/cli using command line[^], which helps to explain how to use it.
 
Share this answer
 

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