Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Upgrading COM1 to RS-232 code to COM1 to USB to RS-232 code / Win8 XE6


This question is answered.


Permlink Replies: 3 - Last Post: Nov 26, 2014 2:54 PM Last Post By: Boba ½° Threads: [ Previous | Next ]
Nicholas Gessler

Posts: 20
Registered: 1/25/98
Upgrading COM1 to RS-232 code to COM1 to USB to RS-232 code / Win8 XE6  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 18, 2014 2:12 PM
Problem 1:
I am trying to upgrade code, written to talk to an RS-232 device on COM1, to code that will talk to the same device via TRENDNET's USB to RS-232 Converter, TU-S9 v2.02.
The device is a vintag EZIO analog-to-digital / digital-to-analog converter.
TRENDNET offers no theory-of-operation or examples of how to do this.
Is their device supposed to be transparent, and route all code to COM1 through the USB to the RS-232 D9 connector on the other end?
Or must I now recode everything in a secret language that their device can understand, a language they do not disclose?

Problem 2:
Possibly related to Problem 1...
Although the code example below works on WIndows 7 Enterprise, it Windows 8 it gives the errors shown below.
Our new Dell machines have only USB ports and no COM ports.
However, Embarcadero does not appear to be concerned about that fact.
Embarcadero is complaining that it wants 'const wchar_t *', but got 'const char *'
Since I do not have access to WIN API functions, I'm not sure it's in my power to make the change that it is requesting.
How do I "fix" these two errors?

With thanks, Nick

//===========================================================================
// COM1 COMMUNICATIONS MANAGEMENT: ON FORM CREATE
//===========================================================================
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DCB dcbCommPort;

// OPEN THE COMM PORT.
hComm = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
EMBARCADERO OBJECTS TO "COM1" AS FILE NAME
[bcc32 Error] Unit1.cpp(49): E2034 Cannot convert 'char const[5]' to 'const wchar_t *'
Full parser context
Unit1.cpp(39): parsing: void _fastcall TForm1::FormCreate(TObject *)
[bcc32 Error] Unit1.cpp(49): E2342 Type mismatch in parameter 'lpFileName' (wanted 'const wchar_t *', got 'const char *')
Full parser context
Unit1.cpp(39): parsing: void _fastcall TForm1::FormCreate(TObject *)

// IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE) Application->Terminate();

// SET THE COMM TIMEOUTS IN OUR EXAMPLE.
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 10;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);

// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("57600,N,8,1", &dcbCommPort); // line 66
SetCommState(hComm, &dcbCommPort);
}

EMBARCADERO OBJECTS TO "57600,n,8,1"
[bcc32 Error] Unit1.cpp(66): E2034 Cannot convert 'char const[12]' to 'const wchar_t *'
Full parser context
Unit1.cpp(39): parsing: void _fastcall TForm1::FormCreate(TObject *)
[bcc32 Error] Unit1.cpp(66): E2342 Type mismatch in parameter 'lpDef' (wanted 'const wchar_t *', got 'const char *')
Full parser context
Unit1.cpp(39): parsing: void _fastcall TForm1::FormCreate(TObject *)

~~~~~ end of posting ~~~~~
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Upgrading COM1 to RS-232 code to COM1 to USB to RS-232 code / Win8 XE6
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 18, 2014 4:05 PM   in response to: Nicholas Gessler in response to: Nicholas Gessler
Nicholas wrote:

Is their device supposed to be transparent, and route all code to COM1
through the USB to the RS-232 D9 connector on the other end?

You have to ask TrendNet about that. It might be transparent, or it might
not be. For instance, in one of my projects, I have to communicate with
a serial device connected through a TCP/IP adapter, and that adapter wraps
the serial data with the Telnet protocol. So I have to read and parse Telnet
data to get at the serial data, and vice versa when sending data to the serial
device.

Or must I now recode everything in a secret language that their device
can understand, a language they do not disclose?

Again, you have to ask them about that.

Our new Dell machines have only USB ports and no COM ports.

However, Embarcadero does not appear to be concerned about that fact.

Why should they be? It is not their problem to be concerned about.

Embarcadero is complaining that it wants 'const wchar_t *', but got
'const char *'

That is a bug in your code, not Embarcadero's code. You are calling a Unicode-based
API but passing ANSI data to it. Your code was clearly written for a pre-Unicode
version of C++Builder and has not been updated to handle Unicode correctly.

Since I do not have access to WIN API functions

Yes, you do. You are calling Win32 API funtions in the code you showed.

I'm not sure it's in my power to make the change that it is requesting.

Yes, it is.

How do I "fix" these two errors?

CreateFile() and BuildCommDCB() map to CreateFileW() and BuildCommDCBW()
(Unicode) or to CreateFileA() and BuildCommDCBA() (Ansi) depending on whether
UNICODE is defined, which in turn is controlled by the "TCHAR maps to" setting
in the Project Options. Clearly, your "TCHAR maps to" setting is set to
"wchar_t". If you want to pass hard-coded string literals to CreateFile()
and BuildCommDCB() (or any other Win32 API function that takes TCHAR-based
parameters), you need to either:

1. set "TCHAR maps to" to "char" (but this has a global effect on your app.
Modern apps should be using "wchar_t"), then you don't have to make any
changes to your code.

2. don't change the "TCHAR maps to" value, and use the TEXT() macro so your
code adapts to whatever "TCHAR maps to" happens to be set to (use this only
if you need to support Win9x/ME):

hComm = CreateFile(TEXT("COM1"), ...);
...
BuildCommDCB(TEXT("57600,N,8,1"), ...);

3. call Create(A|W)() and BuildCommDCB(A|W)() directly instead (this is what
you should be using in modern code):

hComm = CreateFileA("COM1", ...);
...
BuildCommDCBA("57600,N,8,1", ...);


hComm = CreateFileW(L"COM1", ...);
...
BuildCommDCBW(L"57600,N,8,1", ...);

On a side note, do not use the TForm::OnCreate event in C++. It is a Delphi
idiom that causes illegal behavior in C++. Use the TForm1 constructor instead.

--
Remy Lebeau (TeamB)

Nicholas Gessler

Posts: 20
Registered: 1/25/98
Re: Upgrading COM1 to RS-232 code to COM1 to USB to RS-232 code / Win8 XE6  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2014 4:39 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy.

Embarcadero is happy.
The code now compiles and runs.

However, communications are not getting through
the USB to RS-232 adapter to the device downstream.
I'll have to take that up with the manufacturer.

Nick
Boba ½°

Posts: 73
Registered: 12/19/03
Re: Upgrading COM1 to RS-232 code to COM1 to USB to RS-232 code / Win8 XE6  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 26, 2014 2:54 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
"Remy Lebeau (TeamB)" wrote in
message news:702348 at forums dot embarcadero dot com...
... do not use the TForm::OnCreate event in C++.
It is a Delphi idiom that causes illegal behavior
in C++...

sorry for jumping in, Gambit, but I think - since it's
been'a so long you've tought'em this particular lesson
for the 1st time - it would sound better if you could
add to your phrase above: 'if used improperly'. I bet
you don't have a problem with using the TForm::OnCreate
event in C++, neither does anyone familiar with both
Pascal and C from Borland; so why should beginners
like the OP get 'scared off' by an expert like you?
Let'em try, fail, experiment and enjoy the results.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02