I do not understand the situation ... Is there a TWO processes on ONE computer that should exchange information? And what side is the serial port here ?! Or, after all there are TWO companies that are connected via a serial bus?
If your processes are executed (physically) on one computer, then using the COM port is meaningless. What for ?!!! You absolutely started to dig in the direction of "linux interprocess communication", but you absolutely do not need to use mutexes - too low and a primitive type of interaction between processes.
I think that for you, if not a very experienced programmer, it is clearer (and therefore easier) to use the so-called. "named pipes".
1) Create a named pipe
mkfifo pipe1
2) In the wizard, open this channel to record
open("pipe1", O_WRONLY)
3) In the slave - open for reading: open ("pipe1", O_RDONLY)
4) After that, in the master use the usual write (...);?, In the slave - read (...);
5) All worries about error checking, synchronization, etc. takes over the OS.
As a WORKING EXAMPLE, here are the listings of two tiny programmers, one of which writes a line to the channel, and the other reads it.
So, the first action is creating a channel:
mkfifo exampl
The second step is to write and compile a reader from the channel:
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { FILE *ff; char line[256]; ff = fopen("exampl", "ro"); if (ff) { fgets(line, 255, ff); line[strlen(line)-1] = '\0'; printf("ΠΡΠΎΡΠΈΡΠ°Π»ΠΈ ΠΈΠ· ΠΊΠ°Π½Π°Π»Π°: '%s'\n", line); fclose(ff); } else perror("ΠΡΠΈΠ±ΠΊΠ° ΠΎΡΠΊΡΡΡΠΈΡ FIFO:"); }
The third step is to write and compile a channel recording program:
#include <stdio.h> int main(int argc, char *argv[]) { FILE *ff; ff = fopen("exampl", "wo"); if (ff) { fprintf(ff, "ΠΠΈΡΠ΅ΠΌ Π² ΠΊΠ°Π½Π°Π»\n"); fclose(ff); } else perror("ΠΡΠΈΠ±ΠΊΠ° ΠΎΡΠΊΡΡΡΠΈΡ FIFO:"); }
Last step - we test the received programs:
$ ./main_r & [1] 6096 $ ./main_w ΠΡΠΎΡΠΈΡΠ°Π»ΠΈ ΠΈΠ· ΠΊΠ°Π½Π°Π»Π°: 'ΠΠΈΡΠ΅ΠΌ Π² ΠΊΠ°Π½Π°Π»' [1]+ ΠΠΎΡΠΎΠ²ΠΎ ./main_r
Since I used only one terminal, I started the reader in the background (&). She hung on waiting for input. Launched a program to write to the channel. She wrote down the line and ended. The reading program took a line from the channel and ended.
What could be easier ?!