Author Topic: fwrite() and fread() to/from serial port on a PC  (Read 14110 times)

Joe0x7F

  • Member
  • ***
  • Posts: 12
fwrite() and fread() to/from serial port on a PC
« on: February 07, 2012, 06:37:11 am »
I am trying to emulate (reverse engineer) another program that is reading and writing to the COM4 serial port (connected to another device) with just ANSI C calls like fopen(), fread(), and fwrite(), etc.

The first read or write is a fwrite() and my software protocol analyzer shows that I am writing OK with the bytes expected and a 0 success code.

When I then do a rewind(pH) followed by a fread(buffer, 1, 1, pH), the 1st byte shown in the protocol analyzer is the one I want and expect, BUT it is followed by several other unexpected bytes and a timeout (0x00000102).

Any ideas of what is happening or what I can try? 

I really need this C code to be as skeletal and portable as possible, NOT C++ or .NET specific.

Thanks!

Joe
-------------



Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: fwrite() and fread() to/from serial port on a PC
« Reply #1 on: February 07, 2012, 10:46:44 am »
Are you documenting exactly what the other program sends to the device from startup and sending the same thing from your program?

Jan

Joe0x7F

  • Member
  • ***
  • Posts: 12
Re: fwrite() and fread() to/from serial port on a PC
« Reply #2 on: February 07, 2012, 11:12:23 am »
Are you documenting exactly what the other program sends to the device from startup

Yes.

and sending the same thing from your program?

No.

I was hoping to stay as generic as possible, but if you are implying what I think you are implying, then I need to move to the final platform to see what port setup APIs I have available to me there. It's just a clumsier, slower development environment. Also, you seem to be implying that fread() and fwrite() should work if the setup tasks are performed correctly on the particular platform.

Thanks!

Joe
---------

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: fwrite() and fread() to/from serial port on a PC
« Reply #3 on: February 07, 2012, 11:31:21 am »
I wasn't asking if you were using the same APIs, just whether the protocol analyzer shows the same thing whether using the existing application or the one you're writing. Because if you want to reverse-engineer the application, you'll probably need to emulate the application exactly as far as what's going out on the bus.

For example, maybe you're sending a command that you've seen the other application send, but you haven't sent a setup or configuration command that the application sends before sending the other command. So you need to watch what the application does and emulate it, using whatever software you want as long as the bus traffic that results is the same.

Jan

Joe0x7F

  • Member
  • ***
  • Posts: 12
Re: fwrite() and fread() to/from serial port on a PC
« Reply #4 on: February 07, 2012, 11:34:05 am »
Thanks Jan. This is very helpful.

Best,

Joe
--------