Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:17 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite 170 comm port issues

Author Message
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 07:06am 12 Sep 2020
Copy link to clipboard 
Print this post

I have an issue with a program using MMBasic 5.4.4 on a MX170 28 pin chip. I am using Com1 Rx at 9600 and opening and closing the port in a sub that does not call any other subs but has interrupts that don't involve the port. As the Tx pin for Com1 was not being used I thought I would be smart and use it to produce a log file. During the Log File sub I am using Com1 Tx at 38400 and again opening and closing at the end of the sub. The problem I am having is occasionally the program stops on error Com port already open.
I thought I had found the problem by putting a 200 mS pause after Close #1 which seemed to fix the problem but alas it still happens but not as often.

Any suggestions welcome

OA47
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:42am 12 Sep 2020
Copy link to clipboard 
Print this post

Flags.  Flags are good.

Set a flag when you open the COM port, and clear it when you close the COM port.
Check the flag to find out if the COM port is still open BEFORE you try to open it again.  If the flag is set, then close the COM port, THEN re-open it.

Personally, I like to reserve the COM ports, so if I am using them in a program, I open them at the top of the code, and leave them open.  They are dedicated to the serial device they are connected to and are not used for anything else.  That works for me, and is just my method, but I can see why you want to close and re-open, so you can nab those pins for other uses elsewhere in your code.
Smoke makes things work. When the smoke gets out, it stops!
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 08:23am 12 Sep 2020
Copy link to clipboard 
Print this post

  Grogster said  Flags.  Flags are good.

Set a flag when you open the COM port, and clear it when you close the COM port.
Check the flag to find out if the COM port is still open BEFORE you try to open it again.  If the flag is set, then close the COM port, THEN re-open it.

Personally, I like to reserve the COM ports, so if I am using them in a program, I open them at the top of the code, and leave them open.  They are dedicated to the serial device they are connected to and are not used for anything else.  That works for me, and is just my method, but I can see why you want to close and re-open, so you can nab those pins for other uses elsewhere in your code.


+1
I usually leave the port open. I do, however have a situation where I connect to a device that defaults to 19.2K BAUD but I need to run much faster. I open at 19.2, issue a command to the other device to switch to 115K, close my port and re-open at 115K. This must be the first time I have ever closed a port.

Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 05:01pm 12 Sep 2020
Copy link to clipboard 
Print this post

As Tinine suggested... just leave it open

I do the same with SPI ports. Opening and closing might sound like good practice but it's only your stuff running and just save yourself the time.

Most of my code has an open in the preamble and no corresponding close.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:09pm 12 Sep 2020
Copy link to clipboard 
Print this post

He can't leave the port open.
He is using it at two different baudrates.

Flags and do all the work in the one main loop to prevent an interrupt getting in the way.

Jim
VK7JH
MMedit
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 09:11pm 12 Sep 2020
Copy link to clipboard 
Print this post

  Quote  As Tinine suggested... just leave it open


I have to close the port and re-open to change the baud rate for each routine. If I don't the log file would have to be generated with the slow speed that the Rx is running at and this would introduce more Rx downtime while the log is being written.


OA47
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 12:16am 13 Sep 2020
Copy link to clipboard 
Print this post

Hi OA47,

Could you not use another comm port to write your log file?

Regards,

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 12:53am 13 Sep 2020
Copy link to clipboard 
Print this post

  Quote  Could you not use another comm port to write your log file?


Mick, all other ports/pins are in use. Although Com1 Rx is monitored by the program I thought I would use the Com1 Tx as it was a spare pin and not used.


I have inserted a ComOpen flag and check this before trying to OPEN the port. It took around 24 hours for the last error to appear (Port opened and closed probably 1000 times)

Will see what happens from here.
OA47
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 01:48am 13 Sep 2020
Copy link to clipboard 
Print this post

There is also the Cfunction/Csub serial transmitter for any pin if you want to use that.  Probably not possible, as it sounds like you have used all the other pins for things.  It is amazing how easy it is to use up all the pins on the 28-pin device.

I guess that is where the 44-pin chip could help you, but probably a bit late to suggest that, yes?
Smoke makes things work. When the smoke gets out, it stops!
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 02:25am 13 Sep 2020
Copy link to clipboard 
Print this post

Hi OA47

For what it is worth, I use ON ERROR SKIP immediately before OPEN or CLOSE COM commands when sorting out comms issues similar to yours.

This may help.

Brian
ChopperP
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 06:26pm 13 Sep 2020
Copy link to clipboard 
Print this post

  TassyJim said  He can't leave the port open.
He is using it at two different baudrates.

  OA47 said  
I have to close the port and re-open to change the baud rate for each routine. If I don't the log file would have to be generated with the slow speed that the Rx is running at and this would introduce more Rx downtime while the log is being written.


OA47


ah. yes... didn't read the post properly, soz
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025