Click here to Skip to main content
14,927,597 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a piece of equipment connected to my PC by USB port. This is showing up as COM7.

Using VB I can open the port, but any data sent to it does not get there. This I can tell because the command should switch a light on, amongst other things. I have tried changing COM7 to COM19 and port does not open.

I can open port and send data from HyperTerminal and from Docklight. I can connect my PC to another PC running Docklight or HyperTerminal and the data gets there. So I am fairly confident about my code, shown below.

I have read an article published a few years ago, talking about serial ports and saying that there were problems with Net 3.5. The problems were such that you couldn't use it to address serial ports.




VB.NET
Sub SendSerialData(ByVal data As String)

        ' Send strings to a serial port.
        Dim J As Integer
        Dim jl As Integer


        Try
            SerialPort1.Open()

            'Using com1 As IO.Ports.SerialPort =
            'My.Computer.Ports.OpenSerialPort("COM7", 115200, Parity.None, 8, StopBits.One)

            

            jl = Len(data)

            For J = 1 To jl
                SerialPort1.Write(Mid(data, J, 1))
                MsgBox(Mid(data, J, 1))
            Next

            SerialPort1.Write(Chr(13))

            '   SerialPort1.WriteLine(data)
            '    com1.WriteLine(data)

            MsgBox("Sent - " & data )

            SerialPort1.Close()

            ';End Using
        Catch
            MsgBox("Problem sending")
        End Try

    End Sub


What I have tried:

I have tried various options, two of which you can see in my code above, the remmed out lines being the alternative option.

I have tried sending string, one character at a time and the whole line at a time, nether works.

I have also tried setting port parameters, such as baud rate in device manager.

I have tried have SerialPort on the form, and not having it.

Firstly anyone know if the problems in Net 3.5 have been fixed ? Does 4.6 work ?

Any suggestions please to sort this ? I think I have tried most of write options .

Once this works then I'll need it to read the responses....

Thanks for reading.
Posted
Updated 10-Feb-16 14:52pm
v2
Comments
PIEBALDconsult 10-Feb-16 21:00pm
   
My first thought is that the device is likely expecting 8-bit characters, whereas .net generally uses 16-bit characters. If that's the problem, I'm unsure how to work around it. But look into the https://msdn.microsoft.com/en-us/library/system.text.encoding.ascii(v=vs.110).aspx to see whether or not it helps.
Sergey Alexandrovich Kryukov 10-Feb-16 22:36pm
   
You don't show important part of the code; the port settings...
—SA
Denis Oxon 11-Feb-16 5:18am
   
Thank you for your two comments.

Port settings in device manager are

Baud rate 115200
8 bits
1 stop bit
No parity
No flow control

I have a feeling you may be right about the number of bits. I have read the article, makes a certain amount of sense.
Denis Oxon 11-Feb-16 19:20pm
   
I have now investigated further, converting my string to ASCII, UTF7 and UTF8 though MS do say that the default for WRITE is ASCII, so it should be all right.

I have tried writing using Write and Writeline, sending the whole string and sending character by character.

I notice that the instructions for Writeline given by MS say that Writeline writes to a buffer. Probably a silly question - how do you get the data from the output buffer to the device ? Write goes to serial port, so I assume, maybe wrongly that it will go straight to device.

Any help would be appreciated. Thanks

1 solution

I have updated Net Framework in the project from 4.0 to 4.6 and I can now send strings to the device. Next to sort out reading......
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900