# Serial read problem on mac only

**URL:** <https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785>\
**Category:** General\
**Created:** [March 5, 2014, 9:26pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785 "2014-03-05T21:26:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 5, 2014, 9:26pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/1 "2014-03-05T21:26:51Z")

</div>

I have the following code in the data available event in a serial control:

```auto
  EditField2.TEXT=Serial1.ReadAll
  
  IF EditField2.TEXT="A" then
    thread1.run
    thread3.kill
  END IF
  
  
  IF EditField2.TEXT="B" THEN 'end lane 1
    thread1.kill
    thread3.run
  END IF
```

This code works fine on my pc but not on my mac. I can see the data, which is the letter A flicker in editfield2 on my mac, but it wont trigger my thread that I have in my “if statement”. Every once in a while it will see the A and trigger but mostly it doesnt. It is as if it is happening too fast. Any ideas??? I am testing on a dell pc laptop (works fine) and a macbook pro (does not work).

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 5, 2014, 9:31pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/2 "2014-03-05T21:31:58Z")

</div>

Just for debugging, change your first line to

EditField2.TEXT=EncodeHex(Serial1.ReadAll)

You may be getting extra, nonprintable characters. Also, try Trim’ing the result.

EditField2.TEXT=Trim(Serial1.ReadAll)

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 5, 2014, 9:33pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/3 "2014-03-05T21:33:37Z")

</div>

Another thought: an editfield is a terrible place for string storage. It can modify the string. Use a local variable for your actual tests, and shove it into the editfield just for display.

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 5, 2014, 10:02pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/4 "2014-03-05T22:02:23Z")

</div>

Ok, thanks for the tips, but it still isn’t working…

The trim made it better, it works sometimes, but not all the time. I have this connected to an arduino board connected to an IR timing gate. On my pc I can drop a pen across the beam and it trigger every time. On my mac I can see that data comming in the serial port everytime, but it is like it is ignoring it half the time. Sometimes it trigger just fine, other times I can hold my hand in from of the beam, I can see the data is there, but it just doens’t do anything.

I changed my code so I am now using a variable to read the serial data instead of the editfield. I was just using the editfield for debugging.

any other ideas?

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 5, 2014, 10:05pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/5 "2014-03-05T22:05:14Z")

</div>

I am also using realbasic 2007 release 3 standard edition.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [March 5, 2014, 10:13pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/6 "2014-03-05T22:13:08Z")

</div>

2007 ???  
There’s 7 years of bug fixes & OS changes/patches that could easily be causing you issues

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 5, 2014, 11:41pm UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/7 "2014-03-05T23:41:23Z")

</div>

maybe, but my 2006 version on my pc is working fine…I just can’t justify the $300 upgrade at this point in time. But when I can I will.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [March 6, 2014, 12:17am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/8 "2014-03-06T00:17:47Z")

</div>

Just a heads up

Windows has been much more stagnant in terms of not making huge changes like OS X has over that period  
Basically MS has put new API’s in place & new lipstick on it but the old APIs still got bug fixes & updates

OS X less so may break your apps built with 2007 in weird & wonderful ways as Apple fixed bugs & we found we had done things in ways that were not optimal for the Carbon API’s from that vintage

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 6, 2014, 3:34am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/9 "2014-03-06T03:34:40Z")

</div>

You can try your app in the latest Xojo release for free.

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_carter/32/476_2.png) [@Chris\_Carter](https://forum.xojo.com/u/Chris_Carter)\
**Post date:** [March 6, 2014, 10:58am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/10 "2014-03-06T10:58:18Z")

</div>

> [@69585:@Michael Walach](#):
>
> any other ideas?

It looks to me as though if you hit more than one key, or have an encoding issue, then you may be getting mor than one character at a time whe doing the readall. Perhaps it’s worth looping on the received string to check each character individually, - or at least check it is only 1 byte long and pop up a debug message if so,

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 12:47am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/11 "2014-03-07T00:47:16Z")

</div>

ok, thanks guys. I am downloading xojo and will try it out. It would be great if that was the only issue. If not I will try your other suggestions. I love this forum, you guys have helped me out so many times in the past, it is why I keep coming back!

thanks

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 12:58am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/12 "2014-03-07T00:58:59Z")

</div>

running in XOJO with the same issue. Sometimes it works, sometimes it doesn’t. I’m pretty sure I am only getting one charecter. My firmware on the arduino I wrote, and it only sends a single letter “A” when the beam of my photogate is broken.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 7, 2014, 1:02am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/13 "2014-03-07T01:02:31Z")

</div>

> [@69713:@Chris Carter](#):
>
> at least check it is only 1 byte long and pop up a debug message if so,

Have you tried this yet? Depending on handshaking and speed of transmission, it is possible to get more than one “send” in a single DataAvailable event. Possible you’re getting “AA” or “AB”?

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 1:02am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/14 "2014-03-07T01:02:56Z")

</div>

here is all of the code in my serial event:

```auto
  
  
  EditField2.TEXT=trim(Serial1.ReadAll)
  
  IF EditField2.TEXT="A" and lane1running=false then
    lane1running=true
    thread1.run
    thread3.kill
  END IF
  
  
  IF EditField2.TEXT="1" and lane1running=true THEN 'end lane 1
    lane1running=false
    thread1.kill
    thread3.run
  END IF
  
  IF EditField2.TEXT="B" and lane2running=false then
    lane2running=true
    thread2.run
    thread4.kill
  END IF
  
  
  IF EditField2.TEXT="2" and lane2running=true THEN 'end lane 2
    lane2running=false
    thread2.kill
    thread4.run
  END IF
  
```

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 1:08am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/15 "2014-03-07T01:08:50Z")

</div>

ok, how do I check if it one byte long?

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 1:21am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/16 "2014-03-07T01:21:36Z")

</div>

Ok, I tried this…

```auto
dim s as string
dim i as integer

  EditField2.TEXT=Serial1.ReadAll
  s=Serial1.ReadAll
  i=len(s)
  if EditField2.text<> "A" then
    MsgBox str(i)
  end if
```

I can break my timing gate beam with a pencil as fast as I possibly can and it catches it everytime, meaning the msgbox pops up everytime showing that “i” has a value of “0”. however, my threads do not fire off. I really can’t wrap my head around what is going on here…

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 7, 2014, 5:22am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/17 "2014-03-07T05:22:10Z")

</div>

If I has a value of zero, that means you’re getting an empty string. Which means that trim is removing whatever was there. Try EncodeHex.

s = me.readall  
s = EncodeHex (s)  
msgbox (s)

What do you get?

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_carter/32/476_2.png) [@Chris\_Carter](https://forum.xojo.com/u/Chris_Carter)\
**Post date:** [March 7, 2014, 9:06am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/18 "2014-03-07T09:06:00Z")

</div>

I think I spot a bug in this code…

[quote]EditField2.TEXT=Serial1.ReadAll  
s=Serial1.ReadAll  
i=len(s)[/quote]

You are doing the ReadAll twice …  
The first line is emptying the buffer into the text field,  
the 2nd line is reading what is now an empty buffer.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [March 7, 2014, 9:31am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/19 "2014-03-07T09:31:03Z")

</div>

Good catch! I=0 is just a red herring. But do the EncodeHex anyway.

---

<div class="post-metadata">

**Author:** ![Michael\_Walach](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/3be4f8/32.png) [@Michael\_Walach](https://forum.xojo.com/u/Michael_Walach)\
**Post date:** [March 7, 2014, 11:21am UTC](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785/20 "2014-03-07T11:21:25Z")

</div>

Thanks for the help guys. You were right, I was getting more than one “A” at a time. Here is the finished working code.

```auto
  dim s as string
  

  s=Serial1.ReadAll
  s=Left(s, 1)
  EditField2.text=s
  
  IF s="A" and lane1running=false then
    lane1running=true
    thread1.run
    thread3.kill
  END IF
  
  
  IF s="1" and lane1running=true THEN 'end lane 1
    lane1running=false
    thread1.kill
    thread3.run
  END IF
  
  IF s="B" and lane2running=false then
    lane2running=true
    thread2.run
    thread4.kill
  END IF
  
  
  IF s="2" and lane2running=true THEN 'end lane 2
    lane2running=false
    thread2.kill
    thread4.run
  END IF
  
```

[Next page](https://forum.xojo.com/t/serial-read-problem-on-mac-only/14785.md?page=2)
