Go Down

Topic: Serial Transmission problems. (Read 835 times) previous topic - next topic

pamam

Scenario

1.  Arduino 2560 with serial Port 6 Receiving data from my laptop on Serial Port 6 on laptop.
2.  Arduino is ;pushing results of receipt to Serial Monitor on Port 5.
3. All ports are set up the same with speed of 9600 baud, and all the other stuff.

Laptop code which is C# which is close enough to C, C++ I think for most to understand.

Code: [Select]

string[] Data = PrepareDataToSend(Mode);
if (Data != null)
{
//Send this to prepare for the data for this mode.
string[] modes = { "MODE", Mode };
for (int i = 0; i < 2; i++)
{
serialPort1.Write(modes[i]+"\n");
}

//Send the data for the Mode sent above.
for (int i = 0; i < Data.Length; i++)
{
serialPort1.Write(Data[i]+"\n");
// Application.DoEvents();
// Thread.Sleep(150); ;
}
}



4.  There a 5 Modes of operation, each mode has a certain number of Strings to process: usually in groups of 4 with the first one being a Name the last three are values.  There is one group of just two, a Name and a Value.

5.  Each mode has the name UDCLK with 3 values in it.

Testing reveals that the first Mode always passes and processes the Name , UDCLK, and 3 values, while the four Modes after that all fail on the very same Name of UDCLK.

I added the Sleep() into the equation, see commented out code, and now the first Mode fails and the four modes after it now all pass.

I noticed a larger hesitation right before the UDCLK, when the process got out of sync.

6.  There is one other thing I'm not sure about since it is in the Arduino code.  That is the following code.

Code: [Select]

void loop()
{
  // Convert my names to "int" values because of the switch statement not accept strings
  String Names = "MODECRegDFWFTW1FTW2FSKRROSKIMOSKQMOSKRRPA1PA2QDACUDCLKRefCLK";
  //                      012345678901234567890123456789012345678901234567890123456789
using the same constant width char font the numbers line up with the start of the strings.
  // if there's any serial available, read it:
  while (Serial1.available() > 0)
  {
    Name = Serial1.readStringUntil('\n');
    Serial.print("Name = ");Serial.println(Name);
   pos = Names.indexOf(Name, 0);=====================Question about at end.
    Serial.print("pos = ");Serial.println(pos);
//    switch ( pos )
//    {
//      // look for the next valid String in the incoming serial buffer.
//      case 0:
//        Mode = Serial1.readStringUntil('\n');
//        Serial.print(" Mode = "); Serial.println(Mode);
//      break;
//      case 4:
//        CRAddress = Serial1.readStringUntil('\n');
//        CRData = Serial1.readStringUntil('\n');
//        CRLength = Serial1.readStringUntil('\n');
//        Serial.print(" CRAddress = "); Serial.println(CRAddress);
//        Serial.print(" CRData = "); Serial.println(CRData);
//        Serial.print(" CRLength = "); Serial.println(CRLength);
//      break;
//      case 8:
//        DFWAddress = Serial1.readStringUntil('\n');
//        DFWData = Serial1.readStringUntil('\n');
//        DFWLength = Serial1.readStringUntil('\n');
//        Serial.print(" DFWAddress = "); Serial.println(DFWAddress);
//        Serial.print(" DFWData = "); Serial.println(DFWData);
//        Serial.print(" DFWLength = "); Serial.println(DFWLength);
//      break;     
//      case 11:
//        FTW1Address = Serial1.readStringUntil('\n');
//        FTW1Data = Serial1.readStringUntil('\n');
//        FTW1Length = Serial1.readStringUntil('\n');
//        Serial.print(" FTW1Address = "); Serial.println(FTW1Address);
//        Serial.print(" FTW1Data = "); Serial.println(FTW1Data);
//        Serial.print(" FTW1Length = "); Serial.println(FTW1Length);
//      break;
//     
//      case 15:
//        FTW2Address = Serial1.readStringUntil('\n');
//        FTW2Data = Serial1.readStringUntil('\n');
//        FTW2Length = Serial1.readStringUntil('\n');
//        Serial.print(" FTW2Address = "); Serial.println(FTW2Address);
//        Serial.print(" FTW2Data = "); Serial.println(FTW2Data);
//        Serial.print(" FTW2Length = "); Serial.println(FTW2Length);
//      break;
//      case 19:
//        FSKRRAddress = Serial1.readStringUntil('\n');
//        FSKRRData = Serial1.readStringUntil('\n');
//        FSKRRLength = Serial1.readStringUntil('\n');
//        Serial.print(" FSKRRAddress = "); Serial.println(FSKRRAddress);
//        Serial.print(" FSKRRData = "); Serial.println(FSKRRData);
//        Serial.print(" FSKRRLength = "); Serial.println(FSKRRLength);
//      break;
//      case 24:
//        OSKIMAddress = Serial1.readStringUntil('\n');
//        OSKIMData = Serial1.readStringUntil('\n');
//        OSKIMLength = Serial1.readStringUntil('\n');
//        Serial.print(" OSKIMAddress = "); Serial.println(OSKIMAddress);
//        Serial.print(" OSKIMData = "); Serial.println(OSKIMData);
//        Serial.print(" OSKIMLength = "); Serial.println(OSKIMLength);
//      break;
//      case 29:
//        OSKQMAddress = Serial1.readStringUntil('\n');
//        OSKQMData = Serial1.readStringUntil('\n');
//        OSKQMLength = Serial1.readStringUntil('\n');
//        Serial.print(" OSKQMAddress = "); Serial.println(OSKQMAddress);
//        Serial.print(" OSKQMData = "); Serial.println(OSKQMData);
//        Serial.print(" OSKQMLength = "); Serial.println(OSKQMLength);
//      break;
//      case 34:
//        OSKRRAddress = Serial1.readStringUntil('\n');
//        OSKRRData = Serial1.readStringUntil('\n');
//        OSKRRLength = Serial1.readStringUntil('\n');
//        Serial.print(" OSKRRAddress = "); Serial.println(OSKRRAddress);
//        Serial.print(" OSKRRData = "); Serial.println(OSKRRData);
//        Serial.print(" OSKRRLength = "); Serial.println(OSKRRLength);
//      break;
//      case 39:
//        PA1Address = Serial1.readStringUntil('\n');
//        PA1Data = Serial1.readStringUntil('\n');
//        PA1Length = Serial1.readStringUntil('\n');
//        Serial.print(" PA1Adress = "); Serial.println(PA1Address);
//        Serial.print(" PA1Data = "); Serial.println(PA1Data);
//        Serial.print(" PA1Length = "); Serial.println(PA1Length);
//      break;
//      case 42:
//        PA2Address = Serial1.readStringUntil('\n');
//        PA2Data = Serial1.readStringUntil('\n');
//        PA2Length = Serial1.readStringUntil('\n');
//        Serial.print(" PA2Address = "); Serial.println(PA2Address);
//        Serial.print(" PA2Data = "); Serial.println(PA2Data);
//        Serial.print(" PA2Length = "); Serial.println(PA2Length);
//      break;
//      case 45:
//        QDACAddress = Serial1.readStringUntil('\n');
//        QDACData =    Serial1.readStringUntil('\n');
//        QDACLength =  Serial1.readStringUntil('\n');
//        Serial.print(" QDACAddress = "); Serial.println(QDACAddress);
//        Serial.print(" QDACData = ");    Serial.println(QDACData);
//        Serial.print(" QDACLength = ");  Serial.println(QDACLength);
//      break;
//      case 49:
//        UDClkAddress = Serial1.readStringUntil('\n');
//        UDClkData = Serial1.readStringUntil('\n');
//        UDClkLength = Serial1.readStringUntil('\n');
//        Serial.print(" UDClkAddress = "); Serial.println(UDClkAddress);
//        Serial.print(" UDClkData = "); Serial.println(UDClkData);
//        Serial.print(" UDClkLength = "); Serial.println(UDClkLength);
//      break;     
//      case 54:
//        RefClockMultiplier = Serial1.readStringUntil('\n');
//        delay(100);
//        Serial.print(" RefClockMultiplier = "); Serial.println(RefClockMultiplier);
//      break;
       )   
)
comment code should display in constant width chars.

quest: 1 -- As you can see I'm parsing the string to find a value to use as the "pos" in the case statement.  My question is how reliable is the "string.indexOf " with respect to finding a substring stating point starting the search always at pos = 0 ??
quest: 2 -- and more important I'm begging to think, do I need to stop the serial flow with some sort of ack response while I process the data. For instance at 57600 baud all I get printed on the monitor is "\\\////\\\\44%%^^&&*&^&\ etc".

If you know of a good link which handles handshake protocol and how to set it up I would appreciate it.
Thanks in advance for your efforts.
pamam


Robin2

#1
Jul 12, 2020, 10:04 pm Last Edit: Jul 12, 2020, 10:05 pm by Robin2
Quote
My question is how reliable is the "string.indexOf "
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).


Quote
1.  Arduino 2560 with serial Port 6 Receiving data from my laptop on Serial Port 6 on laptop.
A Mega does not have a Serial6. Serial3 is the highest number.

