简体   繁体   中英

serial port write c++

I'm quite new at programming in C++ and I need your help.

I want to make a simple GUI which will communicate with external device through a serial port and sanding char type to a device. And my question is next

  1. I don't understand mySirialPort->Write(array<Char>^, Int32, Int32) --- array<Char>^ what kind of type variable I need to write in.

Because I get next error.

1>Return_NAN.cpp(19): error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
1>          There is no context in which this conversion is possible
1>Return_NAN.cpp(30): error C2664: 'void System::IO::Ports::SerialPort::Write(cli::array<Type,dimension> ^,int,int)' : cannot convert parameter 1 from 'char' to 'cli::array<Type,dimension> ^'
1>          with
1>          [
1>              Type=wchar_t,
1>              dimension=1
1>          ]

my code:

char a = "A";        
SerialPort^ mySerialPort = gcnew SerialPort(a);
//mySerialPort->PortName = a;   
mySerialPort->BaudRate = 1200;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Handshake = Handshake::None;
mySerialPort->Open();
mySerialPort->Write(t,0,1); // problem
mySerialPort->Close();

If I write "A" directly to write function there are no errors while I'm compiling.

Thanks for your help, KB

System::IO::Ports::SerialPort is a .NET class. Keep in mind that you are using a language extension called C++/CLI, you'll save yourself a lot of time by reading a basic tutorial. It is different enough from C++ to have a learning curve, a week will do a lot of good to learn the basic types and knowing when to use the ^ hat.

You already found out that writing a string is easy, SerialPort::Write() has an overload that accepts a string. It will convert the string to ASCII so you can only write character values between 0 and 127:

String^ example1 = "ABC";
mySerialPort->Write(example1);

Writing a single byte is easiest done by writing to the BaseStream, no conversion is done:

Byte example2 = 'A';   // or 0x41
mySerialPort->BaseStream->WriteByte(example2);

If you want to write an array of bytes, like the error message says you do, then you have to create an array object:

array<Byte>^ example3 = gcnew array<Byte> { 0x01, 0x02, 0x42 };
mySerialPort->Write(example3, 0, example3->Length);

There's no fundamental reason to favor writing an array of bytes over writing the bytes one at a time, serial ports are very slow anyway.

the 1st error is simple: you should write: char a = 'A'; with a single quote, not "a" with double quote.

array^ is clr reference type, it probably expected to something like: array<char>^ t = {1,2,3}; look here:

but you better use write(string^) look here:

You can convert it from c string like this:

const char* str = "Hello, world!";
String^ clistr = gcnew String(str);//allocate cli string and convert the c string
// no need to delete, garbage collector will do it for you.
mySerialPort->Write(clistr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM