Argh!!! [VBA] Read from Serial Port

:frowning:

Oh my god. I've been at this for 4 hours. I can NOT get MS Access to read from serial port. I tried every example you find on google.

That is the best one I could find.

while invoking "Get ..." will hang access

Another one vba - Read from Serial port to Excel - Stack Overflow
After cleaning some $'s out and excel speficic stuff, it goes and, you guessed it-- HANGS. I even took out the darn While loop. Still. Hangs. I am going to be the next that hangs.

If you know of a way that doesn't have a bunch of dependencies, please contact me.

I'm not familure with VBA, but much like VB with wings I expect .

If similar, "Doevents" should be included in any big or waiting loops,
much like the Yeild() or delay(0) in Arduino.

Serial is easy in VB, perhaps my serial stuff will give you some pointers, or make it worse.!
Shout if you think it will help.

I had a quick read of the link and the example.
Hum yep similar but slightly different.
Not sure if you can use VB OCX files in VBA,

I have no idea of your project so not sure if it NEEDS to be done in VBA.
If you just need a simple exe then happy to wrap up something "simple".

if needed I can look deeper and see if I can get that example working,
But I don't need to and you don't learn as much in trial and error!
So your call.

Hv.

Thanks... I'm lost.. I'm a little rusty but with help I get most stuff working... I got some advanced databases open. No luck READing serial port.

I just smashed that example into Excel.

Works fine.!!

I connected to com6 @ 115200 and it pulled the boot string from my esp.

Just make sure that the setting/clearing of the RTS & DTR lines is not resetting your device.

Plus I think the example only had MAXPORTS = 4
Private Const MAX_PORTS = 16

PLUS,, I don't see a timer event in VBA and not sure if your data is only coming in after a request or if it is arriving un requested. if it is streaming in then you need some method to read the data into a buffer at regular intervals and act on the data.

But if you simply Request and Read the reply then that example is fine.

Just make sure you are setting the intPortID
intPortID = 6 ' << adjust as required
and make sure that the strData has enough space to handle the read.
As per read 64 bytes.
strData = String(64, Chr(0))
lngStatus = CommRead(intPortID, strData, 64)
Oh I read the code deeper and see that strData is cleared in the Commread anyway.
So you can simply pass it a blank string and the byte count you want to receive.

That Chunk:

    ' Write data to serial port.
    strData = "dir" & vbCrLf
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If


    strData = String(64, Chr(0))
    ' Read maximum of 64 bytes from serial port.
    lngStatus = CommRead(intPortID, strData, len(strData)) ' << use len(strData) to save numbering
    If lngStatus > 0 Then
        Debug.Print strData
        ' Process data.
    ElseIf lngStatus < 0 Then
        ' Handle error.
    End If

What is your target device.? Some sort of Atmel/Arduino.

I got it working. Some guy had a huge BAS file that worked around whatever had VBA crashing.

Some guy had a huge BAS file that worked around whatever had VBA crashing.

It's nice to share.