Quote
3. All ports are set up the same with speed of 9600 baud, and all the other stuff.
If you are receiving data on the Mega's serial ports and combining it to send to the PC it might be wise to use a much higher baud rate for communication with the PC. The Mega will happily work at 500,000 baud.

Please post the complete program, and not just snippets.

Please post some examples of the data that is being received and the data that is being sent.

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.
Two or three hours spent thinking and reading documentation solves most programming problems.

pamam

I thought I found the problem, but it is still not working as well. 
One of my Names was using UDClk instead of UDCLK I changed it. 

With the 150 millisecond delay all worked after that.  But when I tried it again at full speed, 9600 baud, without the delay it failed again adding the delay back in it worked again. 

But there is still a question about the speed.  Both ports run at the same speed, 9600 baud, so should I run the Laptop higher than the monitor that does the printing.  The monitor will not be used in the final version it is only used for debugging. 

With the readStringUntil() command it does not stop receiving data?  How big is the buffer and can I
change it, and How?

This is the output in fail.  UDCLK is the next string.  9600 baud both ports.  I'm not sneding any data just a "" string

13:31:52.367 -> Name = MODE
13:31:52.367 -> pos = 0
13:31:52.367 ->  Mode = 0
13:31:52.401 -> Name = CReg
13:31:52.401 -> pos = 4
13:31:52.401 ->  CRAddress = 488513312
13:31:52.435 ->  CRData =
13:31:52.435 ->  CRLength = 4
13:31:52.470 -> Name = FTW1
13:31:52.470 -> pos = 11
13:31:52.470 ->  FTW1Address = 11042563100175
13:31:52.503 ->  FTW1Data =
13:31:52.537 ->  FTW1Length = 6
13:31:52.537 -> Name = OSKQM
13:31:52.571 -> pos = 29
13:31:52.571 ->  OSKQMAddress = 8996
13:31:52.605 ->  OSKQMData =
13:31:52.605 ->  OSKQMLength = 2
13:31:52.638 -> Name = OSKIM
13:31:52.638 -> pos = 24
13:31:52.638 ->  OSKIMAddress = 8482
13:31:52.672 ->  OSKIMData =
13:31:52.706 ->  OSKIMLength = 2
13:31:52.706 -> Name = OSKRR
13:31:52.740 -> pos = 34
13:31:52.740 ->  OSKRRAddress = 37
13:31:52.774 ->  OSKRRData =
13:31:52.774 ->  OSKRRLength = 1
13:31:52.807 -> Name = PA1
13:31:52.807 -> pos = 39
13:31:52.807 ->  PA1Adress = 515
13:31:52.842 ->  PA1Data =
13:31:52.842 ->  PA1Length = 2
13:31:52.875 -> Name = QDAC
13:31:52.875 -> pos = 45
13:31:53.826 ->  QDACAddress = 9767
13:31:53.860 ->  QDACData =
13:31:53.860 ->  QDACLength = 2RefCLK
????? expecting    Name = UDCLK
                           UDCLKAddress =  59491648
                           UDCLKData  =
                           UDCLKLength = 4


This is the resulting printout at 115200 baud. Laptop speed ,Monitor speed 9600 still.

13:40:00.009 -> Name = ⸮ ⸮R⸮'⸮%⸮⸮
13:40:00.009 -> pos = -1



Thanks

pamam

Sorry about that of course I meant COM5 and COM6 not port.
Thanks for the catch.
pamam

pamam

Thanks about the "cstring" type in C++.  I was writing string but Arduino kept telling me String.  Maybe I should try and find my C++ book.
Thanks
Pamam

pamam

Robin2,

How do I add cstring to my sketch.  The link you gave me was confusing.  It did not tell me where to get the library.
 
I tried #include <cstring> "string.h" but it did not work I think I need the file since I don't have a cpp IDE that already has it.
Thanks,
pamam

pamam

I finally got it.  The Monitor needed to be speeded up since it was so slow at 9600 it was not done until after my next message came in to be processed.  I used 115200baud for Serial Monitor and 9600 for the COM 6 port.  Gave it plenty of time yet processed faster than ever before.

Thanks y'all  It's absolutely amazing what inputs from other contributors can accomplish.  Proves the old adage that more heads are better than one.

Pamam

pamam

I forgot to attach the list of completed outputs.  Please see attachment if interested.

pamam

Trying once more to upload the file "output from DDS Program.cpp"

Robin2

For short programs it is much more useful if you include the code in your Post using the code button </> so people don't have to download it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up