[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

Write HEX DATA to serial port on the PC

I am just getting started with serial port via Win32 API and I am stuck on the following:

I need to write Hex data to the serial port. So far I can write ASCII characters using:

WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)

but 'lpbuf' barks at me if I try anything else saying that it is expecting a "const void"

Hopefully this is cake for you experienced coders out there... I am working with C++ Builder6.
0
eceman
Asked:
eceman
  • 5
  • 2
1 Solution
 
jlsjlsCommented:
Just cast the pointer lpBuf to "const void"
WriteFile(hComm,(const void)lpBuf,dwToWrite,&dwWritten,&osWrite);
0
 
ecemanAuthor Commented:
Did not work jlsjls...but I may not be following what you suggested.
0
 
ecemanAuthor Commented:
To be more specific, if I do something like this:
WriteFile(hComm, 15 , 2 ,&iBytesWritten,NULL);

I get these errors:

[C++ Error] Unit1.cpp(59): E2034 Cannot convert 'int' to 'const void *'

[C++ Error] Unit1.cpp(59): E2342 Type mismatch in parameter 'lpBuffer' (wanted 'const void *', got 'int')
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jlsjlsCommented:
I suppose you intend to write 0x15 to the serial port (hComm).
You can do it as follow :
char buffer[1];
buffer[0] = 0x15;
WriteFile(hComm,(const void*)buffer,1,&iBytesWritten,NULL);
0
 
ecemanAuthor Commented:
That works! Thanks for taking the time to answer my qwestion jlsjls. I increased the points to 100 now.
How would you convert a user input like a string to something that stores the 0x15? If I try to do something like this:

buffer[1] = Edit1->Text.c_str();

I get this error:

[C++ Error] Unit1.cpp(69): E2034 Cannot convert 'char *' to 'char'

The basic idea is to be able to type a Hex number into an Edit box on the main form to provide access to different commands. Something like a StrToHex would work, I think, but I can only find a HexToStr function :(
0
 
ecemanAuthor Commented:
Help jlsjls !!!
0
 
ecemanAuthor Commented:
He did not follow up to the question.... :(
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now