-
Aug 10th, 2014, 05:26 PM
#1
Thread Starter
Junior Member
Problem sending receiving formatted string commands via serial port.
Hi All,
Hope someone can help me with this. Please excuse the long post, I am trying to pass enough information.
I am an electronics engineer; I am not a VB programmer by any means although I have toyed with it when I have had to, like now. I have some micro-controller programming experience in BASIC. I have written the following 'code' See below.
I have a device which has a 9pin serial port; I am trying to connect to it via serial port to USB converter, so far it is fine. The device communicates in string only, so anything sent to it and received will be in string and will need to write it to a text file. The text file bit, I havent even started, but I am guessing I can do that later.
I need to read the memory space within this device from address &h0000 to &hFFFF. To do this I need to send a command 'DM' followed by a (0) padded address, and the device will reply with 128 byte packet in the format; address followed by 16 bytes CR & LF. So it will look like this:
Send:
"DM0000" CR LF
Receive:
0000:00 11 64 60 00 00 00 50 10 02 91 40 20 00 00 03
0010:3D 00 00 00 00 00 00 00 49 52 20 52 41 44 20 20
0020:04 41 10 62 50 00 62 50 10 02 B0 38 10 00 00 07
0030:24 00 00 01 01 00 31 25 57 45 53 54 46 4C 44 20
0040:00 05 68 00 00 00 00 50 10 82 93 40 20 00 00 05
0050:7B 00 00 02 01 00 00 00 53 41 52 20 20 20 20 20
0060:04 56 65 00 00 00 50 00 10 02 B0 38 10 00 00 06
0070:2A 00 00 03 01 00 25 00 20 20 20 20 20 20 20 20
The address part of the command must be in hex and padded (4 positions)
Above is an actual command and its returned packet and is correct. I have a loop that starts from 0 to 65535 step 128 to feed the send command address field, I have also Googled and tried to find a way to format the padded hex address string for days, hence the many commented lines and extra strings, I think I have it right now as I can see the format is correct in debug step mode looking at 'outPkt'. However, there is a problem which I am not sure what is causing it.
The above received packet is 128 bytes returned for address 0000, my next loop jump should be &H0080 or 128 decimal passed as the address but I get the following packet (the data is irrelevant but not the address):
Send:
"DM0128" CR LF
0120:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0130:00 00 00 09 00 00 00 00 20 20 20 20 20 20 20 20
0140:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0150:00 00 00 10 00 00 00 00 20 20 20 20 20 20 20 20
0160:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0170:00 00 00 11 00 00 00 00 20 20 20 20 20 20 20 20
0180:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0190:00 00 00 12 00 00 00 00 20 20 20 20 20 20 20 20
It should start from &h0080 onwards for 128 byte (&h80). Below are the results of the following: DM0256 - DM0384 - DM0512 - DM0640
0250:00 00 00 18 00 00 00 00 20 20 20 20 20 20 20 20
0260:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0270:00 00 00 19 00 00 00 00 20 20 20 20 20 20 20 20
0280:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0290:00 00 00 20 00 00 00 00 20 20 20 20 20 20 20 20
02A0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
02B0:00 00 00 21 00 00 00 00 20 20 20 20 20 20 20 20
02C0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0380:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0390:00 00 00 28 00 00 00 00 20 20 20 20 20 20 20 20
03A0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
03B0:00 00 00 29 00 00 00 00 20 20 20 20 20 20 20 20
03C0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
03D0:00 00 00 30 00 00 00 00 20 20 20 20 20 20 20 20
03E0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
03F0:00 00 00 31 00 00 00 00 20 20 20 20 20 20 20 20
0510:00 00 00 40 00 00 00 00 20 20 20 20 20 20 20 20
0520:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0530:00 00 00 41 00 00 00 00 20 20 20 20 20 20 20 20
0540:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0550:00 00 00 42 00 00 00 00 20 20 20 20 20 20 20 20
0560:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0570:00 00 00 43 00 00 00 00 20 20 20 20 20 20 20 20
0580:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0640:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0650:00 00 00 50 00 00 00 00 20 20 20 20 20 20 20 20
0660:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0670:00 00 00 51 00 00 00 00 20 20 20 20 20 20 20 20
0680:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0690:00 00 00 52 00 00 00 00 20 20 20 20 20 20 20 20
06A0:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
06B0:00 00 00 53 00 00 00 00 20 20 20 20 20 20 20 20
Obviously something is wrong, possibly about the jumps is not right or consistent. On the basis that the first packet from DM0000 returned correctly, I am assuming the format is correct and I am reading the port OK. The 'for next' loop is simple enough, I just dont understand why it is not returning the right values. The very first packet is all correct ending with address 0070, the next packet should start with 0080 followed by 0090, 00A0, 00B0, 00C0, 00D0, 00E0, 00F0 - next packet with 0100, 0110, 0120 etc. you get the idea. I would very much appreciate any input.
Thank you.
Sean
My very rough code below:
Code:
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim Command As String = "AR5BA4"
Dim RXed As String
'DECLARE A COMM PORT
Dim WithEvents ADRport As SerialPort = New _
System.IO.Ports.SerialPort("COM2", _
19200, _
Parity.None, _
8, _
StopBits.Two)
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
'CHECK IF PORT IS OPEN AND THEN CLOSE COMM PORT
If ADRport.IsOpen Then
ADRport.Close()
End If
End Sub
Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'CREATE 20 COM PORTS TO BE SELECTIONS IN PULL DOWN MENU
Dim PortCount As Integer
For PortCount = 1 To 3
ComboBox1.Items.Add("COM" & PortCount)
Next
'SET COMBO BOX TO COM 1 (DEFAULT)
ComboBox1.SelectedIndex = 1
End Sub
'open port & send commands
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btConnect.Click
'CHECK IF PORT IS CLOSED AND THEN OPEN COM PORT FROM COMOBOX PORT NAMES
ADRport.PortName = ComboBox1.SelectedItem
If Not ADRport.IsOpen Then
ADRport.Open()
End If
'initial connection fully to AR5k
ConAR5k()
'connect using password
Command = "AR5BA4"
SendCommand(Command)
'set command and send
'Command = "AFC1"
'SendCommand(Command)
End Sub
'initial connection using CR&LF
Sub ConAR5k()
'Connect to AR5000 using password
ADRport.Write(Chr(10) + Chr(13))
'ADRport.Write(Chr(10) + Chr(13))
'WAIT 20MS FOR COMMAND TO BE SENT
Thread.Sleep(20)
End Sub
'send command
Public Function SendCommand(ByVal Command As String) As String
ADRport.Write(Command)
ADRport.Write(Chr(10) + Chr(13))
Thread.Sleep(20)
End Function
'Private Sub btnConnect1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Command = "DMFFEE"
' Try
' 'Connect to AR5000
' ADRport.Write(Chr(10) + Chr(13))
' 'WAIT 20MS FOR COMMAND TO BE SENT
' Thread.Sleep(20)
' SendCommand(Command)
' 'ADRport.Write("AR5BA4" + Chr(13))
' 'SET READ TIMOUT FOR RS232 READ TO 100MS
' 'ADRport.ReadTimeout = 300
' 'READ DATA FROM RS232 INPUT BUFFER UP TO THE NEXT CR ( CHR(13))
' 'TextBox1.Text = (ADRport.ReadTo(Chr(13)))
' rtBox1.Text = (ADRport.ReadTo(Chr(13)))
' Catch ex As TimeoutException
' 'IF THERE IS A TIMEOUT, PRINT" No Data " IN TEXTBOX1.TEXT
' 'TextBox1.Text = "NoData"
' 'THE NEXT LINE ENABLES A SPLASH SCREEN WITH AN ERROR MESSAGE ( DELETE IF NOT DESIRED)
' MsgBox(ex.Message)
' End Try
'End Sub
'SEND address & process
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSend1.Click
Dim fmt As String = "0000"
Dim outCmd As String
Dim outPkt As String
Dim inPkt As String
Dim myLoop As Integer
Dim myVal As String
Dim tmpI As String
Dim mVal As String = myLoop.ToString(myLoop)
For myLoop = &H0 To &H280 Step &H80
Console.WriteLine(myLoop.ToString(fmt))
tmpI = (myLoop.ToString(fmt))
myVal = myLoop.ToString("X")
'myVal.Format("{0X4}", myLoop)
String.Format("{0:X4}", tmpI)
myVal.PadLeft(4, "0"c)
'Commands being sent out
'rtBox2.Text = rtBox2.Text + tmpI & Chr(10) + Chr(13)
'ADRport.Write(CBox2.Text & Int(Val(myLoop)) & Chr(10) + Chr(13))
'ADRport.Write("DM" & Int(Val(myLoop)) & Chr(10) + Chr(13))
'outPkt = ("DM" & tmpI & Chr(10) + Chr(13))
outPkt = ("DM" & tmpI & Chr(10) & Chr(13))
rtBox2.Text = rtBox2.Text & outPkt
ADRport.Write(outPkt)
Thread.Sleep(100)
inPkt = (ADRport.ReadTo(Chr(10) + Chr(13)))
Thread.Sleep(100)
'rtBox1.Text = rtBox1.Text + (ADRport.ReadTo(Chr(10) + Chr(13)))
rtBox1.Text = rtBox1.Text + inPkt
'rtBox1.Text = (ADRport.ReadTo(Chr(10) + Chr(13)))
'rtBox1.Text = "" + Chr(10) + Chr(13)
Thread.Sleep(10)
Next
End Sub
Private Shared Sub DataReceivedHandler(sender As Object,e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
Console.WriteLine("Data Received:")
Console.Write(indata)
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles btConnect.Click
End Sub
'close port & form
Private Sub MeClose_Click(sender As System.Object, e As System.EventArgs) Handles btClose1.Click
ADRport.Close()
Me.Close()
End Sub
Private Sub btClear1_Click(sender As System.Object, e As System.EventArgs) Handles btClear1.Click
rtBox1.Clear()
End Sub
Private Sub rtBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles rtBox1.TextChanged
End Sub
Private Sub rtBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles rtBox2.TextChanged
End Sub
End Class
-
Aug 10th, 2014, 05:51 PM
#2
Re: Problem sending receiving formatted string commands via serial port.
Looks like you have a lot of unnecessary string formatting going on.
The format "X4" will produce four digits of Hex, with leading zeros if the number is less than 4 digits.
So,
Code:
Dim fmt As String = "X4"
For myLoop = 0 To &H280 Step &H80
Console.WriteLine(myLoop.ToString(fmt))
tmpI = (myLoop.ToString(fmt))
'myVal = myLoop.ToString("X")
''myVal.Format("{0X4}", myLoop)
'String.Format("{0:X4}", tmpI)
'myVal.PadLeft(4, "0"c)
'...
outPkt = ("DM" & tmpI & Chr(10) & Chr(13))
'...
It should start from &h0080 onwards for 128 byte (&h80). Below are the results of the following: DM0256 - DM0384 - DM0512 - DM0640
The addresses should be hex. The DM commands you are specifying multiples of 128 in decimal, but being interpreted as hex, thus the reason it is returning what you ask, to the nearest lower address of 16 bytes (10 hex), e.g. 128 (interpreted as hex is rounded down to 120 (12hex * 16).
256hex is rounded down to 250hex, 384hex down to 380 hex.
Last edited by passel; Aug 10th, 2014 at 05:59 PM.
-
Aug 11th, 2014, 02:03 AM
#3
Thread Starter
Junior Member
Re: Problem sending receiving formatted string commands via serial port.
Hi passel,
Thank you for your reply.
The various string formatting in the code most of which are commented out are the results of trying different formatting commands. If I remember correctly, the “X4” packet didn’t like being in the outPkt = statement as it throw a VB error of some sort so I changed it to the current one which uses the .Tostring(fmt), which is accepted by the packet statement above.
I would never have thought about the DM being converted to hex! Do you have a suggestion as to how to do this?
-
Aug 11th, 2014, 02:08 AM
#4
Thread Starter
Junior Member
Re: Problem sending receiving formatted string commands via serial port.
By the why is it taking the "DM" as a hex number, I dont think I have asked it to do so, it is a 2 letters within quotation marks, why would it be taken as hex?
Thanks,
-
Aug 11th, 2014, 05:54 AM
#5
Re: Problem sending receiving formatted string commands via serial port.
Obviously the DM command expects hex digits because that is what your results are showing you.
You only give it 4 digits, and you can't reach 65535 with 4 decimal digits.
You also said: "I need to read the memory space within this device from address &h0000 to &hFFFF."
Those are the address inputs that the DM command expects.
The code I posted worked fine for me, with "Dim fmt As String = "X4".
Code:
For myLoop = 0 To &H280 Step &H80
tmpI = (myLoop.ToString(fmt))
outPkt = ("DM" & tmpI & Chr(10) & Chr(13))
Next
Sets outPkt to
DM0000
DM0080
DM0100
DM0180
etc...
p.s. I think you may have misunderstood me.
I didn't mean DM was changed to Hex (that doesn't even make sense, as DM are not hex characters).
I meant the address string portion of the DM command is expected to be in Hex, not decimal.
Last edited by passel; Aug 11th, 2014 at 05:59 AM.
-
Aug 11th, 2014, 06:05 AM
#6
Re: Problem sending receiving formatted string commands via serial port.
Try this:
Code:
For x As Integer = 0 To 65535 Step 128
Dim outPkt As String = String.Format("DM{0:X4}", x)
Debug.WriteLine(outPkt)
Next
-
Aug 11th, 2014, 09:07 AM
#7
Thread Starter
Junior Member
Re: Problem sending receiving formatted string commands via serial port.
passel,
Yes the DM command and the address range requires hex numbers, you may have missed that as I mentioned "..tried to find a way to format the padded hex address string for days." somewhere in the long post.
Yes I was surprised as I misunderstood you regarding DM command somehow turning into a hex number.
Thank you in any case, I will try your solution and report back, having tried so many ways to get that working, now I cant remember which worked and which didn’t.
Dwayne,
Thank you for your input. I will try and report.
I see you are still relentlessly helping clueless people like me!
-
Aug 11th, 2014, 11:25 AM
#8
Thread Starter
Junior Member
Re: Problem sending receiving formatted string commands via serial port.
Both worked, although Dwayne your Dim statement caused a warning that a variable was being hidden in the statement
Now I go on to do the rest of the code, I may be back!
Thank you both very much.
Sean
-
Aug 11th, 2014, 01:03 PM
#9
Re: Problem sending receiving formatted string commands via serial port.
Originally Posted by seanstevens
passel,
Yes the DM command and the address range requires hex numbers, you may have missed that as I mentioned "..tried to find a way to format the padded hex address string for days." somewhere in the long post.
...
No, I didn't miss that.
But you were passing decimal addresses in your examples and made it sound like you didn't know why if was responding the way it was.
" DM0128" CR LF
0120:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0130:00 00 00 09 00 00 00 00 20 20 20 20 20 20 20 20
0140:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0150:00 00 00 10 00 00 00 00 20 20 20 20 20 20 20 20
0160:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0170:00 00 00 11 00 00 00 00 20 20 20 20 20 20 20 20
0180:FF FF FF FF FF FF FF FF FF 02 00 00 00 00 00 00
0190:00 00 00 12 00 00 00 00 20 20 20 20 20 20 20 20
It should start from &h0080 onwards for 128 byte (&h80). Below are the results of the following: DM0256 - DM0384 - DM0512 - DM0640
Those should have been DM0080, DM0100, DM0180, DM0200, DM0280, which is what I was saying at the bottom of my first post. You were passing DM commands with a decimal string, rather than a Hex string address.
Since all the Decimal Digit characters are valid Hex Digit characters, whatever processed the Dump Memory command interpreted them as Hex.
-
Aug 11th, 2014, 01:31 PM
#10
Re: Problem sending receiving formatted string commands via serial port.
Originally Posted by seanstevens
passel,
Yes the DM command and the address range requires hex numbers, you may have missed that as I mentioned "..tried to find a way to format the padded hex address string for days." somewhere in the long post.
Yes I was surprised as I misunderstood you regarding DM command somehow turning into a hex number.
Thank you in any case, I will try your solution and report back, having tried so many ways to get that working, now I cant remember which worked and which didn’t.
Dwayne,
Thank you for your input. I will try and report.
I see you are still relentlessly helping clueless people like me!
If outPkt was defined outside the loop then this
Code:
Dim outPkt As String = String.Format("DM{0:X4}", x)
would be changed to
Code:
outPkt = String.Format("DM{0:X4}", x)
-
Aug 11th, 2014, 04:25 PM
#11
Thread Starter
Junior Member
Re: Problem sending receiving formatted string commands via serial port.
passel,
Sorry, I apologise, you are right. The 3rd & 4th packets looked like they had hex in the address field , now I understand what you mean. Thank you for explaining it. When you speak to someone who doesnt speak hex this is what you get .
Dwayne - understood.
-
Aug 11th, 2014, 04:52 PM
#12
Re: Problem sending receiving formatted string commands via serial port.
HA! Your codes have been hexed.
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|