views:

375

answers:

2

I have ported our project from Delphi 7 to Delphi 2010. After adding some type casts now my project is running well and all the features work correctly except main functionality of the program which is bound to COM ports and the MSCOMM ActiveX component.

It can read and write from a COM port but it seems something is wrong with that because the device that connected to he port returns error code although it works correctly with same code in Delphi7. In the code you can see below after sending some bytes to the device a byte is sent as checksum. On the other side the device gets the bytes and calculates check sum with same formula if received check sum was equal with calculated check sum device returns "O" as OK else it returns "E" as Error. OutCom is defined as Olevariant.

OutCom := chr(ord(Fbyte));
Mscomm1.Output := OutCom;

OutCom := chr(ord(Sbyte));
Mscomm1.Output := OutCom;

OutCom := chr(ord(DigitOne));
Mscomm1.Output := OutCom;
OutCom := chr(ord(DigitTwo));
Mscomm1.Output := OutCom;
OutCom := chr(ord(DigitThree));
Mscomm1.Output := OutCom;
SumOfBits := (System_No - 1) + Fbyte + Sbyte + DigitOne + DigitTwo + DigitThree;

CheckSum := ( (SumOfBits mod 256) xor 255 ) + 1;

OutCom := chr(ord(CheckSum));
Mscomm1.Output := OutCom;

OutCom := 'E';
Mscomm1.Output := OutCom;
OutCom := 'N';
Mscomm1.Output := OutCom;

Same code in Delphi 2010 returns different result. I guess that the chr() function returns a different result as Delphi7. If this is true then how can I get the Ascii char of a byte or is there a way to pass a byte to Mscomm without converting to char?

+2  A: 

Chr now returns a Unicode WideChar (pre-Delphi 2009, it returned a single-byte AnsiChar).

The easiest solution is to typecast byte values to ansischar :

var_ansichar := AnsisChar(var_byte);

instead of using Chr(). But note what Mghie says. Make sure the typing of your components, declared variables etc is right (AnsiChar, AnsiString etc).

Marco van de Voort
Also rmember that character consts now by default return Unicode characters. See this blow entry http://wiert.wordpress.com/2010/01/18/delphi-highcharunicode-directive-delphi-rad-studio/ and this newsgroup thread https://forums.embarcadero.com/thread.jspa?threadID=31260 for more background information.
Jeroen Pluimers
+1  A: 

Since you still didn't post any information about the data types used I won't go into the problems your code may have, but I think too that the char type being two bytes wide in Delphi 2009+ is the most probable reason for your code no longer working correctly.

However, if I had that problem I would definitely check first what bytes are sent over the serial connection. For that you can use Portmon for Windows, a SysInternals tool that uses the filtering API of the drivers for parallel and serial interfaces, and is thus able to display all API functions called and all data bytes sent and received. Run this with only the COM port you are interfacing over enabled, and check the data sent from the PC to the external device. If you see a lot of $00 bytes between your data bytes you are sending wide chars instead of Ansi chars, and correcting the data types should fix things.

mghie
Thank for your help. I used the Portmon and the result is that chr() function returns a wrong value for those values that are larger than 128. So i used Ansichar() instead of chr() and every thing went OK.
m-abdi