More seriously, you never actually allocated memory to store the characters. If any are received, they will be written at some random place in memory, identified by the uninitialized value of the variable buf
.
You probably want something more like
int main(int argc, char **argv) {
char buf[256];
int res = OpenComport(16, 9600);
printf("Open port result: %d\n\n\n", res);
while(1) {
int n = PollComport(16, buf, sizeof(buf)-1);
printf("n: %d\n", n);
sleep(1);
}
CloseComport(16);
}
There are a couple of other nits. First, for a hosted program it is accepted practice to use the full signature of main()
. That makes it easier to get command line arguments and report success or failure to the shell. This is just a throw-away, so its less important here. Second, you declared the variable buf
in a place that is legal C99, but not legal C89. A large corpus of code is still C89, and you should generally be careful about assuming a C99 environment. Notably the Microsoft compilers have been slow to keep up with standards. You don't provide a way to select the serial port, having hardwired the reference to ttyUSB0 with the code 16. This would be a problem on Windows.
I also notice that this library is rather silent on the other kinds of configuration one might want to perform on a serial port. It lets you specify baud rate, but not byte width or parity. My real concern is that it doesn't mention hardware or software handshake, and for example the Windows COM ports generally default to assuming that hardware handshake will be used. I don't happen to know how what handshake and other configurations that linux ttys default to, but there will be defaults. IIRC, the Arduino boards (like most embedded systems) don't bother with the hardware handshake wires, so that could be the whole reason.
Some things to try include:
- Can you talk to the board with Hyperterm on Windows or some terminal emulator on linux?
- What settings did you use?
- Does the program start working after talking to the board with a terminal emulator?
Edit: Missed the "linux" tag on first reading, so I've edited it to tone down the assumption that the OP has Windows available. Although all the details about serial ports differ between the two platforms, the need for complete configuration to match the other end of the wire remains the same.