2 Replies - 6225 Views - Last Post: 20 June 2013 - 07:48 AM Rate Topic: -----

#1 justin1269  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 09

Sending hex character to Serial Port using VB.net

Posted 10 June 2013 - 08:59 PM

Hi,


I am new to Visual Basic and I now started using Visual Basic for actual projects. I was at the dreamincode forum and I read some of the answers and I thought maybe you can provide some guidance with a project I am involve.

I have created vb application to send Part Number, Serial Number and Install Date via serial. The application has been working great so far, but when other users are entering the info usually forget the Hex byte (c8, ca, &cc).

Currently this is how I am entering the P/N/S/N and Install date: (

1. Initialie CPU by Entering the followong: [00]
2. Entering Part Number: [c8 1121225]
3. Entering Serial Number: [caJC13F00060]
4. Entering Install Date: [cc06102013]

This is what I am hoping to accomplish:


My problem is that I do not know how to implement the following prefixes ([c8 ] [ca] [cc] inside my code, where the user doesn't have to enter the [c8 1121225. I just want the operator to scan part number/Serial Number and Install Date as follows: 1121225, JC13E00050, and 06102013. I hope the detailed info make sense and you're able to help. Please see the pictures picture1 is how I am currently sending the Info, and PPicture2 is how I am hoping to send the info.

Thanks,

WJ

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class FrmMain

    'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim myPort As Array 'COM Ports detected on the system will be stored here
    Dim c8 As String
    Dim ca As String
    Dim cc As String

    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data.

    Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)
        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
        btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled

    End Sub
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
        SerialPort1.PortName = CmbPort.Text 'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = CmbBaud.Text 'Set Baud rate to the selected value on
        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8 'Open our serial port
        SerialPort1.Open()
        BtnConnect.Enabled = False 'Disable Connect button
        BtnDisconnect.Enabled = True 'and Enable Disconnect button
    End Sub
    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDisconnect.Click
        SerialPort1.Close() 'Close our Serial Port
        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
        SerialPort1.Write(TxtTransmit.Text & vbCr") 'The text contained in the txtText will be sent to the serial port as ascii
        'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
        'Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
        'SerialPort1.Write[("c8",0,12) 'The text contained in the txtText will be sent to the serial port as ascii
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.RtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.RtbReceived.Text &= [text]
        End If
    End Sub
    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = CmbPort.Text 'pop a message box to user if he is changing ports
        Else 'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub
    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = CmbBaud.Text 'pop a message box to user if he is changing baud rate
        Else 'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub TxtTransmit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtTransmit.TextChanged

    End Sub
End Class

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by AdamSpeight2008: 11 June 2013 - 02:07 PM
Reason for edit:: Please can you use the correct code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Sending hex character to Serial Port using VB.net

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Sending hex character to Serial Port using VB.net

Posted 10 June 2013 - 10:14 PM

Is there any reason why you wish to use only one TextBox for user input?

I would recommend using three TextBoxes for user input; one for Part number, one for serial number, and one for install date.

This way, you can have the user fill in all three TextBoxes, hit a "Send" Button, and build the strings using string concatenation. So you would have the data placed into variables something like
    strPartNo = "[c8] " & txtPartNo.Text
    strSerialNo = "[ca] " & txtSerialNo.Text
    strInstallDate = "[cc] " & txtInstallDate.Text


Then you can send the strings to the serial port.
Was This Post Helpful? 0
  • +
  • -

#3 justin1269  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 09

Re: Sending hex character to Serial Port using VB.net

Posted 20 June 2013 - 07:48 AM

Hi all,

Thank you for your help I was able resolve the issue

Results:
1. Enter 00 Output: [00]
2. Enter 1121225 Ouput: [c8 1121225]
3. Enter JC13E00050 Output: [caJC13E00050]
4. Enter 06202013 Output: [cc06202013]

Thanks for your suggestions


Private Sub btnSend1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend1.Click
  Dim TextToSend As String
  TextToSend = Space(0)
  TextToSend = "[ca" & Space(0) & txtTransmit1.Text & "]"
  SerialPort1.Write(TextToSend)
End Sub

Private Sub btnSend2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend2.Click
  Dim TextToSend As String
  TextToSend = Space(1)
  TextToSend = "[cc" & txtTransmit2.Text & "]"
  SerialPort1.Write(TextToSend)
End Sub

Private Sub btnSend3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend3.Click
  Dim TextToSend As String
  TextToSend = Space(1)
  TextToSend = "[00]"
  SerialPort1.Write(TextToSend)
End Sub

This post has been edited by AdamSpeight2008: 20 June 2013 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1