WriteFile/ReadFile problem

WriteFile/ReadFile problem

While trying to write simple MODBUS Master and Slave applications, I ran into a problem. I tried to send an unsigned char array over RS232 using WriteFile. In the other application, I tried to read the same unsigned char array using ReadFile. I know I have to use pointers, and it works up to a point. I've boiled it down to what looks like a data type range problem, but it shouldn't be.

I began using a single usigned char, defined as:

unsigned char test = 0x01;

and sending the char as &test in the WriteFile. That works fine. When I increase the value to 0x80(128) the value of test on the ReadFile side is 0. Anybody have any clue what's going on here/ how to make this work? I need to be able to have a value of 0xff, and I thought an unsigned char would take care of that.

Happy to post any more code if needed. Thanks.

Chris

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

is VC working? would it be related to file type you're writing to?

if VC works, please provide a code snipet around the WriteFile, creating the file andclosing the file.

Jennifer

VC = Visual C? I'm a fairly new programmer, and I've just started working with C++ in the last month or so.

Opening:

char COM_PORT[] = "COM1";

printf("COM port being used is %s.nn", COM_PORT);

HANDLE hComm;
hComm = CreateFile( COM_PORT,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

Writing:

DWORD bytesWritten = 0;
int status;
unsigned char test = 0x80;

status = WriteFile(hComm, &test, 1, &bytesWritten, NULL);
printf("Status: %d; bytesWritten: %d;n", status, bytesWritten);

I don't have anything closing the file.

I opened the file in the same way in the slave application, then used a ReadFile.

Reading:

DWORD bytesWritten = 0;
int status;
DWORD transmitCheck;
unsigned char test;

status = ReadFile(hComm, &test, 1, &transmitCheck, NULL);

Icall the CreateFile in another function I called Open_Port.cpp. In the write portion, if I set test = 0x00, up to 0x7f, the ReadFile works fine. (I was setting the values usinghex to make iteasier for me to visualize the MODBUS code) When I get to test = 0x80 = 128, test is read in as 0 on the slave side.

My goal was to have an array of characters sent, with each character containing one byte of the MODBUS code to be sent.

Thanks,

Chris

is it working with vc?

Quoting - Jennifer Jiang (Intel)

is it working with vc?

what is vc?

vc is visual c++.
Right now the 11.0 supports Visual Studio .NET 2003, VS2005, VS2008. VS contains a C++ compiler visual c++ (cl.exe).

I am using visual c++ in VS .NET 2003. I have the 9.0 as well.

Quoting - cfann61

I am using visual c++ in VS .NET 2003. I have the 9.0 as well.

You need to set number of data and stop bits as well as parity and baud rate for serial port when you open it.

It seems to me that this problem of yours is totally unrelated to Intel Compiler.

-- Regards, Igor Levicki If you find my post helpfull, please rate it and/or select it as a best answer where applies. Thank you.

Leave a Comment

Please sign in to add a comment. Not a member? Join today