Author Topic: output data to serial port  (Read 2771 times)

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
output data to serial port
« on: November 01, 2019, 04:39:11 AM »
Hi All,
This is not really a AHMI issue but kind of related.
I'm sending out some data from the AHMI out to serial port to third party device:
https://www.manualslib.com/manual/961674/Netmc-Marine-Videotxt.html#manual
this is my code:
Code: [Select]
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If SerialPort1.IsOpen Then
            SerialPort1.Write(BasicLabel1.Text & Space(8) & BasicLabel2.Text & Space(8) & DateAndTime.Now.ToString & vbCrLf & vbNewLine & DateAndTime.Now.ToString & vbCrLf)

        End If

    End Sub

I just need two line on the output screen that will continuously update but now I got following output:
Data1 Data2 11/1/2019 1:08:25 am
 11/1/2019 1:08:25 am
 Data1 Data2 11/1/2019 1:08:26 am
 11/1/2019 1:08:26 am
 Data1 Data2 11/1/2019 1:08:27 am
 11/1/2019 1:08:27 am
 Data1 Data2 11/1/2019 1:08:28 am
 11/1/2019 1:08:28 am
 Data1 Data2 11/1/2019 1:08:29 am
 11/1/2019 1:08:29 am
 So on....

I notice this happen when I added the vbnewline.
I'm not sure what is the trick if there someone could please help.


   

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: output data to serial port
« Reply #1 on: November 01, 2019, 02:20:18 PM »
What is the output like if you remove what you added?

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: output data to serial port
« Reply #2 on: November 02, 2019, 01:27:16 AM »
If i delete vbnewline, output will be on first line only if the character is less than 59 (thats how the setting on the device). If my character more than 59, it will continue on 2nd line and start repeating as my previous post.
i think it has to do with the hex code function as in attachment. Maybe i have to change the vbcrlf with the code?
may i know how can i use that hex code in my code?

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: output data to serial port
« Reply #4 on: November 03, 2019, 12:02:41 AM »
In your code you have DateAndTime.Now.ToString appearing 2 times.
Is that necessary or could it be 1 only:

Code: [Select]
        If SerialPort1.IsOpen Then
            SerialPort1.Write(BasicLabel1.Text & Space(8) & BasicLabel2.Text & Space(8) & DateAndTime.Now.ToString & vbCrLf)
        End If

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: output data to serial port
« Reply #5 on: November 03, 2019, 09:52:37 PM »
I manage to stop the data from repeating by using the Hex Code provided in the manual. here is the modified code:
Code: [Select]
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RichTextBox1.Text = Chr(&H13) & "Date    :" & DateAndTime.DateString &
                            Chr(&H13) & Chr(&H14) & "Time    :" & DateAndTime.TimeString

        If SerialPort1.IsOpen Then

            SerialPort1.Write(Chr(&H15) & RichTextBox1.Text)

        End If

    End Sub
As you can see from the code (I'm not sure what is the correct way), I added the cursor left Chr(&H14) but I get following output:

 Date :11-03-2019
 xxxxxxxxxxxxxxxxxTime :18:07:53

 (xxxx is space)

 How should I make the next line start from left?

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: output data to serial port
« Reply #6 on: November 03, 2019, 10:08:49 PM »
The attached picture shows what I get when I use your code.

Can you use a Notepad and type several lines exactly how you want to them to look before you write each one of them using Serial Port.

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: output data to serial port
« Reply #7 on: November 03, 2019, 10:45:43 PM »
Well I've done that. Ofcourse on my sending richtextbox, I get the same as you got.
if I don't use the hex code, I got this output on the richtextbox:

Date    :11-03-2019
Time    :19:40:20

Because I need to use the hex code to display the output on the device, my output on the device screen is as I showed in previous post.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: output data to serial port
« Reply #8 on: November 04, 2019, 12:24:03 AM »
If I use this code:

Code: [Select]
  RichTextBox1.Text = Chr(&H13) & "Date    :" & DateAndTime.DateString & Environment.NewLine & Chr(&H13) & "Time    :" & DateAndTime.TimeString

then I get the output as in the attached picture.

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: output data to serial port
« Reply #9 on: November 04, 2019, 02:43:17 AM »
Yes I get the same too.
but that is not the case when we see on the device screen.
I've sort out the issue. here is the code:
Code: [Select]
If SerialPort1.IsOpen Then

            SerialPort1.Write(Chr(&H15) & Chr(&H13) & "Date    :" & DateString & vbNewLine &
                            "Time    :" & TimeString & vbNewLine &
                            "Head    :" & Space(9 - Strings.Len("Head    :")) & BasicLabel1.Text & " deg" & Space(10 - Strings.Len(BasicLabel1.Text + " deg")) &
                            "Pitch   :" & Space(9 - Strings.Len("Pitch   :")) & BasicLabel2.Text & " deg" & Space(10 - Strings.Len(BasicLabel2.Text + " deg")) &
                            "Roll    :" & Space(9 - Strings.Len("Roll    :")) & BasicLabel3.Text & " deg" & Space(10 - Strings.Len(BasicLabel3.Text + " deg")) &
                             vbNewLine &
                            "Pt Bury :" & Space(9 - Strings.Len("Pt Bury :")) & BasicLabel10.Text & " cm" & Space(10 - Strings.Len(BasicLabel10.Text + " cm")) &
                            "Stb Bury:" & Space(9 - Strings.Len("Stb Bury:")) & BasicLabel9.Text & " cm" & Space(10 - Strings.Len(BasicLabel9.Text + " cm")) &
                            "Jet Pres:" & Space(9 - Strings.Len("Jet Pres:")) & BasicLabel8.Text & " psi" & Space(10 - Strings.Len(BasicLabel8.Text + " psi")) &
                            vbNewLine &
                            "Alt     :" & Space(9 - Strings.Len("Alt     :")) & BasicLabel4.Text & " mtr" & Space(10 - Strings.Len(BasicLabel4.Text + " mtr")) &
                            "Depth   :" & Space(9 - Strings.Len("Depth   :")) & BasicLabel5.Text & " mtr" & Space(10 - Strings.Len(BasicLabel5.Text + " mtr")) &
                            vbNewLine &
                            RichTextBox3.Text)
        End If
I have to include all the desired output in the If statement. Otherwise it is not well arranged.