0

I want to send data hex format (0x01) or (0xff) to the port where rs232 cable is connected. I am using the MSCOMM control in VC++ 6.0. Is that the correct way to send the hex data. This is the code I have.

CString output;
UCHAR data1,data2;
data1=0x01;
output.Format("%x",data1);
m_mscom.SetOutput(COleVariant(output));
data2=0xff;
output.Format("%x",data2);
m_mscom.SetOutput(COleVariant(output));

If it is not correct, please tell me how to send hex format data to the port.

Thanks in Advance

Coral Doe
  • 1,925
  • 3
  • 19
  • 36

2 Answers2

1

If your data is simply a hex value that you want to send out (as opposed to a hex formatted string), you probably want "%c" rather than "%x". Given a data value of (e.g.) 0x65, "%x" will produce two bytes on the serial port: '6' (ascii value 54) and '5' (ascii value 53). "%c" will produce a single byte on the serial port: 'e' (ascii value 100 or 0x65).

As far as sending data on a serial port in C, have a look at CSerial or CSerialPort: they may simplify things for you a bit (note that I've not used them, I tend to do serial port apps in python with the pyserial module or in wxWidgets with the ctb library).

Edit: The other one that's quite good (I have used it before, but couldn't find the link when I wrote the original post) is CSerialFile, part of the WFC libraries. As I said, I tend to use wxWidgets now, but if you're using the Microsoft foundation classes, CSerialFile does make serial access very easy.

DrAl
  • 70,428
  • 10
  • 106
  • 108
  • what is wxWidgets?How it is useful? –  Jun 05 '09 at 08:40
  • wxWidgets is a GUI library (primarily for C++) that is free and open source. I find its design better than the Microsoft Foundation Classes (which seem to be too closely based on the C API rather than designed as a C++ library). The wxWidgets libraries have the added advantage that the source code will compile without changes for Windows, Linux & Mac, so your application becomes cross-platform for free! – DrAl Jul 01 '09 at 09:43
0

I'm no familiar with MSCOM but it seems like it won't work. Format may re-format the data to an ASCII string representation instead.

Alternatively, you can just use any serial port as a 'file' in Windows. Look at the windows api for opening files and you will see that you can address certain devices as files by using a filename like 'COM1:' instead.

Then, you can read/write from it like a file.

sybreon
  • 3,128
  • 18
  • 19