libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

modbus_receive() function returns -1 on USB serial port (TTL level) devices

Open ZhiyuanYuanNJ opened this issue 7 months ago • 1 comments

  • Which operating system are you using? Windows 10 x64

  • Describe the exact steps which reproduce the problem in as many details as possible. For example, the software/equipment which runs the Modbus server, how the clients are connected (TCP, RTU, ASCII) and the source code you are using. Modbus RTU I'm using modbus rtu, my code runs fine on a virtual serial port, but when I change the serial port to an actual TTL level USB serial port, the modbus_receive() function always returns -1.

  • Enable the debug mode, libmodbus provides a function to display the content of the Modbus messages and it's very convenient to analyze issues (http://libmodbus.org/docs/modbus_set_debug/).

The following is the debug report: .\RemoteGPIOControl.exe : ERROR timed out: select 所在位置 行:1 字符: 1

  • .\RemoteGPIOControl.exe COM37 COM70 2>out_1.txt
  •   + CategoryInfo          : NotSpecified: (ERROR timed out: select:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError
    
    

My Code Process:

// init 
modbus_new_rtu
modbus_mapping_new_start_address
modbus_set_slave
modbus_connect
do {
	ret = modbus_receive(MBInfo.mb, query);     //always return -1
	printf("ret == %d\n", ret); 
      } while (ret == 0);
.....

ZhiyuanYuanNJ avatar Jul 06 '24 02:07 ZhiyuanYuanNJ