locked
SerialPort->Write(Byte) problem RRS feed

  • Question

  • Hi,

    I m writing a serial port application to send a number of data located an unsigned char array. I can not call serialPort->write method to send out my data since it does not accept my first paremeter altough i tried a few different types which can be seen from forums.

    I m using Visual C++ 2010 Express edition with .NET 4 Framework

    The buffer i want to send declared as;

     

     

    unsigned char TxBfr [24];


    And the write method called as;

    serialPort1->Write(TxBfr[0],

     

    0,24);


    this is the last step to complete my project and i have limited time. Could you please help me?

    Best regards,

     
    Friday, March 12, 2010 3:50 PM

Answers

  • Hi,

    Are you getting the following compiler error with your codes?

    error C2664: 'void System::IO::Ports::SerialPort::Write(cli::array<Type,dimension> ^,int,int)' : cannot convert parameter 1 from 'unsigned char' to 'cli::array<Type,dimension> ^'

    1> with

    1> [

    1> Type=wchar_t,

    1> dimension=1

    1> ]

    If so, then you need to convert your TxBfr, you can try the following codes:

     

    array<unsigned char>^ texBufArray = gcnew array<unsigned char>(24);

     

    for (int i=0; i<24; i++)

    texBufArray[i] = texBuf[i];

    serialPort->Write(texBufArray, 0, 24);


    Also, this post has a detailed example, you may want to take a look:
    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/6d47261b-27c6-45d4-8c02-81d1490011fd


    Thanks,
    Jianhua
    • Marked as answer by Nancy Shao Friday, March 19, 2010 2:17 AM
    Friday, March 12, 2010 6:33 PM

All replies

  • Hi,

    Are you getting the following compiler error with your codes?

    error C2664: 'void System::IO::Ports::SerialPort::Write(cli::array<Type,dimension> ^,int,int)' : cannot convert parameter 1 from 'unsigned char' to 'cli::array<Type,dimension> ^'

    1> with

    1> [

    1> Type=wchar_t,

    1> dimension=1

    1> ]

    If so, then you need to convert your TxBfr, you can try the following codes:

     

    array<unsigned char>^ texBufArray = gcnew array<unsigned char>(24);

     

    for (int i=0; i<24; i++)

    texBufArray[i] = texBuf[i];

    serialPort->Write(texBufArray, 0, 24);


    Also, this post has a detailed example, you may want to take a look:
    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/6d47261b-27c6-45d4-8c02-81d1490011fd


    Thanks,
    Jianhua
    • Marked as answer by Nancy Shao Friday, March 19, 2010 2:17 AM
    Friday, March 12, 2010 6:33 PM
  • Hi Jianhua,

    Thanks a lot for your answer. Yes you are rigth. I was geting the same error you stated. Moreover, I have already cleared this problem by using following local variable and sent comments;

     

     

    array<Byte>^TxBfr={0xC0,0x00,0x00,0xC1,0x00,0x00,0xC2,0x00,0x00,0xC3,0x00,0x00,

    0xC4,0x00,0x00,0xC5,0x00,0x00,0xC6,0x00,0x00,0xC7,0x00,0x00};

    serialPort1->Write(TxBfr,

     

    0,3); Thread::Sleep(2); // 192 yi gönder 2 ms bekle

    serialPort1->Write(TxBfr,

     

    3,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    6,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    9,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    12,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    15,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    18,3); Thread::Sleep(2);

    serialPort1->Write(TxBfr,

     

    21,3); Thread::Sleep(2); // 199 u gönder 2 ms bekle

    I m correctly getting these data with my 16-bit dual core MCU. No problem on sending data now. But now i just want to know since it will make my job easier; How can i define an array as global variable. When i use the same array at global decleration area it gives following error

    My Project is SERPAR_FORM

    decleration is under namespace SERPAR_FORM

    "error C3145: 'texBufArray' : global or static variable may not have managed type 'cli::array<Type> ^'

    1> with

    1> [

    1> Type=unsigned char

    1> ]

    1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap

     

    "

    decleration is under FORM1 public area and error is like below;

    "error C3845: 'SerPar_Form::Form1::texBufArray': only static data members can be initialized inside a ref class or value type"

    I just wonder is it possible to define arrays as global to use all over the form?

    -------------------------------------------------------------------------------------------

    Second and important question now is receiving data that are sent;

    i m using serial port receive data tread to get data

     

     

    public:

    String^ RxDt;

    private

     

     

    : System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {

     

    RxDt=serialPort1->ReadExisting();

     

    // Listbox a gelen veriyi koy

     

    }

    listBox2->BeginUpdate();

    listBox2->Items->Add(RxDt);

    listBox2->EndUpdate();

    but this way i can not see what i send in listbox2 since i m sending byte values but receiving as string. So i m seeing somethings which are not meaningfull at listbox2. I m around the complete project but i need to see what i send. sending lines directly connected to the receiving lines. So it is completeley loopback and i must see what i send. I tried to change type change but could not find any way. Do you have any suggestion to read sending bytes?

    Best regards,

    Wednesday, March 24, 2010 9:54 PM
  • Hi,

    C++/CLI doesn't support global variables.

    I think you can define a class, and then inside it, define the array as a public static member, this will expose it everywhere in your codes.

    For your second question, I found a post on web, is that what you want?

    http://programmerramblings.blogspot.com/2008/03/convert-hex-string-to-byte-array-and.html

    Thanks,

    Jianhua

    Thursday, March 25, 2010 7:45 PM
  • Hi Jianhua Li,

    Thanks a lot for your kindly support.

    I defined a class which involve an array. Then i changed serial port read method from ReadExisting (returns string) to ReadByte (returns byte).

    I finalized the GUI.

    Best regards,

    Tuesday, April 13, 2010 6:41 PM