Comments by contributor Fran Jarnjak
Attached is the sample code. It is very straight
forward and it should work on any Unix system - for example, Linux, Mac OS X, Solaris,
etc..since I used standard Unix system calls and POSIX library. I tested it at home on my
Mac though.
Code creates an object that opens serial port and then a user can
read and write bytes to it. It expects write followed by a read sequence which is OK for
your PLC native protocol and Modbus which are request/response protocols. It gives about
0.2 seconds for PLC to prepare reply to a message.
Click here to download project files: SerialMacExample.tgz
Macintosh:~/Development/SerialExample
fjarnjak$ ./a.out
/dev/tty.PL2303-000014FD
Current in baud rate: 9600 bps
Current out baud rate: 9600 bps
Current c_cflag= 19200
Current c_lflag= 0
Current c_iflag= 0
Current c_oflag= 0
After setting:
Current in baud rate: 38400 bps
Current out baud rate: 38400 bps
Current c_cflag= 35584
Current c_lflag= 0
Current c_iflag= 1
Current c_oflag= 0
Enter msg to send:@01IR0000*
To be written to Serial port:
buff[0]: 64
buff[1]: 48
buff[2]: 49
buff[3]: 73
buff[4]: 82
buff[5]: 48
buff[6]: 48
buff[7]: 48
buff[8]: 48
buff[9]: 42
buff[10]: 13
11 bytes written.
11 bytes read.
Response from Serial port (11 bytes):
readBuff[0]: @ (int=64)
readBuff[1]: 0 (int=48)
readBuff[2]: 1 (int=49)
readBuff[3]: I (int=73)
readBuff[4]: R (int=82)
readBuff[5]: 0 (int=48)
readBuff[6]: 1 (int=49)
readBuff[7]: 5 (int=53)
readBuff[8]: B (int=66)
readBuff[9]: * (int=42)
readBuff[10]: = 13 (\r)
Enter msg to send:quit
Quit entered
Exiting