Announcement

Collapse
No announcement yet.

Serial comms (4D-CD-00051 and similar)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Serial comms (4D-CD-00051 and similar)

    Hi at all,
    Starting from the Staff CodeBase 51 (and similar) example, I'm trying to use com0 to talk to a PC (as debug) or a PIC (final purpose). The communication is simple, composed of a few characters and always ends with a <CR>. While TX works properly, I can't receive anything. I tried two different TTL / RS232 adaptation HWs, connected to a 4D-UPA, but the result does not change.
    Code:
    Main program:
    var BufferIn[10];       //20 bytes
    var CarRic;
    InitSer();
    and, in the main loop:
        RiceSer();
    is called as fast as possible.
        ScriviACaso();
    is called every time  I press the touch screen.
    
    Routines:
    func InitSer()
        com_Init(BufferIn, 20, 0);
        com_SetBaud(COM0, 960);
    endfunc
    
    func ScriviACaso()
        to(COM0); putstr("Hello from COM0\r");
        pause(10);
    endfunc
    
    func RiceSer()
        CarRic := com_Count();
        if (CarRic > 1)
            if (BufferIn[CarRic] == 13)
                print ("CR received\n");
                pause (10);
    //            do somethings
            endif
        endif
    endfunc
    Result:
    - ScriviACaso works properly
    - The value of CarRic is always at zero [I have monitored them with a simply "print ("CarRic=", com_Count(), " ") ;]", it seems I don't receive nothing...
    Have I miss something?
    Best regards, Valter

  • #2
    Hi Valter,

    The value of CarRic is always at zero [I have monitored them with a simply "print ("CarRic=", com_Count(), " ") ;]", it seems I don't receive nothing...
    If you try to print this on a specified location, you will receive the number of characters available on the buffer.
    Code:
    txt_MoveCursor(0,0);
    print(CarRic := com_Count());
    You need to introduce another buffer to store the bytes, and use the serin() function to release the character from the queue. You may do it this way.
    Code:
    func RiceSer()
        CarRic := com_Count();
        if (CarRic > 0)  // if there is a character received
            BufferStore[n]:=serin(); // remove the buffered byte on the circular buffer
    
            //monitor the incoming byte
            txt_MoveCursor(1,0);
            print("Byte: ",n, " = ", [CHR]BufferStore[n]);
    
            n++;
    
            if (BufferStore[13] == 13) // if the 14th byte is 13
                print ("CR received\n");
                pause (10);
            endif
        endif
    
    endfunc
    Best Regards,
    Kevin

    Comment


    • #3
      Hi Kevin,
      Thanks for the fast reply!
      I understood some errors in my code (like CarRic > 1 and others...), and, following you example, I wrote:
      HTML Code:
      Main program:
      var BufferIn[10];       //20 bytes
      var PBufferIn, CarRic;
      ......
          InitSer();
      ......
      Loop:
      ......
          RiceSer();
          txt_MoveCursor (21, 30);
          print ("Count=", com_Count(), " CarRic=", CarRic, ", PBuf=", PBufferIn, " , (", [CHR]BufferIn[PBufferIn], ")");
      ......
      
      Subroutines:
      func InitSer()
          com_Init(BufferIn, 20, 0);
          com_SetBaud(COM0, 960);
          PBufferIn := 0;
      endfunc
      
      func RiceSer()
          CarRic := com_Count();
          if (CarRic > 0)
              BufferIn[PBufferIn] := serin();
              txt_MoveCursor(1,0);
              print("Byte: ",PBufferIn, " = ", [CHR]BufferIn[PBufferIn]);
              PBufferIn++;
              if (BufferIn[PBufferIn] == 13)
                  txt_MoveCursor (0, 0);
                  print ("CR received\n");
                  pause (10);
                  PBufferIn := 0;
              endif
          endif
      endfunc
      The result in the display monitoring is always:
      Count=0, CarRic=0, PBuf=0, ()
      I use TeraTerm as terminal emulator and I try 2 different TTL to RS232 interfaces.
      I try also to shortcut pins RX0 and TX0 in 4D-UPA programmer (removing the interface to PC), I am sure the display transmits, but I don't read anything in reception.
      Other suggestion?
      Maybe a HW default in the display? (now I have ordered another to be sure).
      Best regards, Valter

      Comment


      • #4
        Hi Valter,

        Code:
        Count=0, CarRic=0, PBuf=0, ()
        I tested the code you attached, you should be able to see the PBuf incrementing. The Count and CarRic will always be '0' because you are removing the byte on a circular buffer using serin() command.

        Maybe you could replace this code on your RiceSer() function, to check and test the output.

        Code:
            CarRic:=com_Count();
            if(CarRic > 0)
                txt_MoveCursor(0,0);
                print("Characters on buffer: ",CarRic);
            endif
        
            if(com_Full()) // this will print the buffer when it is full (if 20 bytes is received)
             txt_MoveCursor(5,0);
            putstr(BufferIn);
        
            endif
        I use TeraTerm as terminal emulator and I try 2 different TTL to RS232 interfaces.
        Maybe the problem lies in the utility that you are using? You can always use the Terminal application (just use the Terminal 9600, since you are using that baud rate), located on Tools Tab.

        You need to introduce another buffer to store the bytes
        Also, what I mean by this is you create another buffer to store the data (which is what I used on the code I attached) - var BufferStore[20];

        I hope this helps.

        Best Regards,
        Kevin

        Comment


        • #5
          Hi Kevin,
          sorry for my delay, I have wait for other display, and the behavior is the same.
          The problem is HW, not SW.
          I discover:
          - in 4D-UPA, the serial transmit pin is RX0 (not TX0), and it works properly.
          - in fact, in the 4D-UPA scheme, the word "swapped" appears.
          - following this logic, I used pin TX0 as serial reception, but it does not work. Surely it is internally set as an output, in fact:
          - if I leave the output of the serial adapter disconnected, I see the transmission pulses transiting from + 5V to zero volts (correct).
          - if instead I connect the output of the serial adapter to pin TX0 of the 4D-UPA, the impulses start at 3.5V and finish at about 2.5V (not zero!).
          This behavior is the same with two different serial RS232-TTL adapters, which, with other processors, work correctly.
          Reading the instructions GEN4-ULCD43-DT, at page 7 I found "Used in conjunction with the TX pin for programming this microLCD. This pin is tolerant up to 5.0V levels."
          Could it be that these pins are already used for programming and therefore cannot be used later?
          Or is possible to reset or bypass the programming configuration and use this pins again as serial communication? I dont' find instructions like "pin_Set(PIN_INP..." for this pins.
          Best regards, Valter.

          Comment


          • #6
            Hi Valter,

            Thank you for getting back to us. I haven't asked if you are using the RX0 and TX0 to communicate with the other device since I am using it on the Terminal for debugging. The Diablo16 still has 3 configurable TTL serial comports available. You can always use this to configure the GPIO pins available on the module.
            To set the pin as RX and TX for the COM1 port, use this command:
            Code:
            COM1_RX_pin(pin);
            COM1_TX_pin(pin);
            To set the baud rate of the COM port, use this command:
            Code:
            com_SetBaud(COM1,960)
            Then you can use the other Serial(UART) Communications Functions (e.g. serin1(), com_Init1() ).

            Let me know if that helps.

            Best Regards,
            Kevin

            Comment


            • #7
              Hi Kevin
              I note that with 4D-UPA I cannot use COM0, so I switch to COM1.
              However the "com1_Count" variable always remains at zero.
              I solved the problem by constantly checking the presence of a character with "serin1 ()".
              But I am not convinced that it is the best solution, as I could lose some character, in particular if I increase the speed of transmission.
              Actually I use your "Terminal 9600" application, so my write speed is really slow...
              I note also that insert or not the instruction "com1_Init (BufferIn, NBufferIn, 0);" does not change the result. Why?
              Here are the various pieces of the program:
              Code:
              Main:
              ...
              #constant NBufferIn 10
              var BufferIn[NBufferIn+1];       //20 bytes ?
              var PBufferIn, CarRic;
              ...
                  InitSer();
              ...
              in the main loop:
                  RiceSer();
              
              Subroutines:
              func InitSer()
                  COM1_RX_pin(PA6);                  //GPIO8 su 4D-UPA
                  COM1_TX_pin(PA5);                  //GPIO9 su 4D-UPA
              //    com1_Init(BufferIn, NBufferIn, 0);
                  com_SetBaud(COM1, 960);
                  com_Mode(8, 'N', 1, COM1);
                  com1_Reset();
                  ClearBufferIn();
              endfunc
              
              func ClearBufferIn()
                  for (i := 0; i < NBufferIn; i++)
                      BufferIn[i] := 0;
                  next
                  PBufferIn := 0;
              endfunc
              
              func RiceSer()
                 txt_MoveCursor(1,0);
                 print("NCar: ", com1_Count(), " ", PBufferIn);
                      CarRic := serin1();
                      if (CarRic != -1)
                      BufferIn[PBufferIn] := CarRic;
                      print("Car: ", [CHR]BufferIn[PBufferIn], " ");
                      if ((BufferIn[PBufferIn] == 13) || (PBufferIn >= NBufferIn))
                          print ("\n");
              //            putstr (BufferIn);                 //Doesn't work!
                          for (i:=0; i<PBufferIn; i++)
                              print ([CHR]BufferIn[i]);
                          next
                          print ("<CR>  ");
                          ClearBufferIn();
                      endif
                      PBufferIn++;
                 endif
              endfunc
              Other suggestions?
              Best regards, Valter

              Comment


              • #8
                Hi Valter,

                However the "com1_Count" variable always remains at zero. I solved the problem by constantly checking the presence of a character with "serin1 ()".
                As what I state on the post #2,
                The Count and CarRic will always be '0' because you are removing the byte on a circular buffer using serin() command.

                But I am not convinced that it is the best solution, as I could lose some character, in particular if I increase the speed of transmission.
                Can I ask, what do you mean by this? To process each byte from the buffer, you need to remove it from the queue - which is through serin() function.

                Actually I use your "Terminal 9600" application, so my write speed is really slow...
                You can always increase the Baudrate configuration.
                Code:
                com_SetBaud(COM1, 960);
                Please check the function definition on the Diablo16 Internal Functions (by pressing the F1 key).

                I note also that insert or not the instruction "com1_Init (BufferIn, NBufferIn, 0);" does not change the result. Why?
                The init function creates a buffered service for serial communication. By using this function, it captured the serial data without user application having to constantly poll the serial port.
                Depending on how you want to implement it, you could always take the byte from the buffered service and process it accordingly.

                I suggest for you to test first each command that you are using, and also check its function definition.

                Best Regards,
                Kevin

                Comment


                • #9
                  Hi Kevin
                  I tried to use "com1_Count ()" but it doesn't work as you say (and as the manual says).
                  In fact, even if I delete the "serin1 ()" command, "com1_Count ()" always remains at zero.
                  The fear of losing a few characters in reception, in particular by increasing the speed, is due to the fact that I must read and memorize every character received, and this may depend on the time used for each program loop.
                  I was hoping that the "com1_Init ()" function would help me, automatically storing the characters in the declared buffer, but, not counting the characters received, I don't know how to handle them.
                  Actually I use the free version 4.5.0.17. Does this version have limitations?
                  Here's how I currently work:
                  Code:
                  func InitSer()
                      COM1_RX_pin(PA6);                  //GPIO8 su 4D-UPA
                      COM1_TX_pin(PA5);                  //GPIO9 su 4D-UPA
                  //    setbaud(BAUD_9600);
                      com1_Init(BufferIn, NBufferIn, 0);  //if removed, all works properly
                      com_SetBaud(COM1, 960);
                      com_Mode(8, 'N', 1, COM1);
                      com1_Reset();
                      ClearBufferIn();
                  endfunc
                  
                  func ClearBufferIn()
                      for (i := 0; i < NBufferIn; i++)
                          BufferIn[i] := 0;               //0 = carattere fine stringa
                      next
                      PBufferIn := 0;
                  endfunc
                  
                  func RiceSer()
                  var Str;
                  var pStr;
                  //    NCarRic := com1_Count();
                  //    if (NCarRic > 0)
                      txt_MoveCursor(5,0);
                      print("NCar: ", com1_Count(), " ", PBufferIn);
                      CarRic := serin1();
                      if (CarRic != -1)
                          BufferIn[PBufferIn] := CarRic;
                          PBufferIn++;
                          print("Car: ", [CHR]BufferIn[PBufferIn], " ");
                          if ((BufferIn[PBufferIn-1] == 13) || (PBufferIn >= NBufferIn))
                              print ("\n");
                  //            putstr (BufferIn);        //doesn't work
                              pStr := str_Ptr(Str);
                              for (i:=0; i<PBufferIn-1; i++)
                                  print ([CHR]BufferIn[i]);
                                  str_PutByte(pStr+i, BufferIn[i]);
                              next
                              print ("<CR>  ");
                              pStr := str_Ptr(Str);
                              str_Printf (&pStr, "%s");
                              pStr := str_Ptr(Str);
                              str_GetW(&pStr, &Mis1);
                              ClearBufferIn();
                          endif
                      endif
                  endfunc
                  Now I'm studying the "String Class Functions" to manage the received string, so as to memorize the numeric values (word) in the correct variables (eg Mis1, Mis2, etc. ..).
                  Best regards, Valter

                  Comment


                  • #10
                    Hello Valter,

                    I have attached a simple program to further guide you on how to implement a buffered service.
                    Please test this on your display module. I hope you can use and apply this to your application.
                    There are also comments attached to it.

                    I hope this helps.

                    Best Regards,
                    Kevin
                    Attached Files

                    Comment


                    • #11
                      Hi Kevin
                      Many thanks for the support, but:
                      1) I load your example, only changing the display model, and, using COM14 (the USB port assigned to 4D-UPA) all works properly (obvious!).
                      2) I copy the example and I change the display and the serial port (from COM0 to COM1), I use my connections (not COM14, the port of 4D-UPA), and it doesn't work.
                      3) I try to reduce the speed and use Terminal9600, uselessly.
                      I didn't understand why it doesn't work!
                      Keep in mind that COM1 works regularly using "serin1 ()", as I already told you.
                      If you want, check the modified programs attached, I checked them carefully and it seems to me that there are no mistakes.
                      Best regards, Valter
                      Attached Files

                      Comment


                      • #12
                        Hi Valter,

                        1) I load your example, only changing the display model, and, using COM14 (the USB port assigned to 4D-UPA) all works properly (obvious!).
                        Can you confirm that the example works on you? Using the Terminal Utility to send data to the display?

                        2) I copy the example and I change the display and the serial port (from COM0 to COM1), I use my connections (not COM14, the port of 4D-UPA), and it doesn't work.
                        The code uses the Terminal to send data to the display. In this case, the sender / COM1 (your other device) must be configured to send something on the display. How are you sending the data?

                        3) I try to reduce the speed and use Terminal9600, uselessly.
                        If you are using COM1, you are using the external device to communicate with the display. The Terminal uses COM0 to communicate with the display.
                        On the example program, the baud rate is set to 115200. I do notice that you change the baud rate to 9600 - is this what you mean?
                        Then the baud rate of the other device must also match with the baud rate of the display.

                        If the code is working on the Terminal, that means the problem lies on the other device. I am not sure how you are sending the message from the other device.
                        I did test the "SerialBasics_COM1" and it is working fine on my end. I use the Arduino to communicate with the display. I connect the GPIO8 (RX) of the 4D-UPA to the GPIO1 (TX) of the Arduino, and the GPIO9 (TX) of the 4D-UPA to the GPIO0 (RX) of the Arduino.

                        Here is a simple code which I used to send some bytes on the display

                        Code:
                        static byte message1[10]={0x34,0x44,0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73}; // will send 4D Systems
                        static byte message2[5]={0x31,0x32,0x33,0x34,0x35}; // 1 to 5
                        static byte message3[5]={0x36,0x37,0x38,0x39,0x30}; // 6 to 9, 0
                        
                        void setup() {
                          Serial.begin(9600);
                        
                        }
                        void loop() {
                        
                        
                         Serial.write(message1,10);
                         delay(1000);
                        
                         Serial.write(message2,5);
                         delay(300);
                        
                         Serial.write(message3,5);
                         delay(300);
                        
                         delay(500);
                        }
                        Best Regards,
                        Kevin





                        Comment


                        • #13
                          Hi Kevin
                          Update:
                          1) I confirm that the original code works properly, using 4D Terminal utility.
                          2) I try again with the second code (COM1), and I discover it works properly! I don't understand why yesterday doesn't work, considering that the enviroments is the same. I am sorry for the wrong test of yesterday.
                          3) I check the COM1 code at 9600BPS (as per attached file), to be sure the serial line and the terminal utility doesn't have speed problems (I try also with TeraTerm and others).
                          The stupidest thing that happens to me is this:
                          In my program (which does other things besides talking to the serial), for safety I removed the reference to "serin1 ()", I entered only the control on the number of characters received ("com1_Count()") and I show the value constantly in the display: it continues to remain at zero! Incredible!
                          I still don't understand where I'm wrong, but, as soon as I find out, I'll let you know.
                          Thanks again for your support!
                          Best regards, Valter

                          Comment


                          • #14
                            Open the manual and read what com_Reset() does and then look at where you have it in your code
                            Mark

                            Comment


                            • #15
                              GREAT!
                              At page 247 I find: "function (which will return serial reception to polled mode)".
                              Without thinking (or reading) that much, I thought that an initialization of the serial didn't hurt ....
                              I remove "com1_Reset();" from the initializations and now the counter run properly!
                              Solved!
                              Many thanks to all!!
                              Best regards, Valter

                              Comment

                              Working...
                              X