1

I am trying to make an interface with another program so I have to use C++.

It's been years since I have programmed in C++ and I have been at this problem for about a week so I'm slowly starting to see how everything works.

I want to read byte data coming from a serial port device.

I have verified that I can get text through the serial port using the readline command:

For example:

String^ message = _serialPort->Readline();

Is how the data is read in an example from MSDN that I got to work successfully.

However I have tried to modify it several times and I'm having no luck coming up with something that reads the data as bytes. (I already have conversion of byte data to string so I can actually see the bytes such as the number 15 equaling 0f in bytes.)

Modifying the code to

wchar_t message = _serialPort->Readline();

gives me

error c2440: 'initializing' : cannot convert from System::String ^' to 'wchar_t'.

I'm not familiar with Readline. Is it only for strings? I have verified that it does work with strings and if I use a serial device that sends a string the first set of code does work.

Can someone explain what method I could use to read byte data? Thanks.

Charles
  • 50,943
  • 13
  • 104
  • 142
newb_k1dd
  • 21
  • 2
  • 3
  • How is _serialPort defined? Why are trying to convert output to wchar_t when you want bytes? What is System::String^ ? – ravenspoint Apr 14 '11 at 19:48
  • Without diving to the depths of the mechanism, Readline() returns a _line_, not a _character_, so assigning it to a character won't work. Secondly, it clearly returns a `System::String ^` so you should probably be using that instead. – Assaf Levy Apr 14 '11 at 19:52
  • I see from sehe's answer that this is not C++. Please fix question title and tags. – ravenspoint Apr 14 '11 at 19:54

2 Answers2

1

Update

Pure C++ Win32 API versions:

See the following good references

Is there any specific reason you are doing this in C++/CLI code?

I thought you might not even be aware of that (otherwise, tag your questions, please).

String^, Readline etc are CLR functions (i.e. .NET, think: "you could do this more easily in C#). So, again,

  • If there is a need for this to be in C++, why don't you look at the native Win32 API
  • Otherwise, why are you bothering with C++

If you really wanted C++/CLI I suggest not mixing native/managed code when handling the serial IO. You can get an UnmanagedMemoryStream to marshal the data in/out of managed land.

$0.02

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    I don't think he uses C++/Cli on purpose. He just started a new Visual C++ project from VS and Clr option was selected by default. – ali_bahoo Apr 14 '11 at 22:27
  • I know it's in C++/CLI.I was trying to tag all that but I ran into the 5 tag issue and just assumed that would not be a big deal. **I would like to do it in pure C++ but I'm not familiar with how to do that. I'm using Visual Studio 2010 Professional.** I'm working from the example in MSDN: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx – newb_k1dd Apr 15 '11 at 13:11
  • Updated answer with relevant links – sehe Apr 15 '11 at 13:33
  • I'm not sure what the problem is but I got the CodeProject source to work with Visual Studio 2010 but I can't read any data from that program. I tried to input to text with hyperterminal and the serial device itself but I'm not getting anything in that program. – newb_k1dd Apr 15 '11 at 15:55
  • 2
    @newb_k1dd: C++/CLI is not a bad way to get a GUI up and running with C++, but I suggest using talking to the serial port using the native APIs, either direct use of Win32 Communications Functions, or a wrapper as ildjarn suggested. The .NET SerialPort class caused nothing but grief when I tried it. – Ben Voigt Apr 15 '11 at 23:07
  • What's a good reference for the API? I've been reading through some MSDN links and some random sites but I'm not finding anything that's very straight-forward. – newb_k1dd Apr 19 '11 at 12:37
  • I personally can't think of a better reference than the MSDN link. However, if you're looking for pointers to the individual functions, [start here](http://msdn.microsoft.com/en-us/library/aa363140(v=VS.85).aspx)? – sehe Apr 19 '11 at 13:01
  • Okay will any of this allow me to read bytes of data? I'm just looking at multiple sources and trying to get some code to work with Boost right now and having issues with that library. – newb_k1dd Apr 19 '11 at 16:22
  • bytes? yes. boost: you should be able to use [boost.asio](http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/overview/serial_ports.html) instead with generic iostream library: see also [this previous answer](http://stackoverflow.com/questions/5675358/how-to-send-ostream-via-boost-sockets-in-c/5675952#5675952) – sehe Apr 19 '11 at 17:09
1

If you actually want to use C++ rather than C++/CLI, I recommend using boost.asio. It is well established, relatively easy to understand, and has a specific set of functionality just for working with serial ports.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • I am downloading that now and will be taking a look at it. – newb_k1dd Apr 15 '11 at 13:20
  • I'm trying to use Boost but I'm having a lot of trouble installing this on a Windows XP computer with Visual Studio 2010. It won't properly link the libraries. I'm still attempting this along with some other solutions but if you have any advice about this install that would be great. I've tried the zip and the executable. – newb_k1dd Apr 19 '11 at 12:40
  • @newb_k1dd : Are you following the [Getting Started Guide](http://www.boost.org/doc/libs/release/more/getting_started/windows.html)? – ildjarn Apr 19 '11 at 13:47
  • Yes. I finally got it working, I had referenced one folder up from the actual folder. I'm trying the example http://www.webalice.it/fede.tft/serial_port/serial_port.html but this also uses CLI, I'm trying to find a straight-forward example I'm just running in circles with CLI showing up everywhere and a bunch of other things I don't need. – newb_k1dd Apr 19 '11 at 14:50