Writing an int or other variables to the serial port.

I've setup serial comm's on VC++ 2005 and can open the port and write the information out. However it wil only write as a string or char array. The prototypes don't seem to give the option for anything else

this->Serialport->Write(string): Is how its currently being done. I want to be able to write a number out without it taking up so many bytes..

Any ideas ?

Thanks.

[389 byte] By [Paulw0t] at [2008-1-10]
# 1

If your Serialport member is a SerialPort object, then according to MSDN it supports transferring of byte arrays. Now your problem is: how to convert a number to and from byte array?

Supposing this, you can consider and improve the following example for long numbers:

Code Block

// sending:

long x = 12345;

array< byte > ^ b = gcnew array< byte >(sizeof(x));

pin_ptr< byte > p = &b[0];

*(long*)p = x;

this->Serialport->Write(b, 0, b.length);

Code Block

// receiving:

long x;

array< byte > ^ b = gcnew array< byte >(sizeof(x));

this->Serialport->Read(b, 0, b.length);

pin_ptr< byte > p = &b[0];

x = *(long*)p;

I hope this helps. Otherwise give us more relevant details.

Viorel. at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ Language...