Two questions:

1. What function will output N bytes of data? The data may contain and may end with numeric zero.
2. What function will cause the process to suspend for N milliseconds?

An explanation for question 1.

struct modbus_structure {
            char device_address;
            char function_code;
            UINT function_address;
            UINT address;
            UINT checksum;
      };
 
struct modbus_structure send_buffer
...
status = some_fput_function( send_buffer, 8, Port_Handle );

As I imagine it, some_fput_function takes 3 arguments, the data, the number of bytes to output, and the handle to use to output the data. Is there a function that will effectively do this?

Late edit, BTW, this is C only, compiled under the LCC compiler. No windows or GUI code.

Thanks for your time,
bkelly

Dani AI

Generated

Short answer: use a binary-safe I/O call for the bytes and use an OS-specific sleep. was on the right track; is correct that the millisecond sleep is platform-dependent. Below are practical, safe patterns you can drop into C code.

For a FILE* stream (binary data):

/* write exactly nbytes from buf to stream */
size_t written = fwrite(buf, 1, nbytes, Port_Handle);
if (written != nbytes) { perror("fwrite"); /* handle error */ }
fflush(Port_Handle); /* push stdio buffers out */

For a POSIX file descriptor (or when writing to serial devices at the fd level) handle partial writes and optionally wait for transmission to complete:

ssize_t nw;
char *p = buf;
size_t left = nbytes;
while (left > 0) {
    nw = write(fd, p, left);
    if (nw < 0) { perror("write"); break; }
    left -= nw; p += nw;
}
/* if you need to block until the driver has sent everything: tcdrain(fd); */

Sleeping N milliseconds:

/* POSIX */
struct timespec ts = { ms/1000, (ms%1000)*1000000 };
nanosleep(&ts, NULL);

/* Win32 */
Sleep(ms);

Or a small portability wrapper using #ifdef _WIN32 to choose between Sleep and nanosleep.

Important cautions and troubleshooting:

  • Never use string functions (strcpy, puts, fprintf("%s")) for binary data; they stop at NUL.
  • Raw memory sizeof(struct) is fragile: compilers add padding and endian affects multi-byte fields. Serialize fields into a uint8_t buffer with fixed-width types (uint8_t/uint16_t) and defined byte order before sending.
  • Check return values and errno/GetLastError; use fflush/FlushFileBuffers/tcdrain as appropriate to ensure data leaves the OS buffers.
  • Millisecond sleeps on general OSes are not high-precision; for tight timing use real-time APIs or hardware timers.

These patterns work for common C toolchains (including LCC); adapt the low-level calls to the actual type of Port_Handle (FILE*, fd, or OS HANDLE).

Recommended Answers

All 2 Replies

you could use fwrite() assuming Port_Handle is the handle to an open file. -- see C docs for description of that function.

With regard to question #2, that would be implementation specific. If you are not on a hosted system, I would imagine that you can at least name the platform, which may have its own specifics (and manuals) to query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.