C linux: Serial communication lost some first bytes when receive data - c

I'm working on Linux system that using modbus rtu RS485 to send and receive data. My device is master and just send "request latest data"(8Bytes include 2Byte CRC) to slaver (from now is only 1 slaver) every 1second. when Slaver receive request, they will prepare data (71Bytes include CRC) and send back to Master. I can't see the source of Slaver because this is commercial product.
Both of Master and Slaver using same baud rate 38400.
Result:
When check communication between Master and Slaver, sometimes (average is 1-2hours) data from Slaver is lost some first Bytes, and first Byte that received has been modified with other value that sent from Slaver (sometime lost some first Bytes only)
Sometime data from Slaver not come (timeout but do not receive any data). I tried to increase timeout by 500ms or 1sec but still occurs without any change
I tested Slaver by communication with Teraterm and there is no error like above. Data that sent and received are OK. With Master, I also tested with Teraterm and there is no error.
When I try to catching data while Master and Slaver send & receive data, when problem are occurred(not receive any data or lost some first bytes) on both of Master side and on my PC side (try to catching data byte Teraterm on PC).
I believe that problem is on Master side, and maybe on serial port setting, but I don't know where were wrong. Please help.
Sorry for my poor English!

I used to work with RS485 bus a lot. And one problem that sometimes appeared was very similar to your. Because RS485 is half duplex bus, there is mechanism that switching receive mode and transmit mode on RS485 bus driver. And this was exactly cause of my problems.
When master device sent some data, slave was ready to reply (and replied) before bus driver (on master side) was switched to receive mode. This behavior ended with data loss.
May I suggest you to check, using oscilloscope, that slave sent data correctly? If so, you probably don't have too much options to do as possible solutions are:
Slave have to wait some time before sending reply to master.
Change HW, some RS485 driver that will be faster in switching modes or use different BUS.

Related

Avoid filling up netmap tx buffer on link failure

I'm currently using netmap to send and receive packets. It's working like a charm, but there is one thing causing me problems.
When I pull out the network cable, netmap is filling up the available slots in the tx ring. When I reconnect the cable, all the old packets are immediately transmitted. I don't want that to happen.
Is there any way to avoid the old packets from being sent?
I'm programming in C and the program is running on FreeBSD.

C UART not working all the time

I'm having an issue connecting a serial device to an embedded device I'm writing code for.
The device I am writing has two serial ports, an incoming from my laptop, and an outgoing to an external device.
When I connect both terminals to my laptop and view the data, I get exactly the data I am expecting.
When I connect my laptop to the external device directly, I am getting exactly what I expect, and a response.
When I connect the laptop and the external device to the embedded device I am working on, the laptop sends data to it, it receives it, it passes it on to the external device. This works as expected.
However, the external device doesn't send back the response.
If I send data to the external device from the embedded device, each new message I send allows it to send the original reply.
I know the first message got through correctly because the external device whirrs to live, and I know when it is sending the response by running and logic analyser on the tx/rx comms and viewing the traffic.
I considered that the embedded device is holding the rx line and preventing its transmission, but I don't see how that possible in the code. Also if that is the case it shouldn't work when I plug both lines into my laptop.
I also considered the DTR was not set high, but checked this and it appears to be set high.
Does anyone know a reason which would prevent a device from responding?
Note: When I say Serial Ports I am referring to the UART when referring to the embedded device. All device use a DB9 connector running RS232.
Edit: Operating System on laptop is Windows 10. Embedded device is a Atmega324p.
Edit 2: Did some more testing. It appears that it sometimes work and sometimes doesn't.
I have added an image which show a almost perfect signal of the response.
The blue section is a gap in the signal that shouldn't be there.
Ended up finding a solution.
The RTS line was held via the embedded device at 1.2v, while the Pc was holding it at 5.2v.
Pulling the RTS line up to 5v fixed the issue.

STM32 USB CDC did not work

I'm using stm32l151 and I want to use usb peripheral in cdc mode. I used stm32 cubeMx to generate the project. however the function CDC_Transmit_FS() always return USBD_BUSY. can anyone help me?
Here is my code:
while (1)
{
CDC_Transmit_FS(Buffer, 6);
HAL_Delay(2000);
}
I only added the above code to the project created by STM32cubeMX but it didn't work.
STM library is buggy and it stalls when you for example try to send many larger packets of data.
There is a problem with the windows drivers and there are quite long delays between packets. So 12MB is quite difficult to archive - using CubeMx I could only have about 3-4Mb (12MB when packet is sent).
Another problem I had - I needed to send some data to the device bofore I could transmit next packet (max 8kB in my case), otherwise it stalled randomly.
Fortunately, the problem is solved and I can receive data in the PC from the STM32. There was a problem in my terminal software. I used serial port manitor to monitor the received data from the serial port, however this serial monitor software do not open the serial port by itself and if any other software opens the serial port and receives the data, it monitors data receptions. I didn't know that.
Thanks

Xbee : Some device don't receive Broadcast

I use Xbee series 1 and Arduino Uno to control them. I put a code that analyse Rx and than send to another to get the "coordinator". All data are send in API mode to get all information.
I have a network of Xbee in my office (one per room) and I use another Xbee that broadcast some data. but sometimes, the Xbee in the same room can't send data instead of some that are in other room. The problem is that I need the information of all device in my field of area.
Is it possible that the fact the Arduino must send some data from other Xbee to the coordinator may cause him not to send the data he receive ?
Thank's , Thana
In fact, the buffer of an Xbee is limited but in my case, the Xbee receive 3 api message and than transfert a new message to the end one which is connected to a computer.
The problem is that i needed to slow down the speed of transmission due to the overflow of buffer.
My solution was sending the message after X sec where X depends on the RSSI so the channel was free and I see that every Xbee can now send what they see.
If someone is on the same case, he can reply on this to have my help

Measure the delay between data sent by udp server and received by the client

I want to check my udp server client application. One of the features that I wanted to check is the time delay between data sent by the server and received by the client and vice versa.
I figured out a way of sending a message from the server to the client and note the time. The client when receives this message sends the same message back to the server. The server gets this echoed message back and again notes the time. The difference between the time at which the message was sent and at which the echoed message is received back tells me the delay between data sent by the server and received by the client.
Is this approach correct?
Because I also foresee a lot of other delays involved using this approach. What could be a possible way to calculate more accurate delays?
Waiting for help.
Yes this is the most traditional way of doing ,you can do this.
You can see on sniffer, using relative time taken between sender's udp packet and receiver's udp packet. For the need of more accurate results, you have to go deep into the window's stack where it checks for udp packet received or not. And for calculation of timer's you can use a real time clock which gives upto microsecond delay. Also you are using udp which has high priority of packet getting lost, unlike tcp which is much reliable.
What stack are you using? LwIP ?

Resources